Passed
Push — master ( 6c1980...4e24b5 )
by Alexey
03:34
created

UserFactory::createFromDTO()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 0
cts 13
cp 0
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\Service\Factory;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Doctrine\ORM\EntityRepository;
8
use Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler\User as UserDTO;
9
use Skobkin\Bundle\PointToolsBundle\Entity\User;
10
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\ApiException;
11
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\Factory\InvalidUserDataException;
12
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\InvalidResponseException;
13
14
15
class UserFactory
16
{
17
    /**
18
     * @var EntityManager
19
     */
20
    private $em;
21
22
    /**
23
     * @var EntityRepository
24
     */
25
    private $userRepository;
26
27
    /**
28
     * @param EntityManager $em
29
     */
30
    public function __construct(EntityManager $em)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $em. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
31
    {
32
        $this->em = $em;
33
        $this->userRepository = $em->getRepository('SkobkinPointToolsBundle:User');
34
    }
35
36
    /**
37
     * @param array $data
38
     *
39
     * @return User
40
     * @throws ApiException
41
     * @throws InvalidResponseException
42
     */
43
    public function createFromArray(array $data)
44
    {
45
        $this->validateArrayData($data);
46
47
        /** @var User $user */
48 View Code Duplication
        if (null === ($user = $this->userRepository->find($data['id']))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
            // Creating new user
50
            $user = new User($data['id']);
51
            $this->em->persist($user);
52
        }
53
54
        // Updating data
55
        $user
56
            ->setLogin($data['login'])
57
            ->setName($data['name'])
58
        ;
59
60
        return $user;
61
    }
62
63
    /**
64
     * @param UserDTO $userData
65
     *
66
     * @return User
67
     * @throws ApiException
68
     * @throws InvalidUserDataException
69
     */
70
    public function createFromDTO(UserDTO $userData)
71
    {
72
        $this->validateDTOData($userData);
73
74
        /** @var User $user */
75
        if (null === ($user = $this->userRepository->find($userData->getId()))) {
76
            // Creating new user
77
            $user = new User($userData->getId());
78
            $this->em->persist($user);
79
        }
80
81
        // Updating data
82
        $user
83
            ->setLogin($userData->getLogin())
84
            ->setName($userData->getName())
85
        ;
86
87
        return $user;
88
    }
89
90
    /**
91
     * @param array $data
92
     *
93
     * @throws InvalidResponseException
94
     */
95
    private function validateArrayData(array $data)
96
    {
97
        if (!(array_key_exists('id', $data) || !!array_key_exists('login', $data) || !array_key_exists('name', $data))) {
98
            throw new InvalidResponseException('Invalid user data');
99
        }
100
    }
101
102
    /**
103
     * @param array $data
104
     *
105
     * @throws InvalidResponseException
106
     */
107
    private function validateDTOData(UserDTO $data)
108
    {
109
        if (!$data->getId() || !$data->getLogin()) {
110
            throw new InvalidUserDataException('User have no id or login', $data);
111
        }
112
    }
113
}