Completed
Push — master ( 7a55c4...e26fa9 )
by Cedric
03:29
created

Descriptor   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 253
Duplicated Lines 9.09 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 0
dl 23
loc 253
rs 10
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 10 1
A addModule() 0 6 1
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
        '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
        $this->validateScopeValue($scope);
78
79
        if (false !== $this->getScopeKey($scope)) {
80
            return $this;
81
        }
82
83
        $this->descriptor['scopes'][] = $scope;
84
        return $this;
85
    }
86
87
    /**
88
     * @param string $scope
89
     *
90
     * @return bool
91
     */
92
    private function validateScopeValue($scope)
93
    {
94
        switch ($scope) {
95
            case self::SCOPE_ACT_AS_USER:
96
            case self::SCOPE_ADMIN:
97
            case self::SCOPE_NONE:
98
            case self::SCOPE_READ:
99
            case self::SCOPE_WRITE:
100
            case self::SCOPE_DELETE:
101
            case self::SCOPE_JIRA_PROJECT_ADMIN:
102
            case self::SCOPE_CONFLUENCE_SPACE_ADMIN:
103
                return true;
104
        }
105
        throw new \InvalidArgumentException(sprintf("Unknown scope %s", $scope));
106
    }
107
108
    /**
109
     * @param $scope
110
     *
111
     * @return mixed
112
     */
113
    private function getScopeKey($scope)
114
    {
115
        $key = array_search($scope, $this->descriptor['scopes']);
116
        return $key;
117
    }
118
119
    /**
120
     * @param string $scope
121
     *
122
     * @return $this
123
     * @throws \Exception
124
     */
125 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...
126
    {
127
        $this->validateScopeValue($scope);
128
129
        $key = $this->getScopeKey($scope);
130
        if (false === $key) {
131
            return $this;
132
        }
133
134
        unset($this->descriptor['scopes'][$key]);
135
        return $this;
136
    }
137
138
    /**
139
     * @return array
140
     */
141
    public function getArray()
142
    {
143
        return $this->descriptor;
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    public function getJson()
150
    {
151
        return json_encode($this->descriptor);
152
    }
153
154
    /**
155
     * @return $this
156
     */
157
    public function enableLicensing()
158
    {
159
        $this->descriptor['enableLicensing'] = true;
160
        return $this;
161
    }
162
163
    /**
164
     * @return $this
165
     */
166
    public function disableLicensing()
167
    {
168
        $this->descriptor['enableLicensing'] = false;
169
        return $this;
170
    }
171
172
    /**
173
     * @param int $version
174
     *
175
     * @return $this
176
     */
177
    public function setApiVersion($version)
178
    {
179
        $this->descriptor['apiVersion'] = $version;
180
        return $this;
181
    }
182
183
    /**
184
     * @param string $name
185
     * @param string $url
186
     *
187
     * @return $this
188
     */
189
    public function addLink($name, $url)
190
    {
191
        $this->descriptor['links'][$name] = $url;
192
        return $this;
193
    }
194
195
    /**
196
     * @param string $name
197
     *
198
     * @return $this
199
     */
200
    public function removeLink($name)
201
    {
202
        unset($this->descriptor['links'][$name]);
203
        return $this;
204
    }
205
206
    /**
207
     * @param string $name
208
     * @param string $url
209
     *
210
     * @return $this
211
     */
212
    public function setVendor($name, $url = '')
213
    {
214
        $this->descriptor['vendor'] = [
215
            'name' => $name,
216
            'url'  => $url
217
        ];
218
        return $this;
219
    }
220
221
    /**
222
     * @param string $installed
223
     * @param string $enabled
224
     * @param string $disabled
225
     * @param string $uninstalled
226
     *
227
     * @return $this
228
     */
229
    public function setLifecycleWebhooks($installed, $enabled, $disabled = '', $uninstalled = '')
230
    {
231
        $this->descriptor['lifecycle'] = [
232
            "installed"   => $installed,
233
            "uninstalled" => $uninstalled,
234
            "enabled"     => $enabled,
235
            "disabled"    => $disabled
236
        ];
237
        return $this;
238
    }
239
240
    /**
241
     * @param string $name
242
     * @param array  $description
243
     *
244
     * @return $this
245
     */
246
    public function addModule($name, array $description)
247
    {
248
        $this->descriptor['modules'][$name] = $description;
249
250
        return $this;
251
    }
252
253
    /**
254
     * @param string $name
255
     *
256
     * @return $this
257
     */
258
    public function removeModule($name)
259
    {
260
        unset($this->descriptor['modules'][$name]);
261
        return $this;
262
    }
263
}
264