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