AccountFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 6
dl 0
loc 30
ccs 0
cts 15
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A findOrCreateFromMessage() 0 17 2
1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\Service\Factory\Telegram;
4
5
use Psr\Log\LoggerInterface;
6
use Skobkin\Bundle\PointToolsBundle\Entity\Telegram\Account;
7
use Skobkin\Bundle\PointToolsBundle\Repository\Telegram\AccountRepository;
8
use Skobkin\Bundle\PointToolsBundle\Service\Factory\AbstractFactory;
9
use unreal4u\TelegramAPI\Telegram\Types\Message;
10
11
class AccountFactory extends AbstractFactory
12
{
13
    /** @var AccountRepository */
14
    private $accountRepo;
15
16
17
    public function __construct(LoggerInterface $logger, AccountRepository $accountRepository)
18
    {
19
        parent::__construct($logger);
20
        $this->accountRepo = $accountRepository;
21
    }
22
23
    public function findOrCreateFromMessage(Message $message): Account
24
    {
25
        if (null === $account = $this->accountRepo->findOneBy(['id' => $message->from->id])) {
26
            $account = new Account($message->from->id);
27
            $this->accountRepo->add($account);
28
        }
29
30
        // Setting/updating account data
31
        $account->updateFromMessageData(
32
            $message->from->first_name,
33
            $message->from->last_name,
34
            $message->from->username,
35
            $message->chat->id
36
        );
37
38
        return $account;
39
    }
40
}