Completed
Push — master ( d6c4ff...7a55c4 )
by Cedric
02:46
created

Descriptor::validateScopeValue()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 7.756
c 0
b 0
f 0
cc 9
eloc 12
nc 9
nop 1
1
<?php
2
/**
3
 * This file is part of the adlogix/guzzle-atlassian-connect-middleware package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Adlogix\GuzzleAtlassianConnect\Entity;
10
11
class Descriptor
12
{
13
    const SCOPE_NONE = "none";
14
    const SCOPE_READ = "read";
15
    const SCOPE_WRITE = "write";
16
    const SCOPE_DELETE = "delete";
17
    const SCOPE_ADMIN = "admin";
18
    const SCOPE_ACT_AS_USER = "act_a_user";
19
20
    const SCOPE_JIRA_PROJECT_ADMIN = "project_admin";
21
    const SCOPE_CONFLUENCE_SPACE_ADMIN = "space_admin";
22
23
    /**
24
     * @var array
25
     */
26
    private $descriptor = [
27
        'authentication' => [
28
            'type' => 'jwt'
29
        ],
30
        'modules'        => [],
31
        'scopes'         => []
32
    ];
33
34
35
    /**
36
     * Descriptor constructor.
37
     *
38
     * @param string $baseUrl
39
     * @param string $key
40
     */
41
    public function __construct($baseUrl, $key)
42
    {
43
        $this->descriptor['baseUrl'] = $baseUrl;
44
        $this->descriptor['key'] = $key;
45
    }
46
47
    /**
48
     * @param string $name
49
     *
50
     * @return $this
51
     */
52
    public function setName($name)
53
    {
54
        $this->descriptor["name"] = $name;
55
        return $this;
56
    }
57
58
    /**
59
     * @param string $description
60
     *
61
     * @return $this
62
     */
63
    public function setDescription($description)
64
    {
65
        $this->descriptor['description'] = $description;
66
        return $this;
67
    }
68
69
    /**
70
     * @param string $scope
71
     *
72
     * @return $this
73
     * @throws \Exception
74
     */
75 View Code Duplication
    public function addScope($scope)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        if (!$this->validateScopeValue($scope)) {
78
            throw new \InvalidArgumentException(sprintf("Unknown scope %s", $scope));
79
        }
80
81
        if (in_array($scope, $this->descriptor['scopes'])) {
82
            return $this;
83
        }
84
85
        $this->descriptor['scopes'][] = $scope;
86
        return $this;
87
    }
88
89
    /**
90
     * @param string $scope
91
     *
92
     * @return bool
93
     */
94
    private function validateScopeValue($scope)
95
    {
96
        switch ($scope) {
97
            case self::SCOPE_ACT_AS_USER:
98
            case self::SCOPE_ADMIN:
99
            case self::SCOPE_NONE:
100
            case self::SCOPE_READ:
101
            case self::SCOPE_WRITE:
102
            case self::SCOPE_DELETE:
103
            case self::SCOPE_JIRA_PROJECT_ADMIN:
104
            case self::SCOPE_CONFLUENCE_SPACE_ADMIN:
105
                return true;
106
        }
107
        return false;
108
    }
109
110
    /**
111
     * @param string $scope
112
     *
113
     * @return $this
114
     * @throws \Exception
115
     */
116 View Code Duplication
    public function removeScope($scope)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
    {
118
        if (!$this->validateScopeValue($scope)) {
119
            throw new \InvalidArgumentException(sprintf("Unknown scope %s", $scope));
120
        }
121
122
        $key = array_search($scope, $this->descriptor['scopes']);
123
        if (false === $key) {
124
            return $this;
125
        }
126
127
        unset($this->descriptor['scopes'][$key]);
128
        return $this;
129
    }
130
131
    /**
132
     * @return array
133
     */
134
    public function getArray()
135
    {
136
        return $this->descriptor;
137
    }
138
139
    /**
140
     * @return string
141
     */
142
    public function getJson()
143
    {
144
        return json_encode($this->descriptor);
145
    }
146
147
    /**
148
     * @return $this
149
     */
150
    public function enableLicensing()
151
    {
152
        $this->descriptor['enableLicensing'] = true;
153
        return $this;
154
    }
155
156
    /**
157
     * @return $this
158
     */
159
    public function disableLicensing()
160
    {
161
        $this->descriptor['enableLicensing'] = false;
162
        return $this;
163
    }
164
165
    /**
166
     * @param int $version
167
     *
168
     * @return $this
169
     */
170
    public function setApiVersion($version)
171
    {
172
        $this->descriptor['apiVersion'] = $version;
173
        return $this;
174
    }
175
176
    /**
177
     * @param string $name
178
     * @param string $url
179
     *
180
     * @return $this
181
     */
182
    public function addLink($name, $url)
183
    {
184
        $this->descriptor['links'][$name] = $url;
185
        return $this;
186
    }
187
188
    /**
189
     * @param string $name
190
     *
191
     * @return $this
192
     */
193
    public function removeLink($name)
194
    {
195
        unset($this->descriptor['links'][$name]);
196
        return $this;
197
    }
198
199
    /**
200
     * @param string $name
201
     * @param string $url
202
     *
203
     * @return $this
204
     */
205
    public function setVendor($name, $url = '')
206
    {
207
        $this->descriptor['vendor'] = [
208
            'name' => $name,
209
            'url'  => $url
210
        ];
211
        return $this;
212
    }
213
214
    /**
215
     * @param string $installed
216
     * @param string $enabled
217
     * @param string $disabled
218
     * @param string $uninstalled
219
     *
220
     * @return $this
221
     */
222
    public function setLifecycleWebhooks($installed, $enabled, $disabled = '', $uninstalled = '')
223
    {
224
        $this->descriptor['lifecycle'] = [
225
            "installed"   => $installed,
226
            "uninstalled" => $uninstalled,
227
            "enabled"     => $enabled,
228
            "disabled"    => $disabled
229
        ];
230
        return $this;
231
    }
232
233
234
    /**
235
     * @param string $name
236
     * @param array  $description
237
     *
238
     * @return $this
239
     */
240
    public function addModule($name, array $description)
241
    {
242
        $this->descriptor['modules'][$name] = $description;
243
244
        return $this;
245
    }
246
247
    /**
248
     * @param string $name
249
     * @param array  $description
0 ignored issues
show
Bug introduced by
There is no parameter named $description. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
250
     *
251
     * @return $this
252
     */
253
    public function removeModule($name)
254
    {
255
        unset($this->descriptor['modules'][$name]);
256
        return $this;
257
    }
258
}
259