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
declare(strict_types=1);
namespace src\PointToolsBundle\Service\Factory\Blogs;
namespace App\Factory\Blog;
use App\Factory\AbstractFactory;
use Psr\Log\LoggerInterface;
use src\PointToolsBundle\Entity\Blogs\File;
use src\PointToolsBundle\Repository\Blogs\FileRepository;
use src\PointToolsBundle\Exception\Api\InvalidResponseException;
use src\PointToolsBundle\Service\Factory\AbstractFactory;
use App\Entity\Blog\File;
use App\Repository\Blog\FileRepository;
class FileFactory extends AbstractFactory
{
/** @var FileRepository */
private $fileRepository;
public function __construct(LoggerInterface $logger, FileRepository $fileRepository)
{
public function __construct(
LoggerInterface $logger,
private readonly FileRepository $fileRepository,
) {
parent::__construct($logger);
$this->fileRepository = $fileRepository;
}
/**
@ -42,39 +39,17 @@ class FileFactory extends AbstractFactory
return $files;
}
/**
* @param string $url
*
* @return File
*
* @throws InvalidResponseException
*/
public function createFromUrl(string $url): File
{
$this->validateData($url);
// Replacing HTTP with HTTPS
$url = str_replace('http://', 'https://', $url);
if (null === ($file = $this->fileRepository->findOneBy(['remoteUrl' => $url]))) {
// Creating new file
$file = new File($url);
$this->fileRepository->add($file);
$this->fileRepository->save($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
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 Psr\Log\LoggerInterface;
use src\PointToolsBundle\DTO\Api\{PostsPage};
use src\PointToolsBundle\Entity\{Blogs\PostTag};
use src\PointToolsBundle\Exception\{Api\InvalidResponseException};
use src\PointToolsBundle\Repository\Blogs\PostRepository;
use src\PointToolsBundle\Service\Factory\{AbstractFactory, Blogs\CommentFactory, Blogs\TagFactory};
use src\PointToolsBundle\DTO\Api\MetaPost;
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;
use App\DTO\Api\{MetaPost, PostsPage, Post as PostDTO};
use App\Entity\Blog\{PostTag, Post};
use App\Entity\User;
use App\Exception\{Api\InvalidResponseException};
use App\Exception\Factory\Blog\InvalidDataException;
use App\Repository\Blog\PostRepository;
class PostFactory extends AbstractFactory
{
/** @var EntityManagerInterface */
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(
public function __construct(
LoggerInterface $logger,
EntityManagerInterface $em,
PostRepository $postRepository,
UserFactory $userFactory,
FileFactory $fileFactory,
CommentFactory $commentFactory,
TagFactory $tagFactory
private readonly EntityManagerInterface $em,
private readonly PostRepository $postRepository,
private readonly UserFactory $userFactory,
private readonly FileFactory $fileFactory,
private readonly CommentFactory $commentFactory,
private readonly TagFactory $tagFactory,
) {
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()),
$postData->getType() ?: Post::TYPE_POST
);
$this->postRepository->add($post);
$this->postRepository->save($post);
}
$post
@ -159,10 +130,7 @@ class PostFactory extends AbstractFactory
return $post;
}
/**
* @param Post $post
* @param string[] $tagsStrings
*/
/** @param string[] $tagsStrings */
private function updatePostTags(Post $post, array $tagsStrings): void
{
$tags = $this->tagFactory->createFromStringsArray($tagsStrings);
@ -170,41 +138,37 @@ class PostFactory extends AbstractFactory
// Hashing tags strings
$tagStringsHash = [];
foreach ($tagsStrings as $tagsString) {
$tagStringsHash[mb_strtolower($tagsString)] = $tagsString;
$tagStringsHash[\mb_strtolower($tagsString)] = $tagsString;
}
// Hashing current post tags
$newTagsHash = [];
foreach ($tags as $tag) {
$newTagsHash[mb_strtolower($tag->getText())] = $tag;
$newTagsHash[\mb_strtolower($tag->getText())] = $tag;
}
// Hashing old post tags (from DB)
$oldTagsHash = [];
foreach ($post->getPostTags() as $postTag) {
$oldTagsHash[mb_strtolower($postTag->getOriginalTagText())] = $postTag;
$oldTagsHash[\mb_strtolower($postTag->getOriginalTagText())] = $postTag;
}
// Adding missing tags
foreach ($tags as $tag) {
if (!array_key_exists(mb_strtolower($tag->getText()), $oldTagsHash)) {
$tmpPostTag = new PostTag($post, $tag, $tagStringsHash[mb_strtolower($tag->getText())]);
if (!array_key_exists(\mb_strtolower($tag->getText()), $oldTagsHash)) {
$tmpPostTag = new PostTag($post, $tag, $tagStringsHash[\mb_strtolower($tag->getText())]);
$post->addPostTag($tmpPostTag);
}
}
// Removing deleted tags
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);
}
}
}
/**
* @param Post $post
* @param array $urls
*/
private function updatePostFiles(Post $post, array $urls): void
{
$files = $this->fileFactory->createFromUrlsArray($urls);

View file

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

View file

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

View file

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