|
| 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\Subscription\Model\SubscribePage; |
| 9 | +use PhpList\Core\Domain\Subscription\Model\SubscribePageData; |
| 10 | +use PhpList\Core\Domain\Subscription\Repository\SubscriberPageDataRepository; |
| 11 | +use PhpList\Core\Domain\Subscription\Repository\SubscriberPageRepository; |
| 12 | +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
| 13 | + |
| 14 | +class SubscribePageManager |
| 15 | +{ |
| 16 | + public function __construct( |
| 17 | + private readonly SubscriberPageRepository $pageRepository, |
| 18 | + private readonly SubscriberPageDataRepository $pageDataRepository, |
| 19 | + private readonly EntityManagerInterface $entityManager, |
| 20 | + ) { |
| 21 | + } |
| 22 | + |
| 23 | + public function createPage(string $title, bool $active = false, ?int $owner = null): SubscribePage |
| 24 | + { |
| 25 | + $page = new SubscribePage(); |
| 26 | + $page->setTitle($title) |
| 27 | + ->setActive($active) |
| 28 | + ->setOwner($owner); |
| 29 | + |
| 30 | + $this->pageRepository->save($page); |
| 31 | + |
| 32 | + return $page; |
| 33 | + } |
| 34 | + |
| 35 | + public function getPage(int $id): SubscribePage |
| 36 | + { |
| 37 | + /** @var SubscribePage|null $page */ |
| 38 | + $page = $this->pageRepository->find($id); |
| 39 | + if (!$page) { |
| 40 | + throw new NotFoundHttpException('Subscribe page not found'); |
| 41 | + } |
| 42 | + |
| 43 | + return $page; |
| 44 | + } |
| 45 | + |
| 46 | + public function updatePage(SubscribePage $page, ?string $title = null, ?bool $active = null, ?int $owner = null): SubscribePage |
| 47 | + { |
| 48 | + if ($title !== null) { |
| 49 | + $page->setTitle($title); |
| 50 | + } |
| 51 | + if ($active !== null) { |
| 52 | + $page->setActive($active); |
| 53 | + } |
| 54 | + if ($owner !== null) { |
| 55 | + $page->setOwner($owner); |
| 56 | + } |
| 57 | + |
| 58 | + $this->entityManager->flush(); |
| 59 | + |
| 60 | + return $page; |
| 61 | + } |
| 62 | + |
| 63 | + public function setActive(SubscribePage $page, bool $active): void |
| 64 | + { |
| 65 | + $page->setActive($active); |
| 66 | + $this->entityManager->flush(); |
| 67 | + } |
| 68 | + |
| 69 | + public function deletePage(SubscribePage $page): void |
| 70 | + { |
| 71 | + $this->pageRepository->remove($page); |
| 72 | + } |
| 73 | + |
| 74 | + public function getPageData(SubscribePage $page, string $name): ?string |
| 75 | + { |
| 76 | + /** @var SubscribePageData|null $data */ |
| 77 | + $data = $this->pageDataRepository->findOneBy(['id' => $page->getId(), 'name' => $name]); |
| 78 | + return $data?->getData(); |
| 79 | + } |
| 80 | + |
| 81 | + public function setPageData(SubscribePage $page, string $name, ?string $value): SubscribePageData |
| 82 | + { |
| 83 | + /** @var SubscribePageData|null $data */ |
| 84 | + $data = $this->pageDataRepository->findOneBy(['id' => $page->getId(), 'name' => $name]); |
| 85 | + |
| 86 | + if (!$data) { |
| 87 | + $data = (new SubscribePageData()) |
| 88 | + ->setId((int)$page->getId()) |
| 89 | + ->setName($name); |
| 90 | + $this->entityManager->persist($data); |
| 91 | + } |
| 92 | + |
| 93 | + $data->setData($value); |
| 94 | + $this->entityManager->flush(); |
| 95 | + |
| 96 | + return $data; |
| 97 | + } |
| 98 | +} |
0 commit comments