Passed
Pull Request — master (#29)
by Alexander
11:45
created

GroupRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
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
21
declare(strict_types=1);
22
23
namespace Gnumoksha\FreeIpa\Model\Group;
24
25
use BadMethodCallException;
26
use Gnumoksha\FreeIpa\Infra\Json\JsonException;
27
use Gnumoksha\FreeIpa\Infra\Repository\BaseRepository;
28
use Gnumoksha\FreeIpa\Infra\Rpc\Client;
29
use Gnumoksha\FreeIpa\Infra\Rpc\Request\Body as RequestBodyInterface;
30
use Gnumoksha\FreeIpa\Infra\Rpc\Response\Body as ResponseBodyInterface;
31
use Psr\Http\Client\ClientExceptionInterface;
32
33
use function strlen;
34
35
class GroupRepository extends BaseRepository
36
{
37
    /** @var string */
38
    private const TOPIC = 'group';
39
40
    private Client $client;
41
42
    private RequestBodyInterface $body;
43
44
    public function __construct(Client $client, RequestBodyInterface $body)
45
    {
46
        $this->client = $client;
47
        $this->body   = $body;
48
    }
49
50
    /**
51
     * @TODO document string-only argument
52
     * @throws JsonException
53
     * @throws ClientExceptionInterface
54
     */
55
    public function find(array $arguments, array $options): ResponseBodyInterface
56
    {
57
        $defaultOptions = [
58
            'all'           => true,
59
            'private'       => false,
60
            'posix'         => false,
61
            'external'      => false,
62
            'nonposix'      => true,
63
            'no_members'    => true,
64
            'raw'           => false,
65
        ];
66
67
        $body = $this->body->withMethod(self::TOPIC . '_find')
68
            ->withArguments($arguments)
69
            ->withAddedOptions(array_merge($defaultOptions, $options));
70
71
        return $this->client->sendRequest($body);
72
    }
73
74
    /**
75
     * @throws JsonException
76
     * @throws ClientExceptionInterface
77
     *
78
     * @see \Gnumoksha\FreeIpa\Model\Group\GroupRepository::find() base method
79
     */
80
    public function findBy(string $field, string $value): ResponseBodyInterface
81
    {
82
        return $this->find([], [$field => $value]);
83
    }
84
85
    /**
86
     * @param string $group
87
     * @param string $uid
88
     * @return ResponseBodyInterface
89
     * @throws JsonException
90
     * @throws ClientExceptionInterface
91
     */
92
    public function addMember(string $group, string $uid): ResponseBodyInterface
93
    {
94
        $defaultOptions = [
95
            'all'           => false,
96
            'raw'           => false,
97
            'no_members'    => true,
98
        ];
99
100
        $body = $this->body->withMethod(self::TOPIC . '_add_member')
101
            ->withArguments([
102
                $group,
103
            ])
104
            ->withAddedOptions(array_merge($defaultOptions, [
105
                'user' => [$uid]
106
            ]));
107
108
        return $this->client->sendRequest($body);
109
    }
110
111
    public function __call(string $name, array $arguments): ResponseBodyInterface
112
    {
113
        if (strncmp($name, 'findBy', 6) === 0 && strlen($name) > 6) {
114
            $field = str_replace('findBy', '', $name);
115
            $field = strtolower($field); // Sn => sn
116
            // #TODO camelCase to snake_case em alguns casos (givenname excecao)
117
            return $this->findBy($field, $arguments[0]);
118
        }
119
120
        throw new BadMethodCallException(sprintf('Call to undefined method %s::%s', __CLASS__, $name));
121
    }
122
}
123