Completed
Push — master ( fa35f0...64e350 )
by Gabriel
03:11
created

UserValidator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 3
dl 0
loc 29
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
B __invoke() 0 17 5
1
<?php
2
3
namespace Sinergi\Users\User;
4
5
use Interop\Container\ContainerInterface;
6
use Sinergi\Users\Container;
7
8
class UserValidator
9
{
10
    public function __construct(ContainerInterface $container)
11
    {
12
        if ($container instanceof Container) {
13
            $this->container = $container;
0 ignored issues
show
Bug introduced by
The property container does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
14
        } else {
15
            $this->container = new Container($container);
16
        }
17
    }
18
19
    public function __invoke(UserEntityInterface $user): array
20
    {
21
        $errors = [];
22
23
        /** @var UserRepositoryInterface $userRepository */
24
        $userRepository = $this->container->get(UserRepositoryInterface::class);
25
        $userExists = $userRepository->findByEmail($user->getEmail());
26
        if ($userExists && (!$user->getId() || ($user->getId() !== $userExists->getId()))) {
27
            $errors[1300] = 'Email is already in user';
28
        }
29
30
        if (empty($user->getPassword())) {
31
            $errors[1301] = 'Password is empty';
32
        }
33
34
        return $errors;
35
    }
36
}
37