UserRepository   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 60
c 1
b 0
f 0
dl 0
loc 133
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A del() 0 10 1
A __call() 0 10 3
A findBy() 0 3 1
A show() 0 14 1
A add() 0 22 2
A mod() 0 17 1
A find() 0 15 1
1
<?php
2
3
/**
4
 * FreeIPA library for PHP
5
 * Copyright (C) 2015-2019 Tobias Sette <[email protected]>
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Lesser General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
declare(strict_types=1);
21
22
namespace Gnumoksha\FreeIpa\Model\User;
23
24
use BadMethodCallException;
25
use Gnumoksha\FreeIpa\Infra\Repository\BaseRepository;
26
use Gnumoksha\FreeIpa\Infra\Rpc\Client;
27
use Gnumoksha\FreeIpa\Infra\Rpc\Request\Body as RequestBodyInterface;
28
use Gnumoksha\FreeIpa\Infra\Rpc\Response\Body as ResponseBodyInterface;
29
30
use function strlen;
31
32
/**
33
 * @method ResponseBodyInterface findByGivenName($value) first name
34
 * @method ResponseBodyInterface findBySn($value) last name
35
 * @method ResponseBodyInterface findByCn($value) full name
36
 * @method ResponseBodyInterface findByInGroup($value)
37
 * @method ResponseBodyInterface findByNotInGroup($value)
38
 * @method ResponseBodyInterface findByMail($value)
39
 * @method ResponseBodyInterface findByUid($value) unique name
40
 * @method ResponseBodyInterface findByUidNumber($value) unique number
41
 */
42
class UserRepository extends BaseRepository
43
{
44
    /** @var string */
45
    private const TOPIC = 'user';
46
47
    /** @var \Gnumoksha\FreeIpa\Infra\Rpc\Client */
48
    private $client;
49
    /** @var \Gnumoksha\FreeIpa\Infra\Rpc\Request\Body */
50
    private $body;
51
52
    public function __construct(Client $client, RequestBodyInterface $body)
53
    {
54
        $this->client = $client;
55
        $this->body   = $body;
56
    }
57
58
    /**
59
     * @param object|array $user
60
     */
61
    public function add($user, array $arguments = [], array $options = []): ResponseBodyInterface
62
    {
63
        if (\is_object($user)) {
64
            $user = (array)$user;
65
        }
66
67
        $defaultOptions = [
68
            'all'        => false,
69
            'no_members' => false,
70
            'noprivate'  => false,
71
            'random'     => false,
72
            'raw'        => false,
73
        ];
74
75
        $arguments = array_merge([$user['uid']], $arguments);
76
        unset($user['uid']);
77
78
        $body = $this->body->withMethod(self::TOPIC . '_add')
79
                           ->withArguments($arguments)
80
                           ->withAddedOptions(array_merge($defaultOptions, $user, $options));
81
82
        return $this->client->sendRequest($body);
83
    }
84
85
    public function show(array $arguments, array $options = []): ResponseBodyInterface
86
    {
87
        $defaultOptions = [
88
            'all'        => true,
89
            'no_members' => false,
90
            'raw'        => false,
91
            'rights'     => false,
92
        ];
93
94
        $body = $this->body->withMethod(self::TOPIC . '_show')
95
                           ->withArguments($arguments)
96
                           ->withAddedOptions(array_merge($defaultOptions, $options));
97
98
        return $this->client->sendRequest($body);
99
    }
100
101
    /**
102
     * @TODO document string-only argument
103
     * @throws \Gnumoksha\FreeIpa\Infra\Json\JsonException
104
     * @throws \Psr\Http\Client\ClientExceptionInterface
105
     */
106
    public function find(array $arguments, array $options): ResponseBodyInterface
107
    {
108
        $defaultOptions = [
109
            'all'        => true,
110
            'no_members' => false,
111
            'pkey_only'  => false,
112
            'raw'        => false,
113
            'whoami'     => false,
114
        ];
115
116
        $body = $this->body->withMethod(self::TOPIC . '_find')
117
                           ->withArguments($arguments)
118
                           ->withAddedOptions(array_merge($defaultOptions, $options));
119
120
        return $this->client->sendRequest($body);
121
    }
122
123
    /**
124
     * @throws \Gnumoksha\FreeIpa\Infra\Json\JsonException
125
     * @throws \Psr\Http\Client\ClientExceptionInterface
126
     *
127
     * @see \Gnumoksha\FreeIpa\Model\User\UserRepository::find() base method
128
     */
129
    public function findBy(string $field, $value): ResponseBodyInterface
130
    {
131
        return $this->find([], [$field => $value]);
132
    }
133
134
    public function mod(string $uid, array $newData, array $arguments = [], array $options = []): ResponseBodyInterface
135
    {
136
        $defaultOptions = [
137
            'all'        => false,
138
            'no_members' => false,
139
            'random'     => false,
140
            'raw'        => false,
141
            'rights'     => false,
142
        ];
143
144
        $arguments = array_merge([$uid], $arguments);
145
146
        $body = $this->body->withMethod(self::TOPIC . '_mod')
147
                           ->withArguments($arguments)
148
                           ->withAddedOptions(array_merge($defaultOptions, $newData, $options));
149
150
        return $this->client->sendRequest($body);
151
    }
152
153
    public function del(array $arguments, array $options = []): ResponseBodyInterface
154
    {
155
        $defaultOptions = [
156
        ];
157
158
        $body = $this->body->withMethod(self::TOPIC . '_del')
159
                           ->withArguments($arguments)
160
                           ->withAddedOptions(array_merge($defaultOptions, $options));
161
162
        return $this->client->sendRequest($body);
163
    }
164
165
    public function __call($name, $arguments)
166
    {
167
        if (strncmp($name, 'findBy', 6) === 0 && strlen($name) > 6) {
168
            $field = str_replace('findBy', '', $name);
169
            $field = strtolower($field); // Sn => sn
170
            // #TODO camelCase to snake_case em alguns casos (givenname excecao)
171
            return $this->findBy($field, $arguments[0]);
172
        }
173
174
        throw new BadMethodCallException(sprintf('Call to undefined method %s::%s', __CLASS__, $name));
175
    }
176
}
177