Descriptor   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 262
Duplicated Lines 8.78 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 31
lcom 1
cbo 0
dl 23
loc 262
rs 9.8
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setName() 0 5 1
A setDescription() 0 5 1
A addScope() 11 11 2
B validateScopeValue() 0 15 9
A getScopeKey() 0 5 1
A removeScope() 12 12 2
A getArray() 0 4 1
A getJson() 0 4 1
A enableLicensing() 0 5 1
A disableLicensing() 0 5 1
A setApiVersion() 0 5 1
A addLink() 0 5 1
A removeLink() 0 5 1
A setVendor() 0 8 1
A setLifecycleWebhooks() 0 17 3
A addModule() 0 9 2
A removeModule() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
        'scopes'         => []
31
    ];
32
33
34
    /**
35
     * Descriptor constructor.
36
     *
37
     * @param string $baseUrl
38
     * @param string $key
39
     */
40
    public function __construct($baseUrl, $key)
41
    {
42
        $this->descriptor['baseUrl'] = $baseUrl;
43
        $this->descriptor['key'] = $key;
44
    }
45
46
    /**
47
     * @param string $name
48
     *
49
     * @return $this
50
     */
51
    public function setName($name)
52
    {
53
        $this->descriptor["name"] = $name;
54
        return $this;
55
    }
56
57
    /**
58
     * @param string $description
59
     *
60
     * @return $this
61
     */
62
    public function setDescription($description)
63
    {
64
        $this->descriptor['description'] = $description;
65
        return $this;
66
    }
67
68
    /**
69
     * @param string $scope
70
     *
71
     * @return $this
72
     * @throws \Exception
73
     */
74 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...
75
    {
76
        $this->validateScopeValue($scope);
77
78
        if (false !== $this->getScopeKey($scope)) {
79
            return $this;
80
        }
81
82
        $this->descriptor['scopes'][] = $scope;
83
        return $this;
84
    }
85
86
    /**
87
     * @param string $scope
88
     *
89
     * @return bool
90
     */
91
    private function validateScopeValue($scope)
92
    {
93
        switch ($scope) {
94
            case self::SCOPE_ACT_AS_USER:
95
            case self::SCOPE_ADMIN:
96
            case self::SCOPE_NONE:
97
            case self::SCOPE_READ:
98
            case self::SCOPE_WRITE:
99
            case self::SCOPE_DELETE:
100
            case self::SCOPE_JIRA_PROJECT_ADMIN:
101
            case self::SCOPE_CONFLUENCE_SPACE_ADMIN:
102
                return true;
103
        }
104
        throw new \InvalidArgumentException(sprintf("Unknown scope %s", $scope));
105
    }
106
107
    /**
108
     * @param $scope
109
     *
110
     * @return mixed
111
     */
112
    private function getScopeKey($scope)
113
    {
114
        $key = array_search($scope, $this->descriptor['scopes']);
115
        return $key;
116
    }
117
118
    /**
119
     * @param string $scope
120
     *
121
     * @return $this
122
     * @throws \Exception
123
     */
124 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...
125
    {
126
        $this->validateScopeValue($scope);
127
128
        $key = $this->getScopeKey($scope);
129
        if (false === $key) {
130
            return $this;
131
        }
132
133
        unset($this->descriptor['scopes'][$key]);
134
        return $this;
135
    }
136
137
    /**
138
     * @return array
139
     */
140
    public function getArray()
141
    {
142
        return $this->descriptor;
143
    }
144
145
    /**
146
     * @return string
147
     */
148
    public function getJson()
149
    {
150
        return json_encode($this->descriptor);
151
    }
152
153
    /**
154
     * @return $this
155
     */
156
    public function enableLicensing()
157
    {
158
        $this->descriptor['enableLicensing'] = true;
159
        return $this;
160
    }
161
162
    /**
163
     * @return $this
164
     */
165
    public function disableLicensing()
166
    {
167
        $this->descriptor['enableLicensing'] = false;
168
        return $this;
169
    }
170
171
    /**
172
     * @param int $version
173
     *
174
     * @return $this
175
     */
176
    public function setApiVersion($version)
177
    {
178
        $this->descriptor['apiVersion'] = $version;
179
        return $this;
180
    }
181
182
    /**
183
     * @param string $name
184
     * @param string $url
185
     *
186
     * @return $this
187
     */
188
    public function addLink($name, $url)
189
    {
190
        $this->descriptor['links'][$name] = $url;
191
        return $this;
192
    }
193
194
    /**
195
     * @param string $name
196
     *
197
     * @return $this
198
     */
199
    public function removeLink($name)
200
    {
201
        unset($this->descriptor['links'][$name]);
202
        return $this;
203
    }
204
205
    /**
206
     * @param string $name
207
     * @param string $url
208
     *
209
     * @return $this
210
     */
211
    public function setVendor($name, $url = '')
212
    {
213
        $this->descriptor['vendor'] = [
214
            'name' => $name,
215
            'url'  => $url
216
        ];
217
        return $this;
218
    }
219
220
    /**
221
     * @param string $installed
222
     * @param string $enabled
223
     * @param string $disabled
224
     * @param string $uninstalled
225
     *
226
     * @return $this
227
     */
228
    public function setLifecycleWebhooks($installed, $enabled, $disabled = null, $uninstalled = null)
229
    {
230
        $this->descriptor['lifecycle'] = [
231
            'installed' => $installed,
232
            'enabled'   => $enabled,
233
        ];
234
235
        if (null !== $disabled) {
236
            $this->descriptor['lifecycle']['disabled'] = $disabled;
237
        }
238
239
        if (null !== $uninstalled) {
240
            $this->descriptor['lifecycle']['uninstalled'] = $uninstalled;
241
        }
242
243
        return $this;
244
    }
245
246
    /**
247
     * @param string $name
248
     * @param array  $description
249
     *
250
     * @return $this
251
     */
252
    public function addModule($name, array $description)
253
    {
254
        if (!isset($this->descriptor['modules'])) {
255
            $this->descriptor['modules'] = [];
256
        }
257
        $this->descriptor['modules'][$name] = $description;
258
259
        return $this;
260
    }
261
262
    /**
263
     * @param string $name
264
     *
265
     * @return $this
266
     */
267
    public function removeModule($name)
268
    {
269
        unset($this->descriptor['modules'][$name]);
270
        return $this;
271
    }
272
}
273