Skip to content

Commit f80edc8

Browse files
TatevikGrtatevikg1
andauthored
Subscribepage (#353)
* subscriber page manager * owner entity * test * ci fix * getByPage data --------- Co-authored-by: Tatevik <[email protected]>
1 parent ab6c5fd commit f80edc8

File tree

7 files changed

+388
-4
lines changed

7 files changed

+388
-4
lines changed

config/services/managers.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,7 @@ services:
7171
PhpList\Core\Domain\Subscription\Service\Manager\SubscriberBlacklistManager:
7272
autowire: true
7373
autoconfigure: true
74+
75+
PhpList\Core\Domain\Subscription\Service\Manager\SubscribePageManager:
76+
autowire: true
77+
autoconfigure: true

config/services/repositories.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,13 @@ services:
120120
parent: PhpList\Core\Domain\Common\Repository\AbstractRepository
121121
arguments:
122122
- PhpList\Core\Domain\Subscription\Model\UserBlacklistData
123+
124+
PhpList\Core\Domain\Subscription\Repository\SubscriberPageRepository:
125+
parent: PhpList\Core\Domain\Common\Repository\AbstractRepository
126+
arguments:
127+
- PhpList\Core\Domain\Subscription\Model\SubscribePage
128+
129+
PhpList\Core\Domain\Subscription\Repository\SubscriberPageDataRepository:
130+
parent: PhpList\Core\Domain\Common\Repository\AbstractRepository
131+
arguments:
132+
- PhpList\Core\Domain\Subscription\Model\SubscribePageData

src/Domain/Subscription/Model/SubscribePage.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Doctrine\ORM\Mapping as ORM;
88
use PhpList\Core\Domain\Common\Model\Interfaces\DomainModel;
99
use PhpList\Core\Domain\Common\Model\Interfaces\Identity;
10+
use PhpList\Core\Domain\Identity\Model\Administrator;
1011
use PhpList\Core\Domain\Subscription\Repository\SubscriberPageRepository;
1112

1213
#[ORM\Entity(repositoryClass: SubscriberPageRepository::class)]
@@ -24,8 +25,9 @@ class SubscribePage implements DomainModel, Identity
2425
#[ORM\Column(name: 'active', type: 'boolean', options: ['default' => 0])]
2526
private bool $active = false;
2627

27-
#[ORM\Column(name: 'owner', type: 'integer', nullable: true)]
28-
private ?int $owner = null;
28+
#[ORM\ManyToOne(targetEntity: Administrator::class)]
29+
#[ORM\JoinColumn(name: 'owner', referencedColumnName: 'id', nullable: true)]
30+
private ?Administrator $owner = null;
2931

3032
public function getId(): ?int
3133
{
@@ -42,7 +44,7 @@ public function isActive(): bool
4244
return $this->active;
4345
}
4446

45-
public function getOwner(): ?int
47+
public function getOwner(): ?Administrator
4648
{
4749
return $this->owner;
4850
}
@@ -59,7 +61,7 @@ public function setActive(bool $active): self
5961
return $this;
6062
}
6163

62-
public function setOwner(?int $owner): self
64+
public function setOwner(?Administrator $owner): self
6365
{
6466
$this->owner = $owner;
6567
return $this;

src/Domain/Subscription/Repository/SubscriberPageDataRepository.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,21 @@
77
use PhpList\Core\Domain\Common\Repository\AbstractRepository;
88
use PhpList\Core\Domain\Common\Repository\CursorPaginationTrait;
99
use PhpList\Core\Domain\Common\Repository\Interfaces\PaginatableRepositoryInterface;
10+
use PhpList\Core\Domain\Subscription\Model\SubscribePage;
11+
use PhpList\Core\Domain\Subscription\Model\SubscribePageData;
1012

1113
class SubscriberPageDataRepository extends AbstractRepository implements PaginatableRepositoryInterface
1214
{
1315
use CursorPaginationTrait;
16+
17+
public function findByPageAndName(SubscribePage $page, string $name): ?SubscribePageData
18+
{
19+
return $this->findOneBy(['id' => $page->getId(), 'name' => $name]);
20+
}
21+
22+
/** @return SubscribePageData[] */
23+
public function getByPage(SubscribePage $page): array
24+
{
25+
return $this->findBy(['id' => $page->getId()]);
26+
}
1427
}

src/Domain/Subscription/Repository/SubscriberPageRepository.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,24 @@
77
use PhpList\Core\Domain\Common\Repository\AbstractRepository;
88
use PhpList\Core\Domain\Common\Repository\CursorPaginationTrait;
99
use PhpList\Core\Domain\Common\Repository\Interfaces\PaginatableRepositoryInterface;
10+
use PhpList\Core\Domain\Subscription\Model\SubscribePage;
11+
use PhpList\Core\Domain\Subscription\Model\SubscribePageData;
1012

1113
class SubscriberPageRepository extends AbstractRepository implements PaginatableRepositoryInterface
1214
{
1315
use CursorPaginationTrait;
16+
17+
/** @return array{page: SubscribePage, data: SubscribePageData}[] */
18+
public function findPagesWithData(int $pageId): array
19+
{
20+
return $this->createQueryBuilder('p')
21+
->select('p AS page, d AS data')
22+
->from(SubscribePage::class, 'p')
23+
->from(SubscribePageData::class, 'd')
24+
->where('p.id = :id')
25+
->andWhere('d.id = p.id')
26+
->setParameter('id', $pageId)
27+
->getQuery()
28+
->getResult();
29+
}
1430
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\Core\Domain\Subscription\Service\Manager;
6+
7+
use Doctrine\ORM\EntityManagerInterface;
8+
use PhpList\Core\Domain\Identity\Model\Administrator;
9+
use PhpList\Core\Domain\Subscription\Model\SubscribePage;
10+
use PhpList\Core\Domain\Subscription\Model\SubscribePageData;
11+
use PhpList\Core\Domain\Subscription\Repository\SubscriberPageDataRepository;
12+
use PhpList\Core\Domain\Subscription\Repository\SubscriberPageRepository;
13+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
14+
15+
class SubscribePageManager
16+
{
17+
public function __construct(
18+
private readonly SubscriberPageRepository $pageRepository,
19+
private readonly SubscriberPageDataRepository $pageDataRepository,
20+
private readonly EntityManagerInterface $entityManager,
21+
) {
22+
}
23+
24+
/**
25+
* @SuppressWarnings("BooleanArgumentFlag")
26+
*/
27+
public function createPage(string $title, bool $active = false, ?Administrator $owner = null): SubscribePage
28+
{
29+
$page = new SubscribePage();
30+
$page->setTitle($title)
31+
->setActive($active)
32+
->setOwner($owner);
33+
34+
$this->pageRepository->save($page);
35+
36+
return $page;
37+
}
38+
39+
public function getPage(int $id): SubscribePage
40+
{
41+
/** @var SubscribePage|null $page */
42+
$page = $this->pageRepository->find($id);
43+
if (!$page) {
44+
throw new NotFoundHttpException('Subscribe page not found');
45+
}
46+
47+
return $page;
48+
}
49+
50+
public function updatePage(
51+
SubscribePage $page,
52+
?string $title = null,
53+
?bool $active = null,
54+
?Administrator $owner = null
55+
): SubscribePage {
56+
if ($title !== null) {
57+
$page->setTitle($title);
58+
}
59+
if ($active !== null) {
60+
$page->setActive($active);
61+
}
62+
if ($owner !== null) {
63+
$page->setOwner($owner);
64+
}
65+
66+
$this->entityManager->flush();
67+
68+
return $page;
69+
}
70+
71+
public function setActive(SubscribePage $page, bool $active): void
72+
{
73+
$page->setActive($active);
74+
$this->entityManager->flush();
75+
}
76+
77+
public function deletePage(SubscribePage $page): void
78+
{
79+
$this->pageRepository->remove($page);
80+
}
81+
82+
/** @return SubscribePageData[] */
83+
public function getPageData(SubscribePage $page): array
84+
{
85+
return $this->pageDataRepository->getByPage($page,);
86+
}
87+
88+
public function setPageData(SubscribePage $page, string $name, ?string $value): SubscribePageData
89+
{
90+
/** @var SubscribePageData|null $data */
91+
$data = $this->pageDataRepository->findByPageAndName($page, $name);
92+
93+
if (!$data) {
94+
$data = (new SubscribePageData())
95+
->setId((int)$page->getId())
96+
->setName($name);
97+
$this->entityManager->persist($data);
98+
}
99+
100+
$data->setData($value);
101+
$this->entityManager->flush();
102+
103+
return $data;
104+
}
105+
}

0 commit comments

Comments
 (0)