Completed
Push — master ( 5c4d20...2b3e4b )
by
unknown
12s
created

BaseUserRepositoryCollection::all()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\User;
4
5
use SumoCoders\FrameworkMultiUserBundle\Exception\NoRepositoriesRegisteredException;
6
use SumoCoders\FrameworkMultiUserBundle\Exception\RepositoryNotRegisteredException;
7
use SumoCoders\FrameworkMultiUserBundle\Exception\UserNotFound;
8
use SumoCoders\FrameworkMultiUserBundle\Security\PasswordResetToken;
9
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\User;
10
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\UserRepository;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, SumoCoders\FrameworkMult...dle\User\UserRepository.

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...
11
12
class BaseUserRepositoryCollection
13
{
14
    /** @var UserRepository[] */
15
    private $userRepositories = [];
16
17
    /**
18
     * @param UserRepository[] $userRepositories
19
     */
20
    public function __construct(array $userRepositories)
21
    {
22
        foreach ($userRepositories as $repository) {
23
            $this->addUserRepository($repository);
24
        }
25
    }
26
27
    /**
28
     * Registers the UserRepository to the BaseUserRepositoryCollection.
29
     *
30
     * @param UserRepository $userRepository
31
     */
32
    public function addUserRepository(UserRepository $userRepository)
33
    {
34
        $this->userRepositories[] = $userRepository;
35
    }
36
37
    /**
38
     * Get the userRepositories.
39
     *
40
     * @throws NoRepositoriesRegisteredException
41
     *
42
     * @return UserRepository[]
43
     */
44
    public function all()
45
    {
46
        if (count($this->userRepositories) === 0) {
47
            throw new NoRepositoriesRegisteredException('No user repositories registered');
48
        }
49
50
        return $this->userRepositories;
51
    }
52
53
    /**
54
     * Find the UserRepository for a given BaseUser Class.
55
     *
56
     * @param string $className
57
     *
58
     * @throws RepositoryNotRegisteredException
59
     *
60
     * @return UserRepository
61
     */
62
    public function findRepositoryByClassName($className)
63
    {
64
        foreach ($this->userRepositories as $repository) {
65
            if ($repository->supportsClass($className)) {
66
                return $repository;
67
            }
68
        }
69
70
        throw RepositoryNotRegisteredException::withClassName($className);
71
    }
72
73
    /**
74
     * Check if the BaseUserRepositoryCollection supports a BaseUser class.
75
     *
76
     * @param string $className
77
     *
78
     * @return bool
79
     */
80
    public function supportsClass($className)
81
    {
82
        foreach ($this->userRepositories as $repository) {
83
            if ($repository->supportsClass($className)) {
84
                return true;
85
            }
86
        }
87
88
        return false;
89
    }
90
91
    /**
92
     * @param PasswordResetToken $token
93
     *
94
     * @throws UserNotFound
95
     *
96
     * @return User
97
     */
98
    public function findUserByToken(PasswordResetToken $token)
99
    {
100
        foreach ($this->userRepositories as $repository) {
101
            $user = $repository->findByPasswordResetToken($token);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $user is correct as $repository->findByPasswordResetToken($token) (which targets SumoCoders\FrameworkMult...dByPasswordResetToken()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
102
103
            if ($user) {
104
                return $user;
105
            }
106
        }
107
108
        throw UserNotFound::withToken($token);
109
    }
110
111
    /**
112
     * @param string $username
113
     *
114
     * @throws UserNotFound
115
     *
116
     * @return User
117
     */
118
    public function findUserByUserName($username)
119
    {
120
        foreach ($this->userRepositories as $repository) {
121
            $user = $repository->findByUsername($username);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $user is correct as $repository->findByUsername($username) (which targets SumoCoders\FrameworkMult...itory::findByUsername()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
122
123
            if ($user) {
124
                return $user;
125
            }
126
        }
127
128
        throw UserNotFound::withUsername($username);
129
    }
130
}
131