Completed
Pull Request — master (#14)
by
unknown
02:30
created

UserRepositoryCollection::supportsClass()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 3
eloc 4
nc 3
nop 1
1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\User;
4
5
use SumoCoders\FrameworkMultiUserBundle\Exception\NoRepositoriesRegisteredException;
6
use SumoCoders\FrameworkMultiUserBundle\Exception\RepositoryNotRegisteredException;
7
8
class UserRepositoryCollection
9
{
10
    /**
11
     * @var array
12
     */
13
    private $userRepositories = [];
14
15
    /**
16
     * UserRepositoryCollection constructor.
17
     *
18
     * @param array $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 UserRepositoryCollection.
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 array
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 User Class.
55
     *
56
     * @param $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 UserRepositoryCollection supports a User class.
75
     *
76
     * @param $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
89
    /**
90
     * @param $token
91
     *
92
     * @return UserInterface|null
93
     */
94
    public function findUserByToken($token)
95
    {
96
        foreach ($this->userRepositories as $repository) {
97
            $user = $repository->findByPasswordResetToken($token);
98
99
            if ($user) {
100
                return $user;
101
            }
102
        }
103
104
        return;
105
    }
106
107
    /**
108
     * @param $username
109
     */
110
    public function findUserByUserName($username)
111
    {
112
        foreach ($this->userRepositories as $repository) {
113
            $user = $repository->findByUsername($username);
114
115
            if ($user) {
116
                return $user;
117
            }
118
        }
119
120
        return;
121
    }
122
}
123