Completed
Push — master ( 0f2117...344d8b )
by Daniel
02:08
created

UserService::find()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace DanielPieper\MergeReminder\Service;
4
5
use DanielPieper\MergeReminder\Exception\UserNotFoundException;
6
use DanielPieper\MergeReminder\ValueObject\User;
7
8
class UserService
9
{
10
    /** @var \Gitlab\Client */
11
    private $gitlabClient;
12
13 8
    public function __construct(\Gitlab\Client $gitlabClient)
14
    {
15 8
        $this->gitlabClient = $gitlabClient;
16 8
    }
17
18
    /**
19
     * @param int $id
20
     * @return User|null
21
     */
22 2
    public function find(int $id): ?User
23
    {
24 2
        $user = $this->gitlabClient->users()->show($id);
25 2
        if (!is_array($user)) {
26 1
            return null;
27
        }
28 1
        return User::fromArray($user);
29
    }
30
31
    /**
32
     * @param string $username
33
     * @return User
34
     */
35 2
    public function findByName(string $username): ?User
36
    {
37 2
        $users = $this->all($username);
38 2
        if (!is_array($users) || count($users) == 0) {
0 ignored issues
show
introduced by
The condition is_array($users) is always true.
Loading history...
39 1
            return null;
40
        }
41
42 1
        return array_shift($users);
43
    }
44
45
    /**
46
     * @return User
47
     * @throws UserNotFoundException
48
     */
49 2
    public function getAuthenticated(): User
50
    {
51 2
        $user = $this->gitlabClient->users()->user();
52 2
        if (!is_array($user)) {
53 1
            throw new UserNotFoundException();
54
        }
55 1
        return User::fromArray($user);
56
    }
57
58
    /**
59
     * @param int $id
60
     * @return User
61
     * @throws UserNotFoundException
62
     */
63 2
    public function get(int $id): User
64
    {
65 2
        $user = $this->find($id);
66 2
        if (!$user) {
67 1
            throw new UserNotFoundException();
68
        }
69 1
        return $user;
70
    }
71
72
    /**
73
     * @param string $username
74
     * @return User
75
     * @throws UserNotFoundException
76
     */
77 2
    public function getByName(string $username): User
78
    {
79 2
        $user = $this->findByName($username);
80 2
        if (!$user) {
81 1
            throw new UserNotFoundException();
82
        }
83 1
        return $user;
84
    }
85
86
    /**
87
     * @param string|null $username
88
     * @return array
89
     */
90 4
    public function all(string $username = null): array
91
    {
92 4
        $parameters = ['active' => true, 'blocked' => false];
93 4
        if ($username) {
94 2
            $parameters['username'] = $username;
95
        }
96 4
        $users = $this->gitlabClient->users()->all($parameters);
97 4
        if (!is_array($users)) {
98 2
            return [];
99
        }
100
101
        return array_map(function ($user) {
102 2
            return User::fromArray($user);
103 2
        }, $users);
104
    }
105
}
106