Ported {Comment, File, Post, Tag}Factory. Service\Api\{Post, User}Api modified accordingly.

This commit is contained in:
Alexey Skobkin 2023-03-28 21:45:41 +03:00
parent 5afc8d91b4
commit 5e2f8787ae
No known key found for this signature in database
GPG key ID: 5D5CEF6F221278E7
7 changed files with 70 additions and 143 deletions

View file

@ -1,30 +0,0 @@
<?php
namespace src\PointToolsBundle\Service\Factory\Blogs;
use Psr\Log\LoggerInterface;
use Skobkin\Bundle\PointToolsBundle\Repository\Blogs\{
src\PointToolsBundle\Repository\Blogs\CommentRepository, src\PointToolsBundle\Repository\Blogs\PostRepository};
use src\PointToolsBundle\Service\Factory\{AbstractFactory};
use src\PointToolsBundle\Service\Factory\UserFactory;
class CommentFactory extends AbstractFactory
{
/** @var \src\PointToolsBundle\Repository\Blogs\CommentRepository */
private $commentRepository;
/** @var \src\PointToolsBundle\Repository\Blogs\PostRepository */
private $postRepository;
/** @var UserFactory */
private $userFactory;
public function __construct(LoggerInterface $logger, \src\PointToolsBundle\Repository\Blogs\CommentRepository $commentRepository, \src\PointToolsBundle\Repository\Blogs\PostRepository $postRepository, UserFactory $userFactory)
{
parent::__construct($logger);
$this->userFactory = $userFactory;
$this->commentRepository = $commentRepository;
$this->postRepository = $postRepository;
}
}

View file

@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace App\Factory\Blog;
use App\Factory\{AbstractFactory};
use App\Factory\UserFactory;
use Psr\Log\LoggerInterface;
use App\Repository\Blog\{CommentRepository, PostRepository};
class CommentFactory extends AbstractFactory
{
public function __construct(
LoggerInterface $logger,
private readonly CommentRepository $commentRepository,
private readonly PostRepository $postRepository,
private readonly UserFactory $userFactory,
) {
parent::__construct($logger);
}
}

View file

@ -1,23 +1,20 @@
<?php <?php
declare(strict_types=1);
namespace src\PointToolsBundle\Service\Factory\Blogs; namespace App\Factory\Blog;
use App\Factory\AbstractFactory;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use src\PointToolsBundle\Entity\Blogs\File; use App\Entity\Blog\File;
use src\PointToolsBundle\Repository\Blogs\FileRepository; use App\Repository\Blog\FileRepository;
use src\PointToolsBundle\Exception\Api\InvalidResponseException;
use src\PointToolsBundle\Service\Factory\AbstractFactory;
class FileFactory extends AbstractFactory class FileFactory extends AbstractFactory
{ {
/** @var FileRepository */ public function __construct(
private $fileRepository; LoggerInterface $logger,
private readonly FileRepository $fileRepository,
) {
public function __construct(LoggerInterface $logger, FileRepository $fileRepository)
{
parent::__construct($logger); parent::__construct($logger);
$this->fileRepository = $fileRepository;
} }
/** /**
@ -42,39 +39,17 @@ class FileFactory extends AbstractFactory
return $files; return $files;
} }
/**
* @param string $url
*
* @return File
*
* @throws InvalidResponseException
*/
public function createFromUrl(string $url): File public function createFromUrl(string $url): File
{ {
$this->validateData($url);
// Replacing HTTP with HTTPS // Replacing HTTP with HTTPS
$url = str_replace('http://', 'https://', $url); $url = str_replace('http://', 'https://', $url);
if (null === ($file = $this->fileRepository->findOneBy(['remoteUrl' => $url]))) { if (null === ($file = $this->fileRepository->findOneBy(['remoteUrl' => $url]))) {
// Creating new file // Creating new file
$file = new File($url); $file = new File($url);
$this->fileRepository->add($file); $this->fileRepository->save($file);
} }
return $file; return $file;
} }
}
/**
* @param $data
*
* @throws InvalidResponseException
*/
private function validateData($data): void
{
if (!is_string($data)) {
// @todo Change exception
throw new InvalidResponseException('File data must be a string');
}
}
}

View file

@ -1,60 +1,31 @@
<?php <?php
declare(strict_types=1);
namespace src\PointToolsBundle\Service\Factory\Blogs; namespace App\Factory\Blog;
use App\Factory\AbstractFactory;
use App\Factory\UserFactory;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use src\PointToolsBundle\DTO\Api\{PostsPage}; use App\DTO\Api\{MetaPost, PostsPage, Post as PostDTO};
use src\PointToolsBundle\Entity\{Blogs\PostTag}; use App\Entity\Blog\{PostTag, Post};
use src\PointToolsBundle\Exception\{Api\InvalidResponseException}; use App\Entity\User;
use src\PointToolsBundle\Repository\Blogs\PostRepository; use App\Exception\{Api\InvalidResponseException};
use src\PointToolsBundle\Service\Factory\{AbstractFactory, Blogs\CommentFactory, Blogs\TagFactory}; use App\Exception\Factory\Blog\InvalidDataException;
use src\PointToolsBundle\DTO\Api\MetaPost; use App\Repository\Blog\PostRepository;
use src\PointToolsBundle\DTO\Api\Post as PostDTO;
use src\PointToolsBundle\Entity\Blogs\Post;
use src\PointToolsBundle\Entity\User;
use src\PointToolsBundle\Exception\Factory\Blog\InvalidDataException;
use src\PointToolsBundle\Service\Factory\Blogs\FileFactory;
use src\PointToolsBundle\Service\Factory\UserFactory;
use function Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\mb_strtolower;
class PostFactory extends AbstractFactory class PostFactory extends AbstractFactory
{ {
/** @var EntityManagerInterface */ public function __construct(
private $em;
/** @var PostRepository */
private $postRepository;
/** @var UserFactory */
private $userFactory;
/** @var FileFactory */
private $fileFactory;
/** @var CommentFactory */
private $commentFactory;
/** @var TagFactory */
private $tagFactory;
public function __construct(
LoggerInterface $logger, LoggerInterface $logger,
EntityManagerInterface $em, private readonly EntityManagerInterface $em,
PostRepository $postRepository, private readonly PostRepository $postRepository,
UserFactory $userFactory, private readonly UserFactory $userFactory,
FileFactory $fileFactory, private readonly FileFactory $fileFactory,
CommentFactory $commentFactory, private readonly CommentFactory $commentFactory,
TagFactory $tagFactory private readonly TagFactory $tagFactory,
) { ) {
parent::__construct($logger); parent::__construct($logger);
$this->em = $em;
$this->postRepository = $postRepository;
$this->userFactory = $userFactory;
$this->fileFactory = $fileFactory;
$this->commentFactory = $commentFactory;
$this->tagFactory = $tagFactory;
} }
/** /**
@ -148,7 +119,7 @@ class PostFactory extends AbstractFactory
new \DateTime($postData->getCreated()), new \DateTime($postData->getCreated()),
$postData->getType() ?: Post::TYPE_POST $postData->getType() ?: Post::TYPE_POST
); );
$this->postRepository->add($post); $this->postRepository->save($post);
} }
$post $post
@ -159,10 +130,7 @@ class PostFactory extends AbstractFactory
return $post; return $post;
} }
/** /** @param string[] $tagsStrings */
* @param Post $post
* @param string[] $tagsStrings
*/
private function updatePostTags(Post $post, array $tagsStrings): void private function updatePostTags(Post $post, array $tagsStrings): void
{ {
$tags = $this->tagFactory->createFromStringsArray($tagsStrings); $tags = $this->tagFactory->createFromStringsArray($tagsStrings);
@ -170,41 +138,37 @@ class PostFactory extends AbstractFactory
// Hashing tags strings // Hashing tags strings
$tagStringsHash = []; $tagStringsHash = [];
foreach ($tagsStrings as $tagsString) { foreach ($tagsStrings as $tagsString) {
$tagStringsHash[mb_strtolower($tagsString)] = $tagsString; $tagStringsHash[\mb_strtolower($tagsString)] = $tagsString;
} }
// Hashing current post tags // Hashing current post tags
$newTagsHash = []; $newTagsHash = [];
foreach ($tags as $tag) { foreach ($tags as $tag) {
$newTagsHash[mb_strtolower($tag->getText())] = $tag; $newTagsHash[\mb_strtolower($tag->getText())] = $tag;
} }
// Hashing old post tags (from DB) // Hashing old post tags (from DB)
$oldTagsHash = []; $oldTagsHash = [];
foreach ($post->getPostTags() as $postTag) { foreach ($post->getPostTags() as $postTag) {
$oldTagsHash[mb_strtolower($postTag->getOriginalTagText())] = $postTag; $oldTagsHash[\mb_strtolower($postTag->getOriginalTagText())] = $postTag;
} }
// Adding missing tags // Adding missing tags
foreach ($tags as $tag) { foreach ($tags as $tag) {
if (!array_key_exists(mb_strtolower($tag->getText()), $oldTagsHash)) { if (!array_key_exists(\mb_strtolower($tag->getText()), $oldTagsHash)) {
$tmpPostTag = new PostTag($post, $tag, $tagStringsHash[mb_strtolower($tag->getText())]); $tmpPostTag = new PostTag($post, $tag, $tagStringsHash[\mb_strtolower($tag->getText())]);
$post->addPostTag($tmpPostTag); $post->addPostTag($tmpPostTag);
} }
} }
// Removing deleted tags // Removing deleted tags
foreach ($post->getPostTags() as $postTag) { foreach ($post->getPostTags() as $postTag) {
if (!array_key_exists(mb_strtolower($postTag->getOriginalTagText()), $newTagsHash)) { if (!array_key_exists(\mb_strtolower($postTag->getOriginalTagText()), $newTagsHash)) {
$post->removePostTag($postTag); $post->removePostTag($postTag);
} }
} }
} }
/**
* @param Post $post
* @param array $urls
*/
private function updatePostFiles(Post $post, array $urls): void private function updatePostFiles(Post $post, array $urls): void
{ {
$files = $this->fileFactory->createFromUrlsArray($urls); $files = $this->fileFactory->createFromUrlsArray($urls);

View file

@ -1,23 +1,20 @@
<?php <?php
declare(strict_types=1);
namespace src\PointToolsBundle\Service\Factory\Blogs; namespace App\Factory\Blog;
use App\Factory\AbstractFactory;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use src\PointToolsBundle\Entity\Blogs\Tag; use App\Entity\Blog\Tag;
use src\PointToolsBundle\Repository\Blogs\TagRepository; use App\Repository\Blog\TagRepository;
use src\PointToolsBundle\Service\Factory\AbstractFactory;
class TagFactory extends AbstractFactory class TagFactory extends AbstractFactory
{ {
/** @var TagRepository */ public function __construct(
private $tagRepository; LoggerInterface $logger,
private readonly TagRepository $tagRepository,
) {
public function __construct(LoggerInterface $logger, TagRepository $tagRepository)
{
parent::__construct($logger); parent::__construct($logger);
$this->tagRepository = $tagRepository;
} }
/** /**
@ -47,7 +44,7 @@ class TagFactory extends AbstractFactory
if (null === ($tag = $this->tagRepository->findOneByLowerText($text))) { if (null === ($tag = $this->tagRepository->findOneByLowerText($text))) {
// Creating new tag // Creating new tag
$tag = new Tag($text); $tag = new Tag($text);
$this->tagRepository->add($tag); $this->tagRepository->save($tag);
} }
return $tag; return $tag;

View file

@ -5,7 +5,7 @@ namespace App\Service\Api;
use JMS\Serializer\SerializerInterface; use JMS\Serializer\SerializerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use App\Service\Factory\Blogs\PostFactory; use App\Factory\Blog\PostFactory;
use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\HttpClientInterface;
/** Basic Point.im user API functions from /api/post */ /** Basic Point.im user API functions from /api/post */
@ -15,7 +15,7 @@ class PostApi extends AbstractApi
HttpClientInterface $pointApiClient, HttpClientInterface $pointApiClient,
SerializerInterface $serializer, SerializerInterface $serializer,
LoggerInterface $logger, LoggerInterface $logger,
private readonly PostFactory $postFactory private readonly PostFactory $postFactory,
) { ) {
parent::__construct($pointApiClient, $logger, $serializer); parent::__construct($pointApiClient, $logger, $serializer);
} }

View file

@ -10,7 +10,7 @@ use App\Exception\Api\{ForbiddenException,
NotFoundException, NotFoundException,
UserNotFoundException UserNotFoundException
}; };
use App\Service\Factory\UserFactory; use App\Factory\UserFactory;
use JMS\Serializer\{DeserializationContext, SerializerInterface}; use JMS\Serializer\{DeserializationContext, SerializerInterface};
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\HttpClientInterface;