Compare commits

..

No commits in common. "5e2f8787ae8db5fedc1a65ca83b93ff91643caca" and "7c9ee60103dee70b37104af3d1fa8ed6901ba411" have entirely different histories.

10 changed files with 182 additions and 95 deletions

View file

@ -0,0 +1,16 @@
<?php
namespace src\PointToolsBundle\Service\Factory;
use Psr\Log\LoggerInterface;
abstract class AbstractFactory
{
/** @var LoggerInterface */
protected $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
}

View file

@ -0,0 +1,30 @@
<?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

@ -1,20 +1,23 @@
<?php
declare(strict_types=1);
namespace App\Factory\Blog;
namespace src\PointToolsBundle\Service\Factory\Blogs;
use App\Factory\AbstractFactory;
use Psr\Log\LoggerInterface;
use App\Entity\Blog\File;
use App\Repository\Blog\FileRepository;
use src\PointToolsBundle\Entity\Blogs\File;
use src\PointToolsBundle\Repository\Blogs\FileRepository;
use src\PointToolsBundle\Exception\Api\InvalidResponseException;
use src\PointToolsBundle\Service\Factory\AbstractFactory;
class FileFactory extends AbstractFactory
{
public function __construct(
LoggerInterface $logger,
private readonly FileRepository $fileRepository,
) {
/** @var FileRepository */
private $fileRepository;
public function __construct(LoggerInterface $logger, FileRepository $fileRepository)
{
parent::__construct($logger);
$this->fileRepository = $fileRepository;
}
/**
@ -39,17 +42,39 @@ 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->save($file);
$this->fileRepository->add($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,31 +1,60 @@
<?php
declare(strict_types=1);
namespace App\Factory\Blog;
namespace src\PointToolsBundle\Service\Factory\Blogs;
use App\Factory\AbstractFactory;
use App\Factory\UserFactory;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
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;
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;
class PostFactory extends AbstractFactory
{
public function __construct(
/** @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(
LoggerInterface $logger,
private readonly EntityManagerInterface $em,
private readonly PostRepository $postRepository,
private readonly UserFactory $userFactory,
private readonly FileFactory $fileFactory,
private readonly CommentFactory $commentFactory,
private readonly TagFactory $tagFactory,
EntityManagerInterface $em,
PostRepository $postRepository,
UserFactory $userFactory,
FileFactory $fileFactory,
CommentFactory $commentFactory,
TagFactory $tagFactory
) {
parent::__construct($logger);
$this->em = $em;
$this->postRepository = $postRepository;
$this->userFactory = $userFactory;
$this->fileFactory = $fileFactory;
$this->commentFactory = $commentFactory;
$this->tagFactory = $tagFactory;
}
/**
@ -119,7 +148,7 @@ class PostFactory extends AbstractFactory
new \DateTime($postData->getCreated()),
$postData->getType() ?: Post::TYPE_POST
);
$this->postRepository->save($post);
$this->postRepository->add($post);
}
$post
@ -130,7 +159,10 @@ class PostFactory extends AbstractFactory
return $post;
}
/** @param string[] $tagsStrings */
/**
* @param Post $post
* @param string[] $tagsStrings
*/
private function updatePostTags(Post $post, array $tagsStrings): void
{
$tags = $this->tagFactory->createFromStringsArray($tagsStrings);
@ -138,37 +170,41 @@ 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,20 +1,23 @@
<?php
declare(strict_types=1);
namespace App\Factory\Blog;
namespace src\PointToolsBundle\Service\Factory\Blogs;
use App\Factory\AbstractFactory;
use Psr\Log\LoggerInterface;
use App\Entity\Blog\Tag;
use App\Repository\Blog\TagRepository;
use src\PointToolsBundle\Entity\Blogs\Tag;
use src\PointToolsBundle\Repository\Blogs\TagRepository;
use src\PointToolsBundle\Service\Factory\AbstractFactory;
class TagFactory extends AbstractFactory
{
public function __construct(
LoggerInterface $logger,
private readonly TagRepository $tagRepository,
) {
/** @var TagRepository */
private $tagRepository;
public function __construct(LoggerInterface $logger, TagRepository $tagRepository)
{
parent::__construct($logger);
$this->tagRepository = $tagRepository;
}
/**
@ -44,7 +47,7 @@ class TagFactory extends AbstractFactory
if (null === ($tag = $this->tagRepository->findOneByLowerText($text))) {
// Creating new tag
$tag = new Tag($text);
$this->tagRepository->save($tag);
$this->tagRepository->add($tag);
}
return $tag;

View file

@ -1,25 +1,35 @@
<?php
declare(strict_types=1);
namespace App\Factory;
namespace src\PointToolsBundle\Service\Factory;
use Psr\Log\LoggerInterface;
use App\DTO\Api\User as UserDTO;
use App\Entity\User;
use App\Exception\Factory\InvalidUserDataException;
use App\Repository\UserRepository;
use src\PointToolsBundle\DTO\Api\User as UserDTO;
use src\PointToolsBundle\Entity\User;
use src\PointToolsBundle\Repository\UserRepository;
use src\PointToolsBundle\Exception\Factory\InvalidUserDataException;
use src\PointToolsBundle\Service\Factory\AbstractFactory;
class UserFactory extends AbstractFactory
{
public const DATE_FORMAT = 'Y-m-d_H:i:s';
public function __construct(
LoggerInterface $logger,
private readonly UserRepository $userRepository,
) {
/** @var UserRepository */
private $userRepository;
public function __construct(LoggerInterface $logger, UserRepository $userRepository)
{
parent::__construct($logger);
$this->userRepository = $userRepository;
}
/**
* @param UserDTO $userData
*
* @return User
*
* @throws InvalidUserDataException
*/
public function findOrCreateFromDTO(UserDTO $userData): User
{
// @todo LOG
@ -46,7 +56,9 @@ class UserFactory extends AbstractFactory
return $user;
}
/** @return User[] */
/**
* @return User[]
*/
public function findOrCreateFromDTOArray(array $usersData): array
{
// @todo LOG

View file

@ -1,14 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Factory;
use Psr\Log\LoggerInterface;
abstract class AbstractFactory
{
public function __construct(
protected LoggerInterface $logger,
) {
}
}

View file

@ -1,21 +0,0 @@
<?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

@ -5,7 +5,7 @@ namespace App\Service\Api;
use JMS\Serializer\SerializerInterface;
use Psr\Log\LoggerInterface;
use App\Factory\Blog\PostFactory;
use App\Service\Factory\Blogs\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\Factory\UserFactory;
use App\Service\Factory\UserFactory;
use JMS\Serializer\{DeserializationContext, SerializerInterface};
use Psr\Log\LoggerInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;