diff --git a/src/Controller/ArticlesController.php b/src/Controller/ArticlesController.php index 9efa30c..fae9c47 100644 --- a/src/Controller/ArticlesController.php +++ b/src/Controller/ArticlesController.php @@ -3,26 +3,35 @@ namespace App\Controller; use App\Entity\Article; -use App\Entity\PostCount; use App\Form\ArticleType; -use App\Repository\PostCountRepository; -use DateTime; -use Exception; +use App\Service\ArticleService; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; +use Throwable; class ArticlesController extends AbstractController { + /** @var ArticleService */ + private $articleService; + + /** + * ArticlesController constructor. + * @param ArticleService $articleService + */ + public function __construct(ArticleService $articleService) + { + $this->articleService = $articleService; + } + /** * @Route("/articles", name="articles") */ public function index() { - $articleRepository = $this->getDoctrine()->getRepository(Article::class); - $articles = $articleRepository->findAll(); + $articles = $this->articleService->getList(); return $this->render('articles/index.html.twig', [ 'articles' => $articles, ]); @@ -32,7 +41,7 @@ public function index() * @Route("/articles/new", name="articles_new") * @param Request $request * @return RedirectResponse|Response - * @throws Exception + * @throws Throwable */ public function new(Request $request) { @@ -42,20 +51,7 @@ public function new(Request $request) $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $article = $form->getData(); - $entityManager = $this->getDoctrine()->getManager(); - $entityManager->persist($article); - /** @var PostCountRepository $postCountRepository */ - $postCountRepository = $this->getDoctrine()->getRepository(PostCount::class); - $postCount = $postCountRepository->findOneBy([ - 'postDate' => new DateTime(), - ]); - if (empty($postCount)) { - $postCount = new PostCount(); - $postCount->setPostDate(new DateTime()); - $entityManager->persist($postCount); - } - $postCount->setPostCount($postCount->getPostCount() + 1); - $entityManager->flush(); + $this->articleService->add($article); $this->addFlash('success', '登録しました'); return $this->redirectToRoute('articles'); diff --git a/src/Entity/Article.php b/src/Entity/Article.php index 50996b2..427acf5 100644 --- a/src/Entity/Article.php +++ b/src/Entity/Article.php @@ -2,6 +2,7 @@ namespace App\Entity; +use DateTime; use DateTimeInterface as DateTimeInterfaceAlias; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; @@ -95,7 +96,7 @@ public function setUpdatedAt(DateTimeInterfaceAlias $updatedAt): self */ public function prePersist() { - $now = new \DateTime(); + $now = new DateTime(); $this->setCreatedAt($now); $this->setUpdatedAt($now); } diff --git a/src/Entity/PostCount.php b/src/Entity/PostCount.php index 4c141f1..51af5dd 100644 --- a/src/Entity/PostCount.php +++ b/src/Entity/PostCount.php @@ -2,6 +2,7 @@ namespace App\Entity; +use DateTimeInterface; use Doctrine\ORM\Mapping as ORM; /** @@ -34,12 +35,12 @@ public function getId(): ?int return $this->id; } - public function getPostDate(): ?\DateTimeInterface + public function getPostDate(): ?DateTimeInterface { return $this->postDate; } - public function setPostDate(\DateTimeInterface $postDate): self + public function setPostDate(DateTimeInterface $postDate): self { $this->postDate = $postDate; @@ -57,4 +58,9 @@ public function setPostCount(int $postCount): self return $this; } + + public function incrementPostCount(): void + { + $this->postCount++; + } } diff --git a/src/Repository/ArticleRepository.php b/src/Repository/ArticleRepository.php index 3f1638d..7d92bb8 100644 --- a/src/Repository/ArticleRepository.php +++ b/src/Repository/ArticleRepository.php @@ -4,6 +4,7 @@ use App\Entity\Article; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; +use Doctrine\ORM\ORMException; use Doctrine\Persistence\ManagerRegistry; /** @@ -19,6 +20,15 @@ public function __construct(ManagerRegistry $registry) parent::__construct($registry, Article::class); } + /** + * @param Article $article + * @throws ORMException + */ + public function add(Article $article) + { + $this->getEntityManager()->persist($article); + } + // /** // * @return Article[] Returns an array of Article objects // */ diff --git a/src/Repository/PostCountRepository.php b/src/Repository/PostCountRepository.php index 116e999..2c31063 100644 --- a/src/Repository/PostCountRepository.php +++ b/src/Repository/PostCountRepository.php @@ -4,6 +4,7 @@ use App\Entity\PostCount; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; +use Doctrine\ORM\ORMException; use Doctrine\Persistence\ManagerRegistry; /** @@ -19,6 +20,15 @@ public function __construct(ManagerRegistry $registry) parent::__construct($registry, PostCount::class); } + /** + * @param PostCount $postCount + * @throws ORMException + */ + public function add(PostCount $postCount) + { + $this->getEntityManager()->persist($postCount); + } + // /** // * @return PostCount[] Returns an array of PostCount objects // */ diff --git a/src/Service/ArticleService.php b/src/Service/ArticleService.php new file mode 100644 index 0000000..34c26dc --- /dev/null +++ b/src/Service/ArticleService.php @@ -0,0 +1,78 @@ +entityManager = $entityManager; + $this->articleRepository = $articleRepository; + $this->postCountRepository = $postCountRepository; + } + + /** + * @return Article[] + */ + public function getList() + { + return $this->articleRepository->findAll(); + } + + /** + * @param Article $article + * @throws Throwable + */ + public function add(Article $article) + { + $this->entityManager->transactional(function () use ($article) { + $this->articleRepository->add($article); + $today = new DateTime(); + $postCount = $this->getPostCountOrCreate($today); + $postCount->incrementPostCount(); + }); + } + + /** + * @param DateTime $dateTime + * @return PostCount|null + * @throws ORMException + */ + private function getPostCountOrCreate(DateTime $dateTime) + { + $postCount = $this->postCountRepository->findOneBy([ + 'postDate' => $dateTime, + ]); + if (empty($postCount)) { + $postCount = new PostCount(); + $postCount->setPostDate($dateTime); + $this->postCountRepository->add($postCount); + } + + return $postCount; + } +} \ No newline at end of file