Completed
Push — master ( bbb6c4...cfbe4b )
by Tobias
03:51 queued 02:30
created

UserRepository::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
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
use function strlen;
30
31
/**
32
 * @method ResponseBodyInterface findByGivenName($value) first name
33
 * @method ResponseBodyInterface findBySn($value) last name
34
 * @method ResponseBodyInterface findByCn($value) full name
35
 * @method ResponseBodyInterface findByInGroup($value)
36
 * @method ResponseBodyInterface findByNotInGroup($value)
37
 * @method ResponseBodyInterface findByMail($value)
38
 * @method ResponseBodyInterface findByUid($value) unique name
39
 * @method ResponseBodyInterface findByUidNumber($value) unique number
40
 */
41
class UserRepository extends BaseRepository
42
{
43
    /** @var string */
44
    private const TOPIC = 'user';
45
46
    /** @var \Gnumoksha\FreeIpa\Infra\Rpc\Client */
47
    private $client;
48
    /** @var \Gnumoksha\FreeIpa\Infra\Rpc\Request\Body */
49
    private $body;
50
51
    public function __construct(Client $client, RequestBodyInterface $body)
52
    {
53
        $this->client = $client;
54
        $this->body   = $body;
55
    }
56
57
    /**
58
     * @param object|array $user
59
     */
60
    public function add($user, array $arguments = [], array $options = []): ResponseBodyInterface
61
    {
62
        $defaultOptions = [
63
            'all' => false,
64
            'no_members' => false,
65
            'noprivate' => false,
66
            'random' => false,
67
            'raw' => false,
68
        ];
69
70
        $arguments = array_merge([$user['uid']], $arguments);
71
        unset($user['uid']);
72
73
        $body = $this->body->withMethod(self::TOPIC . '_add')
74
                           ->withArguments($arguments)
75
                           ->withAddedOptions(array_merge($defaultOptions, $user, $options));
76
77
        return $this->client->sendRequest($body);
78
    }
79
80
    public function show(array $arguments, array $options = []): ResponseBodyInterface
81
    {
82
        $defaultOptions = [
83
            'all' => true,
84
            'no_members' => false,
85
            'raw' => false,
86
            'rights' => false,
87
        ];
88
89
        $body = $this->body->withMethod(self::TOPIC . '_show')
90
                           ->withArguments($arguments)
91
                           ->withAddedOptions(array_merge($defaultOptions, $options));
92
93
        return $this->client->sendRequest($body);
94
    }
95
96
    /**
97
     * @TODO document string-only argument
98
     * @throws \Gnumoksha\FreeIpa\Infra\Json\JsonException
99
     * @throws \Psr\Http\Client\ClientExceptionInterface
100
     */
101
    public function find(array $arguments, array $options): ResponseBodyInterface
102
    {
103
        $defaultOptions = [
104
            'all'        => true,
105
            'no_members' => false,
106
            'pkey_only'  => false,
107
            'raw'        => false,
108
            'whoami'     => false,
109
        ];
110
111
        $body = $this->body->withMethod(self::TOPIC . '_find')
112
                           ->withArguments($arguments)
113
                           ->withAddedOptions(array_merge($defaultOptions, $options));
114
115
        return $this->client->sendRequest($body);
116
    }
117
118
    /**
119
     * @throws \Gnumoksha\FreeIpa\Infra\Json\JsonException
120
     * @throws \Psr\Http\Client\ClientExceptionInterface
121
     *
122
     * @see \Gnumoksha\FreeIpa\Model\User\UserRepository::find() base method
123
     */
124
    public function findBy(string $field, $value): ResponseBodyInterface
125
    {
126
        return $this->find([], [$field => $value]);
127
    }
128
129
    public function mod(string $uid, array $newData, array $arguments = [], array $options = []): ResponseBodyInterface
130
    {
131
        $defaultOptions = [
132
            'all' => false,
133
            'no_members' => false,
134
            'random' => false,
135
            'raw' => false,
136
            'rights' => false,
137
        ];
138
139
        $arguments = array_merge([$uid], $arguments);
140
141
        $body = $this->body->withMethod(self::TOPIC . '_mod')
142
                           ->withArguments($arguments)
143
                           ->withAddedOptions(array_merge($defaultOptions, $newData, $options));
144
145
        return $this->client->sendRequest($body);
146
    }
147
148
    public function del(array $arguments, array $options = []): ResponseBodyInterface
149
    {
150
        $defaultOptions = [
151
        ];
152
153
        $body = $this->body->withMethod(self::TOPIC . '_del')
154
                           ->withArguments($arguments)
155
                           ->withAddedOptions(array_merge($defaultOptions, $options));
156
157
        return $this->client->sendRequest($body);
158
    }
159
160
    public function __call($name, $arguments)
161
    {
162
        if (strncmp($name, 'findBy', 6) === 0 && strlen($name) > 6) {
163
            $field = str_replace('findBy', '', $name);
164
            $field = strtolower($field); // Sn => sn
165
            // #TODO camelCase to snake_case em alguns casos (givenname excecao)
166
            return $this->findBy($field, $arguments[0]);
167
        }
168
169
        throw new BadMethodCallException(sprintf('Call to undefined method %s::%s', __CLASS__, $name));
170
    }
171
}
172