Completed
Push — master ( 7eae42...8f150d )
by Thiago
10:26
created

User   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4
Metric Value
wmc 9
lcom 2
cbo 4
dl 0
loc 99
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A save() 0 20 3
A delete() 0 9 1
A findByUserId() 0 4 1
A listAll() 0 6 1
A create() 0 6 1
A update() 0 7 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace User\Service;
5
6
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
7
use Doctrine\ORM\EntityManagerInterface;
8
use User\Factory\User as UserFactory;
9
use User\Entity\User as UserModel;
10
use Exception;
11
use InvalidArgumentException;
12
13
/**
14
 * User Service
15
 *
16
 * @author Thiago Paes <[email protected]>
17
 * @author Romeu Mattos <[email protected]>
18
 */
19
final class User implements UserInterface
20
{
21
    /**
22
     * @var \User\Repository\UserInterface
23
     */
24
    private $users;
25
26
    /**
27
     * @var EntityManagerInterface
28
     */
29
    private $em;
30
31
    /**
32
     * User Service Constructor.
33
     *
34
     * @param EntityManagerInterface $em
35
     */
36
    public function __construct(EntityManagerInterface $em)
37
    {
38
        $this->em       = $em;
39
        $this->users    = $em->getRepository(UserModel::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $em->getRepository(\User\Entity\User::class) of type object<Doctrine\Common\P...tence\ObjectRepository> is incompatible with the declared type object<User\Repository\UserInterface> of property $users.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function save(UserModel $user): UserModel
46
    {
47
        $this->em->beginTransaction();
48
49
        try {
50
            $this->em->persist($user);
51
            $this->em->flush();
52
            $this->em->commit();
53
54
            return $user;
55
        } catch (UniqueConstraintViolationException $ex) {
56
            $this->em->rollBack();
57
58
            throw new InvalidArgumentException('Email is already registered', 409, $ex);
59
        } catch (Exception $ex) {
60
            $this->em->rollBack();
61
62
            throw new InvalidArgumentException($ex->getMessage(), 500, $ex);
63
        }
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69
    public function delete(UserModel $user): UserModel
70
    {
71
        $user->delete();
72
73
        $this->em->persist($user);
74
        $this->em->flush();
75
76
        return $user;
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82
    public function findByUserId(int $id): UserModel
83
    {
84
        return $this->users->findById($id);
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90
    public function listAll(): array
91
    {
92
        $users = $this->users->findAll();
93
94
        return $users;
95
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100
    public function create(string $name, string $email): UserModel
101
    {
102
        $user = UserFactory::create($name, $email);
103
104
        return $this->save($user);
105
    }
106
107
    /**
108
     * @inheritdoc
109
     */
110
    public function update(UserModel $user, string $name, string $email): UserModel
111
    {
112
        /* @var $update \User\Entity\User */
113
        $update = UserFactory::update($user, $name, $email);
114
115
        return $this->save($update);
116
    }
117
}
118