Passed
Push — 1.0 ( 764456...b8c16e )
by Artem
03:09
created

Descriptor::withModules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace AtlassianConnectCore;
4
5
/**
6
 * Class Descriptor
7
 *
8
 * @package AtlassianConnectCore
9
 */
10
class Descriptor
11
{
12
    /**
13
     * @var array
14
     */
15
    private $contents = [];
16
17
    /**
18
     * Descriptor constructor.
19
     *
20
     * @param array $contents
21
     */
22
    public function __construct(array $contents = [])
23
    {
24
        $this->contents = (!$contents ? $this->defaultContents() : $contents);
25
    }
26
27
    /**
28
     * Returns the descriptor contents
29
     *
30
     * @return array
31
     */
32
    public function contents()
33
    {
34
        return $this->contents;
35
    }
36
37
    /**
38
     * Overwrite contents
39
     *
40
     * @param array $contents
41
     *
42
     * @return $this
43
     */
44
    public function overwrite(array $contents)
45
    {
46
        $this->contents = $contents;
47
48
        return $this;
49
    }
50
51
    /**
52
     * Set contents value using dot notation
53
     *
54
     * @param string $key
55
     * @param string|array $value
56
     *
57
     * @return $this
58
     */
59
    public function set($key, $value)
60
    {
61
        array_set($this->contents, $key, $value);
62
63
        return $this;
64
    }
65
66
    /**
67
     * Get value from contents using dot notation
68
     *
69
     * @param string $key
70
     * @param string|array $default
71
     *
72
     * @return array|string|null
73
     */
74
    public function get($key, $default = null)
75
    {
76
        return array_get($this->contents, $key, $default);
77
    }
78
79
    /**
80
     * Merge descriptor contents
81
     *
82
     * @param array $contents
83
     *
84
     * @return $this
85
     */
86
    public function merge(array $contents)
87
    {
88
        $this->contents = array_merge($this->contents, $contents);
89
90
        return $this;
91
    }
92
93
    /**
94
     * Modify contents using callback function
95
     *
96
     * @param callable $callback
97
     *
98
     * @return $this
99
     */
100
    public function modify(callable $callback)
101
    {
102
        $result = $callback($this->contents);
103
104
        $this->contents = (is_array($result) ? $result : $this->contents);
105
106
        return $this;
107
    }
108
109
    /**
110
     * The helper method to use fluent interface
111
     *
112
     * @return $this
113
     */
114
    public function fluent()
115
    {
116
        return $this;
117
    }
118
119
    /**
120
     * Set specific modules
121
     *
122
     * @param array $modules
123
     *
124
     * @return $this
125
     */
126
    public function withModules(array $modules)
127
    {
128
        $this->set('modules', $modules);
129
130
        return $this;
131
    }
132
133
    /**
134
     * Remove modules
135
     *
136
     * @return $this
137
     */
138
    public function withoutModules()
139
    {
140
        $this->set('modules', []);
141
142
        return $this;
143
    }
144
145
    /**
146
     * Set scopes
147
     *
148
     * @param array $scopes
149
     *
150
     * @return $this
151
     */
152
    public function setScopes(array $scopes)
153
    {
154
        $this->set('scopes', $scopes);
155
156
        return $this;
157
    }
158
159
    /**
160
     * Set base contents
161
     *
162
     * @return $this
163
     */
164
    public function base()
165
    {
166
        $this->contents = array_only($this->defaultContents(), [
167
            'name',
168
            'description',
169
            'key',
170
            'baseUrl',
171
            'vendor',
172
            'version',
173
            'authentication',
174
            'lifecycle'
175
        ]);
176
177
        return $this;
178
    }
179
180
    /**
181
     * Default descriptor contents
182
     *
183
     * @return array
184
     */
185
    private function defaultContents()
186
    {
187
        return [
188
            'name' => config('plugin.name'),
189
            'description' => config('plugin.description'),
190
            'key' => config('plugin.key'),
191
            'baseUrl' => config('plugin.url'),
192
            'vendor' => [
193
                'name' => config('plugin.vendor.name'),
194
                'url' => config('plugin.vendor.url'),
195
            ],
196
            'version' => config('plugin.version'),
197
            'authentication' => [
198
                'type' => config('plugin.authType')
199
            ],
200
            'lifecycle' => [
201
                'installed' => route('installed', [], false),
202
                'uninstalled' => route('uninstalled', [], false),
203
                'enabled' => route('enabled', [], false),
204
                'disabled' => route('disabled', [], false)
205
            ],
206
            'scopes' => [
207
                'ADMIN',
208
                'ACT_AS_USER'
209
            ],
210
            'modules' => [
211
                'generalPages' => [
212
                    [
213
                        'key' => 'hello-page',
214
                        'url' => '/hello',
215
                        'name' => [
216
                            'value' => 'Your add-on'
217
                        ],
218
                        'icon' => [
219
                            'width' => 20,
220
                            'height' => 20,
221
                            'url' => asset('vendor/plugin/package.png'),
222
                        ]
223
                    ],
224
                ]
225
            ]
226
        ];
227
    }
228
}