Passed
Push — master ( 1145e4...3a5c01 )
by Alexey
03:43
created

AccountFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
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 37
ccs 0
cts 19
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 Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\EntityRepository;
7
use Skobkin\Bundle\PointToolsBundle\Entity\Telegram\Account;
8
use unreal4u\TelegramAPI\Telegram\Types\Message;
9
10
class AccountFactory
11
{
12
    /**
13
     * @var EntityManagerInterface
14
     */
15
    private $em;
16
17
    /**
18
     * @var EntityRepository
19
     */
20
    private $accountRepo;
21
22
23
    public function __construct(EntityManagerInterface $em)
24
    {
25
        $this->em = $em;
26
        $this->accountRepo = $em->getRepository('SkobkinPointToolsBundle:Telegram\Account');
27
    }
28
29
    public function findOrCreateFromMessage(Message $message): Account
30
    {
31
        if (null === $account = $this->accountRepo->findOneBy(['id' => $message->from->id])) {
32
            $account = new Account($message->from->id);
33
            $this->em->persist($account);
34
        }
35
36
        // Setting/updating account data
37
        $account
38
            ->setFirstName($message->from->first_name)
39
            ->setLastName($message->from->last_name)
40
            ->setUsername($message->from->username)
41
            ->setChatId($message->chat->id)
42
        ;
43
44
        return $account;
45
    }
46
}