Completed
Push — master ( 9488c4...585d40 )
by Derek Stephen
08:30
created

User   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 60%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 19
c 2
b 0
f 0
lcom 1
cbo 7
dl 0
loc 95
ccs 27
cts 45
cp 0.6
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B registerUser() 0 26 6
A __construct() 0 5 1
B createFromArray() 0 12 7
A toArray() 0 13 3
A saveUser() 0 4 1
A getRepository() 0 4 1
1
<?php
2
3
namespace Del\Service;
4
5
use DateTime;
6
use Del\Criteria\UserCriteria;
7
use Del\Entity\Person;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Del\Service\Person.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use Del\Entity\User as UserEntity;
9
use Del\Exception\UserException;
10
use Del\Repository\User as UserRepository;
11
use Del\Service\Person as PersonService;
12
use Del\Value\User\State;
13
use Doctrine\ORM\EntityManager;
14
use InvalidArgumentException;
15
16
class User
17
{
18
    /** @var EntityManager $em */
19
    protected $em;
20
21
    /** @var  PersonService */
22
    private $personSvc;
23
24 3
    public function __construct(EntityManager $em, PersonService $personSvc)
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...
25
    {
26 3
        $this->em = $em;
27 3
        $this->personSvc = $personSvc;
28 3
    }
29
30
   /** 
31
    * @param array $data
32
    * @return UserEntity
33
    */
34 3
    public function createFromArray(array $data)
35
    {
36 3
        $user = new UserEntity();
37 3
        $user->setPerson(new Person());
38 3
        isset($data['id']) ? $user->setId($data['id']) : null;
39 3
        isset($data['email']) ? $user->setEmail($data['email']) : null;
40 3
        isset($data['password']) ? $user->setPassword($data['password']) : null;
41 3
        isset($data['state']) ? $user->setState(new State($data['state'])) : null;
42 3
        isset($data['registrationDate']) ? $user->setRegistrationDate(new DateTime($data['registrationDate'])) : null;
43 3
        isset($data['lastLogin']) ? $user->setLastLogin(new DateTime($data['lastLogin'])) : null;
44 3
        return $user;
45
    }
46
47
48
49
50
    /**
51
     * @return array
52
     */
53 1
    public function toArray(UserEntity $user)
54
    {
55
        return array
56
        (
57 1
            'id' => $user->getID(),
58 1
            'email' => $user->getEmail(),
59 1
            'person' => $user->getPerson(),
60 1
            'password' => $user->getPassword(),
61 1
            'state' => $user->getState()->getValue(),
62 1
            'registrationDate' => is_null($user->getRegistrationDate()) ? null : $user->getRegistrationDate()->format('Y-m-d H:i:s'),
63 1
            'lastLoginDate' => is_null($user->getLastLoginDate()) ? null : $user->getLastLoginDate()->format('Y-m-d H:i:s'),
64 1
        );
65
    }
66
67
    /**
68
     * @param UserEntity $user
69
     * @return UserEntity
70
     */
71 1
    public function saveUser(UserEntity $user)
72
    {
73 1
        return $this->getRepository()->save($user);
74
    }
75
76
   /**
77
    * @return UserRepository
78
    */
79 1
    protected function getRepository()
80
    {
81 1
        return $this->em->getRepository('Del\Entity\User');
82
    }
83
84
    public function registerUser(array $data)
85
    {
86
        if (!$data['email'] || !$data['password'] || !$data['confirm']) {
87
            throw new InvalidArgumentException();
88
        }
89
        if ($data['password'] !== $data['confirm']) {
90
            throw new UserException(UserException::WRONG_PASSWORD);
91
        }
92
93
        $criteria = new UserCriteria();
94
        $criteria->setEmail($data['email']);
95
        $user = $this->getRepository()->findByCriteria($criteria);
96
        if(!is_null($user)) {
97
            throw new UserException(UserException::USER_EXISTS);
98
        }
99
100
        $person = new Person();
101
        $user = new UserEntity();
102
        $state = new State(State::STATE_UNACTIVATED);
103
        $user->setPerson($person)
104
             ->setEmail($data['email'])
105
             ->setRegistrationDate(new DateTime())
106
             ->setState($state);
107
108
109
    }
110
}
111