Compare commits

...

3 commits

Author SHA1 Message Date
Alexey Skobkin 441fdc06b3
Ported all API DTO's (without serializer configuration). 2023-03-28 22:57:11 +03:00
Alexey Skobkin b7e2cbc630
Ported DailyEvents and TopUserDTO. 2023-03-28 22:45:11 +03:00
Alexey Skobkin fd8161c9e1
Ported AccountFactory. 2023-03-28 22:29:19 +03:00
14 changed files with 180 additions and 398 deletions

View file

@ -1,56 +0,0 @@
<?php
namespace src\PointToolsBundle\DTO\Api;
use src\PointToolsBundle\DTO\Api\Comment;
use src\PointToolsBundle\DTO\Api\Post;
use src\PointToolsBundle\DTO\Api\ValidableInterface;
class MetaPost implements ValidableInterface
{
/**
* @var Post|null
*/
private $post;
/**
* @var Comment[]|null
*/
private $comments;
public function getPost(): ?Post
{
return $this->post;
}
public function setPost(?Post $post): void
{
$this->post = $post;
}
/**
* @return Comment[]|null
*/
public function getComments(): ?array
{
return $this->comments;
}
/**
* @param Comment[]|null $comments
*/
public function setComments(?array $comments): void
{
$this->comments = $comments;
}
public function isValid(): bool
{
if (null !== $this->post && $this->post->isValid()) {
return true;
}
return false;
}
}

View file

@ -1,35 +0,0 @@
<?php
namespace src\PointToolsBundle\DTO;
/**
* Events count by day
*/
class DailyEvents
{
/**
* @var \DateTime
*/
private $date;
/**
* @var int
*/
private $eventsCount;
public function __construct(string $date, int $eventsCount)
{
$this->date = new \DateTime($date);
$this->eventsCount = $eventsCount;
}
public function getDate(): \DateTime
{
return $this->date;
}
public function getEventsCount(): int
{
return $this->eventsCount;
}
}

View file

@ -1,35 +0,0 @@
<?php
namespace src\PointToolsBundle\DTO;
/**
* Data Transfer Object for top users list
*/
class TopUserDTO
{
/**
* @var string
*/
private $login;
/**
* @var int
*/
private $subscribersCount;
public function __construct(string $login, int $subscribersCount)
{
$this->login = $login;
$this->subscribersCount = $subscribersCount;
}
public function getLogin(): string
{
return $this->login;
}
public function getSubscribersCount(): int
{
return $this->subscribersCount;
}
}

View file

@ -1,40 +0,0 @@
<?php
namespace src\PointToolsBundle\Service\Factory\Telegram;
use Psr\Log\LoggerInterface;
use src\PointToolsBundle\Entity\Telegram\Account;
use src\PointToolsBundle\Repository\Telegram\AccountRepository;
use src\PointToolsBundle\Service\Factory\AbstractFactory;
use unreal4u\TelegramAPI\Telegram\Types\Message;
class AccountFactory extends AbstractFactory
{
/** @var AccountRepository */
private $accountRepo;
public function __construct(LoggerInterface $logger, AccountRepository $accountRepository)
{
parent::__construct($logger);
$this->accountRepo = $accountRepository;
}
public function findOrCreateFromMessage(Message $message): Account
{
if (null === $account = $this->accountRepo->findOneBy(['id' => $message->from->id])) {
$account = new Account($message->from->id);
$this->accountRepo->add($account);
}
// Setting/updating account data
$account->updateFromMessageData(
$message->from->first_name,
$message->from->last_name,
$message->from->username,
$message->chat->id
);
return $account;
}
}

View file

@ -1,26 +1,14 @@
<?php
declare(strict_types=1);
namespace src\PointToolsBundle\DTO\Api;
use src\PointToolsBundle\DTO\Api\ValidableInterface;
namespace App\DTO\Api;
/** TODO: Refactor to public readonly */
class Auth implements ValidableInterface
{
/**
* @var string|null
*/
private $token;
/**
* @var string|null
*/
private $csRfToken;
/**
* @var string|null
*/
private $error;
private ?string $token;
private ?string $csRfToken;
private ?string $error;
public function getToken(): ?string
{
@ -54,10 +42,6 @@ class Auth implements ValidableInterface
public function isValid(): bool
{
if (null !== $this->token && null !== $this->csRfToken && null === $this->error) {
return true;
}
return false;
return null !== $this->token && null !== $this->csRfToken && null === $this->error;
}
}
}

View file

@ -1,46 +1,18 @@
<?php
declare(strict_types=1);
namespace src\PointToolsBundle\DTO\Api;
use src\PointToolsBundle\DTO\Api\User;
use src\PointToolsBundle\DTO\Api\ValidableInterface;
namespace App\DTO\Api;
/** TODO: Refactor to public readonly */
class Comment implements ValidableInterface
{
/**
* @var string|null
*/
private $postId;
/**
* @var int|null
*/
private $number;
/**
* @var int|null
*/
private $toCommentId;
/**
* @var string|null
*/
private $created;
/**
* @var string|null
*/
private $text;
/**
* @var User|null
*/
private $author;
/**
* @var bool|null
*/
private $isRec;
private ?string $postId;
private ?int $number;
private ?int $toCommentId;
private ?string $created;
private ?string $text;
private ?User $author;
private ?bool $isRec;
public function getPostId(): ?string
@ -120,10 +92,6 @@ class Comment implements ValidableInterface
public function isValid(): bool
{
if (null !== $this->postId && null !== $this->number && null !== $this->author && null !== $this->text) {
return true;
}
return false;
return null !== $this->postId && null !== $this->number && null !== $this->author && null !== $this->text;
}
}
}

40
src/DTO/Api/MetaPost.php Normal file
View file

@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace App\DTO\Api;
/** TODO: Refactor to public readonly */
class MetaPost implements ValidableInterface
{
private ?Post $post;
/** @var Comment[]|null */
private ?array $comments;
public function getPost(): ?Post
{
return $this->post;
}
public function setPost(?Post $post): void
{
$this->post = $post;
}
/** @return Comment[]|null */
public function getComments(): ?array
{
return $this->comments;
}
/** @param Comment[]|null $comments */
public function setComments(?array $comments): void
{
$this->comments = $comments;
}
public function isValid(): bool
{
return null !== $this->post && $this->post->isValid();
}
}

View file

@ -1,52 +1,21 @@
<?php
declare(strict_types=1);
namespace src\PointToolsBundle\DTO\Api;
use src\PointToolsBundle\DTO\Api\User;
use src\PointToolsBundle\DTO\Api\ValidableInterface;
namespace App\DTO\Api;
/** TODO: Refactor to public readonly */
class Post implements ValidableInterface
{
/**
* @var string|null
*/
private $id;
/**
* @var string[]|null
*/
private $tags;
/**
* @var string[]|null
*/
private $files;
/**
* @var User|null
*/
private $author;
/**
* @var string|null
*/
private $text;
/**
* @var string|null
*/
private $created;
/**
* @var string|null
*/
private $type;
/**
* @var bool|null
*/
private $private;
private ?string $id;
/** @var string[]|null */
private ?array $tags;
/** @var string[]|null */
private ?array $files;
private ?User $author;
private ?string $text;
private ?string $created;
private ?string $type;
private ?bool $private;
public function getId(): ?string
{
@ -58,9 +27,7 @@ class Post implements ValidableInterface
$this->id = $id;
}
/**
* @return string[]|null
*/
/** @return string[]|null */
public function getTags(): ?array
{
return $this->tags;
@ -71,9 +38,7 @@ class Post implements ValidableInterface
$this->tags = $tags;
}
/**
* @return string[]|null
*/
/** @return string[]|null */
public function getFiles(): ?array
{
return $this->files;
@ -144,18 +109,13 @@ class Post implements ValidableInterface
public function isValid(): bool
{
if (
null !== $this->id &&
return null !== $this->id &&
null !== $this->author &&
$this->author->isValid() &&
null !== $this->text &&
null !== $this->created// &&
null !== $this->created
// @todo check type existence in incoming data
//null !== $this->type
) {
return true;
}
return false;
;
}
}

View file

@ -1,33 +1,22 @@
<?php
declare(strict_types=1);
namespace src\PointToolsBundle\DTO\Api;
use src\PointToolsBundle\DTO\Api\MetaPost;
use src\PointToolsBundle\DTO\Api\ValidableInterface;
namespace App\DTO\Api;
/** TODO: Refactor to public readonly */
class PostsPage implements ValidableInterface
{
/**
* @var MetaPost[]|null
*/
private $posts;
/** @var MetaPost[]|null */
private ?array $posts;
private ?bool $hasNext;
/**
* @var bool|null
*/
private $hasNext;
/**
* @return MetaPost[]|null
*/
/** @return MetaPost[]|null */
public function getPosts(): ?array
{
return $this->posts;
}
/**
* @param MetaPost[]|null $posts
*/
/** @param MetaPost[]|null $posts */
public function setPosts(?array $posts): void
{
$this->posts = $posts;
@ -45,10 +34,6 @@ class PostsPage implements ValidableInterface
public function isValid(): bool
{
if (null !== $this->posts) {
return true;
}
return false;
return null !== $this->posts;
}
}
}

View file

@ -1,81 +1,25 @@
<?php
declare(strict_types=1);
namespace src\PointToolsBundle\DTO\Api;
use src\PointToolsBundle\DTO\Api\ValidableInterface;
namespace App\DTO\Api;
/** TODO: Refactor to public readonly */
class User implements ValidableInterface
{
/**
* @var string|null
*/
private $id;
/**
* @var string|null
*/
private $login;
/**
* @var string|null
*/
private $name;
/**
* @var string|null
*/
private $about;
/**
* @var string|null
*/
private $xmpp;
/**
* @var string|null
*/
private $created;
/**
* @var bool|null
*/
private $gender;
/**
* @var bool|null
*/
private $denyAnonymous;
/**
* @var bool|null
*/
private $private;
/**
* @var string|null
*/
private $birthDate;
/**
* @var string|null
*/
private $homepage;
/**
* @var string|null
*/
private $email;
/**
* @var string|null
*/
private $location;
/**
* @var string|null
*/
private $error;
private ?string $id;
private ?string $login;
private ?string $name;
private ?string $about;
private ?string $xmpp;
private ?string $created;
private ?bool $gender;
private ?bool $denyAnonymous;
private ?bool $private;
private ?string $birthDate;
private ?string $homepage;
private ?string $email;
private ?string $location;
private ?string $error;
public function getId(): ?string
{
@ -219,10 +163,6 @@ class User implements ValidableInterface
public function isValid(): bool
{
if (null === $this->error && null !== $this->id && null !== $this->login) {
return true;
}
return false;
return null === $this->error && null !== $this->id && null !== $this->login;
}
}
}

View file

@ -1,7 +1,9 @@
<?php
namespace src\PointToolsBundle\DTO\Api;
declare(strict_types=1);
namespace App\DTO\Api;
interface ValidableInterface
{
public function isValid(): bool;
}
}

View file

@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace App\DTO;
/** Events count by day */
class DailyEventsDTO
{
public readonly \DateTime $date;
public function __construct(
string $date,
public readonly int $eventsCount,
) {
$this->date = new \DateTime($date);
}
}

14
src/DTO/TopUserDTO.php Normal file
View file

@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace App\DTO;
/** Data Transfer Object for top users list */
class TopUserDTO
{
public function __construct(
public readonly string $login,
public readonly int $subscribersCount,
) {
}
}

View file

@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace App\Factory\Telegram;
use App\Factory\AbstractFactory;
use Psr\Log\LoggerInterface;
use App\Entity\Telegram\Account;
use App\Repository\Telegram\AccountRepository;
use unreal4u\Telegram\Types\Message;
class AccountFactory extends AbstractFactory
{
public function __construct(
LoggerInterface $logger,
private readonly AccountRepository $accountRepository,
) {
parent::__construct($logger);
}
public function findOrCreateFromMessage(Message $message): Account
{
if (null === $account = $this->accountRepository->findOneBy(['id' => $message->from->id])) {
$account = new Account($message->from->id);
$this->accountRepository->save($account);
}
// Setting/updating account data
$account->updateFromMessageData(
$message->from->first_name,
$message->from->last_name,
$message->from->username,
$message->chat->id
);
return $account;
}
}