Passed
Pull Request — master (#29)
by Alexander
11:45
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
21
declare(strict_types=1);
22
23
namespace Gnumoksha\FreeIpa;
24
25
use Gnumoksha\FreeIpa\Infra\Json\JsonException;
26
use Gnumoksha\FreeIpa\Infra\Rpc\ClientBuilder;
27
use Gnumoksha\FreeIpa\Infra\Rpc\PluginClientBuilder;
28
use Gnumoksha\FreeIpa\Infra\Rpc\Request\Body as RequestBodyInterface;
29
use Gnumoksha\FreeIpa\Infra\Rpc\Request\CommonBody as CommonRequestBody;
30
use Gnumoksha\FreeIpa\Infra\Rpc\Response\Body as ResponseBodyInterface;
31
use Gnumoksha\FreeIpa\Model\User\UserRepository;
32
use Gnumoksha\FreeIpa\Model\Group\GroupRepository;
33
use Psr\Http\Client\ClientExceptionInterface;
34
35
/**
36
 * Façade providing easy bootstrapping and convenient methods.
37
 */
38
class FreeIpa
39
{
40
    private Infra\Rpc\Client $client;
41
42
    private CommonRequestBody|RequestBodyInterface $requestBody;
43
44
    private ?UserRepository $userRepository = null;
45
46
    private ?GroupRepository $groupRepository = null;
47
48
    public function __construct(
49
        Options $options,
50
        ClientBuilder $clientBuilder = null,
51
        RequestBodyInterface $requestBody = null
52
    ) {
53
        $clientBuilder = $clientBuilder ?? new PluginClientBuilder($options);
54
55
        $this->client      = $clientBuilder->build();
56
        $this->requestBody = $requestBody ?? new CommonRequestBody();
57
    }
58
59
    /**
60
     * @throws ClientExceptionInterface
61
     */
62
    public function login(string $username, string $password): void
63
    {
64
        $this->client->login($username, $password);
65
    }
66
67
    public function getUserRepository(): UserRepository
68
    {
69
        if ($this->userRepository === null) {
70
            $this->userRepository = new UserRepository($this->client, $this->requestBody);
71
        }
72
73
        return $this->userRepository;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->userRepository could return the type null which is incompatible with the type-hinted return Gnumoksha\FreeIpa\Model\User\UserRepository. Consider adding an additional type-check to rule them out.
Loading history...
74
    }
75
76
    public function getGroupRepository(): GroupRepository
77
    {
78
        if ($this->groupRepository === null) {
79
            $this->groupRepository = new GroupRepository($this->client, $this->requestBody);
80
        }
81
82
        return $this->groupRepository;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->groupRepository could return the type null which is incompatible with the type-hinted return Gnumoksha\FreeIpa\Model\Group\GroupRepository. Consider adding an additional type-check to rule them out.
Loading history...
83
    }
84
85
    /**
86
     * Sends a raw request.
87
     *
88
     * @throws JsonException
89
     * @throws ClientExceptionInterface
90
     */
91
    public function sendRequest(RequestBodyInterface $body): ResponseBodyInterface
92
    {
93
        return $this->client->sendRequest($body);
94
    }
95
}
96