Test Setup Failed
Push — master ( cb9c22...4c02e1 )
by Alexey
02:54
created

UserFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\Service\Factory;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler\User as UserDTO;
7
use Skobkin\Bundle\PointToolsBundle\Entity\User;
8
use Skobkin\Bundle\PointToolsBundle\Repository\UserRepository;
9
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\ApiException;
10
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\Factory\InvalidUserDataException;
11
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\InvalidResponseException;
12
13
class UserFactory
14
{
15
    /**
16
     * @var UserRepository
17
     */
18
    private $userRepository;
19
20
    /**
21
     * @param EntityManagerInterface $em
0 ignored issues
show
Bug introduced by
There is no parameter named $em. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
22
     */
23
    public function __construct(UserRepository $userRepository)
24
    {
25
        $this->userRepository = $userRepository;
26
    }
27
28
    /**
29
     * @param array $data
30
     *
31
     * @return User
32
     *
33
     * @throws InvalidResponseException
34
     */
35
    public function createFromArray(array $data): User
36
    {
37
        $this->validateArrayData($data);
38
39
        /** @var User $user */
40
        if (null === ($user = $this->userRepository->find($data['id']))) {
41
            // Creating new user
42
            $user = new User($data['id']);
43
            $this->userRepository->add($user);
44
        }
45
46
        // Updating data
47
        $user
48
            ->setLogin($data['login'])
49
            ->setName($data['name'])
50
        ;
51
52
        return $user;
53
    }
54
55
    /**
56
     * @param UserDTO $userData
57
     *
58
     * @return User
59
     *
60
     * @throws ApiException
61
     * @throws InvalidUserDataException
62
     */
63
    public function createFromDTO(UserDTO $userData): User
64
    {
65
        $this->validateDTOData($userData);
66
67
        /** @var User $user */
68
        if (null === ($user = $this->userRepository->find($userData->getId()))) {
69
            // Creating new user
70
            $user = new User($userData->getId());
71
            $this->userRepository->add($user);
72
        }
73
74
        // Updating data
75
        $user
76
            ->setLogin($userData->getLogin())
77
            ->setName($userData->getName())
78
        ;
79
80
        return $user;
81
    }
82
83
    /**
84
     * @param array $data
85
     *
86
     * @throws InvalidResponseException
87
     */
88
    private function validateArrayData(array $data)
89
    {
90
        if (!array_key_exists('id', $data) || !array_key_exists('login', $data) || !array_key_exists('name', $data) || !is_numeric($data['id'])) {
91
            throw new InvalidResponseException('Invalid user data');
92
        }
93
    }
94
95
    /**
96
     * @param UserDTO $data
97
     *
98
     * @throws InvalidResponseException
99
     */
100
    private function validateDTOData(UserDTO $data)
101
    {
102
        if (!$data->getId() || !$data->getLogin()) {
103
            throw new InvalidUserDataException('User have no id or login', $data);
104
        }
105
    }
106
}