Passed
Pull Request — master (#28)
by Alexander
06:41
created

FreeIpa::getGroupRepository()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 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;
23
24
use Gnumoksha\FreeIpa\Infra\Rpc\ClientBuilder;
25
use Gnumoksha\FreeIpa\Infra\Rpc\PluginClientBuilder;
26
use Gnumoksha\FreeIpa\Infra\Rpc\Request\Body as RequestBodyInterface;
27
use Gnumoksha\FreeIpa\Infra\Rpc\Request\CommonBody as CommonRequestBody;
28
use Gnumoksha\FreeIpa\Infra\Rpc\Response\Body as ResponseBodyInterface;
29
use Gnumoksha\FreeIpa\Model\User\UserRepository;
30
use Gnumoksha\FreeIpa\Model\Group\GroupRepository;
31
32
/**
33
 * Façade providing easy bootstrapping and convenient methods.
34
 */
35
class FreeIpa
36
{
37
    /** @var \Gnumoksha\FreeIpa\Infra\Rpc\Client */
38
    private $client;
39
    /** @var \Gnumoksha\FreeIpa\Infra\Rpc\Request\Body */
40
    private $requestBody;
41
    /** @var \Gnumoksha\FreeIpa\Model\User\UserRepository|null */
42
    private $userRepository;
43
    /** @var \Gnumoksha\FreeIpa\Model\Group\GroupRepository|null */
44
    private $groupRepository;
45
46
    public function __construct(
47
        Options $options,
48
        ClientBuilder $clientBuilder = null,
49
        RequestBodyInterface $requestBody = null
50
    ) {
51
        $clientBuilder = $clientBuilder ?? new PluginClientBuilder($options);
52
53
        $this->client      = $clientBuilder->build();
54
        $this->requestBody = $requestBody ?? new CommonRequestBody();
55
    }
56
57
    /**
58
     * @throws \Psr\Http\Client\ClientExceptionInterface
59
     */
60
    public function login(string $username, string $password): void
61
    {
62
        $this->client->login($username, $password);
63
    }
64
65
    public function getUserRepository(): UserRepository
66
    {
67
        if ($this->userRepository === null) {
68
            $this->userRepository = new UserRepository($this->client, $this->requestBody);
69
        }
70
71
        return $this->userRepository;
72
    }
73
74
    public function getGroupRepository(): GroupRepository
75
    {
76
        if ($this->groupRepository === null) {
77
            $this->groupRepository = new GroupRepository($this->client, $this->requestBody);
78
        }
79
80
        return $this->groupRepository;
81
    }
82
83
    /**
84
     * Sends a raw request.
85
     *
86
     * @throws \Gnumoksha\FreeIpa\Infra\Json\JsonException
87
     * @throws \Psr\Http\Client\ClientExceptionInterface
88
     */
89
    public function sendRequest(RequestBodyInterface $body): ResponseBodyInterface
90
    {
91
        return $this->client->sendRequest($body);
92
    }
93
}
94