Passed
Push — master ( 28ec21...7fc19c )
by Toby
05:05 queued 02:27
created

UnionCloud::groups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * UnionCloud Wrapper Class
4
 */
5
6
namespace Twigger\UnionCloud\API;
7
8
use Twigger\UnionCloud\API\Auth\Authentication;
9
use Twigger\UnionCloud\API\Auth\IAuthenticator;
10
use Twigger\UnionCloud\API\Exception\Authentication\AuthenticatorNotFound;
11
use Twigger\UnionCloud\API\Exception\Authentication\BaseUnionCloudAuthenticationException;
12
use Twigger\UnionCloud\API\Request;
13
14
/**
15
 * Class UnionCloud
16
 *
17
 * Choose your resource from here!
18
 *
19
 * @package Twigger\UnionCloud\API\Core
20
 * @license    https://opensource.org/licenses/GPL-3.0  GNU Public License v3
21
 * @author     Toby Twigger <[email protected]>
22
 */
23
24
class UnionCloud
25
{
26
27
    /**
28
     * Holds the Authentication wrapper, a wrapper for the authenticator
29
     *
30
     * @var Authentication
31
     */
32
    protected $authentication = null;
33
34
    /**
35
     * Holds configuration variables
36
     *      - Base URL
37
     *      - Debug
38
     * @var Configuration
39
     */
40
    protected $configuration;
41
42
    /**
43
     * UnionCloud constructor.
44
     *
45
     * Creates an Authenticator and a blank Configuration
46
     *
47
     * @param null|array $authParams Associative array of the Authentication Parameters
48
     * @param null|string $authenticator AuthenticatorClass::class
49
     *
50
     * @throws AuthenticatorNotFound
51
     * @throws Exception\Authentication\AuthenticationParameterMissing
52
     */
53
    public function __construct($authParams = null, $authenticator = null)
54
    {
55
        $this->authentication = new Authentication($authParams, $authenticator);
56
        $this->configuration = new Configuration();
57
    }
58
59
    /**
60
     * Manually set the authenticator
61
     *
62
     * @param IAuthenticator $authenticator
63
     *
64
     * @throws BaseUnionCloudAuthenticationException
65
     */
66
    public function setAuthenticator($authenticator)
67
    {
68
        $this->authentication->setAuthenticator($authenticator);
69
    }
70
71
    /**
72
     * Check UnionCloud is ready for the request
73
     *
74
     * @throws AuthenticatorNotFound
75
     *
76
     * @return void
77
     */
78
    private function checkReadyForRequest()
79
    {
80
        if (!$this->authentication->hasAuthentication())
81
        {
82
            throw new AuthenticatorNotFound();
83
        }
84
        return;
85
    }
86
87
    /**
88
     * Set the Base URL in the configuration class
89
     *
90
     * @param string $baseURL
91
     */
92
    public function setBaseURL($baseURL)
93
    {
94
        $this->configuration->setBaseUrl($baseURL);
95
    }
96
97
    /**
98
     * Set the debug status of the API call.
99
     *
100
     * By calling debug(), you'll see a lot more details
101
     * about the API call.
102
     *
103
     * @param bool $debug Defaults to true
104
     */
105
    public function debug($debug = true)
106
    {
107
        $this->configuration->setDebug($debug);
108
    }
109
110
    /**
111
     * Return a user resource request.
112
     *
113
     * @return Request\UserRequest
114
     *
115
     * @throws AuthenticatorNotFound
116
     */
117
    public function users()
118
    {
119
        $this->checkReadyForRequest();
120
        return new Request\UserRequest($this->authentication, $this->configuration);
121
    }
122
123
    /**
124
     * Return a UserGroup Membership resource request.
125
     *
126
     * @return Request\UserGroupMembershipRequest
127
     *
128
     * @throws AuthenticatorNotFound
129
     */
130
    public function userGroupMemberships()
131
    {
132
        $this->checkReadyForRequest();
133
        return new Request\UserGroupMembershipRequest($this->authentication, $this->configuration);
134
    }
135
136
    /**
137
     * Return a Group resource request.
138
     *
139
     * @return Request\GroupRequest
140
     *
141
     * @throws AuthenticatorNotFound
142
     */
143
    public function groups()
144
    {
145
        $this->checkReadyForRequest();
146
        return new Request\GroupRequest($this->authentication, $this->configuration);
147
    }
148
149
}