Completed
Branch new-addon-api (b7bde0)
by
unknown
18:29 queued 08:41
created

AddonApiVersion::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\addon\api;
4
5
use DomainException;
6
use EventEspresso\core\domain\DomainFactory;
7
use EventEspresso\core\domain\DomainInterface;
8
use EventEspresso\core\domain\values\FilePath;
9
use EventEspresso\core\domain\values\Version;
10
11
/**
12
 * Class AddonApiVersion
13
 * The minimum data required to bootstrap an EE add-on.
14
 * Additional data can be provided via specific API versions
15
 *
16
 * @author  Brent Christensen
17
 * @package EventEspresso\core\domain\entities\addon
18
 * @since   $VID:$
19
 */
20
abstract class AddonApiVersion
21
{
22
    const V1 = 1;
23
24
    /**
25
     * @var int one of the API_VERSION_* constants from above
26
     */
27
    private $api_version;
28
29
    /**
30
     * @var DomainInterface
31
     */
32
    private $domain;
33
34
    /**
35
     * @var Version minimum version of EE core that the add-on will work with
36
     */
37
    private $min_core_version;
38
39
    /**
40
     * @var Version minimum version of WP core that the add-on will work with
41
     */
42
    private $min_wp_version;
43
44
    /**
45
     * @var string  PascalCase identifier for the add-on.
46
     *              IMPORTANT! there must be a class of the same name in the root of the add-ons /src/domain/ folder
47
     */
48
    private $name;
49
50
    /**
51
     * @var string
52
     */
53
    private $addon_namespace;
54
55
    /**
56
     * @var FilePath
57
     */
58
    private $main_file;
59
60
    /**
61
     * @var string
62
     */
63
    private $slug;
64
65
    /**
66
     * @var Version the current add-on version
67
     */
68
    private $version;
69
70
71
    /**
72
     * Bootstrap constructor.
73
     *
74
     * @param string $slug
75
     * @param string $name
76
     * @param string $namespace
77
     * @param string $version
78
     * @param string $min_core_version
79
     * @param string $main_file
80
     * @param int    $api_version
81
     */
82
    protected function __construct(
83
        string $slug,
84
        string $name,
85
        string $namespace,
86
        string $version,
87
        string $min_core_version,
88
        string $main_file,
89
        int $api_version
90
    ) {
91
        $this->setSlug($slug);
92
        $this->setName($name);
93
        $this->setNamespace($namespace);
94
        $this->setMinCoreVersion($min_core_version);
95
        $this->setMainFile($main_file);
96
        $this->setVersion($version);
97
        $this->setApiVersion($api_version);
98
    }
99
100
101
    /**
102
     * @return void
103
     */
104
    public function initialize(): void
105
    {
106
        $this->domain = DomainFactory::create(
107
            "{$this->addon_namespace}\\domain\\Domain",
108
            $this->main_file,
109
            $this->version
110
        );
111
    }
112
113
114
    /**
115
     * @return int[]
116
     */
117
    private function validApiVersions(): array
118
    {
119
        return [
120
            AddonApiVersion::V1,
121
        ];
122
    }
123
124
125
    /**
126
     * @param int $api_version
127
     */
128
    private function setApiVersion(int $api_version): void
129
    {
130
        if (! in_array($api_version, $this->validApiVersions())) {
131
            throw new DomainException(
132
                esc_html__(
133
                    'Invalid Add-on API Version! Please use one of the EventEspresso\core\domain\entities\addon\Bootstrap class constants',
134
                    'event_espresso'
135
                )
136
            );
137
        }
138
        $this->api_version = $api_version;
139
    }
140
141
142
    /**
143
     * @param string $main_file
144
     */
145
    public function setMainFile(string $main_file): void
146
    {
147
        $this->main_file = new FilePath($main_file);
148
    }
149
150
151
    /**
152
     * @param string $min_core_version
153
     */
154
    private function setMinCoreVersion(string $min_core_version): void
155
    {
156
        $this->min_core_version = Version::fromString($min_core_version);
157
    }
158
159
160
    /**
161
     * @param string $name
162
     */
163
    public function setName(string $name): void
164
    {
165
        $this->name = $name;
166
    }
167
168
169
    /**
170
     * @param string $namespace
171
     */
172
    private function setNamespace(string $namespace): void
173
    {
174
        $this->addon_namespace = $namespace;
175
    }
176
177
178
    /**
179
     * @param string $slug
180
     */
181
    private function setSlug(string $slug): void
182
    {
183
        $valid_slug = sanitize_key($slug);
184
        if ($slug !== $valid_slug) {
185
            throw new DomainException(
186
                esc_html__(
187
                    'Invalid Add-on "slug"! Please ensure that slug only uses lowercase characters and dashes.',
188
                    'event_espresso'
189
                )
190
            );
191
        }
192
        $this->slug = $valid_slug;
193
    }
194
195
196
    /**
197
     * @param string $version
198
     */
199
    public function setVersion(string $version): void
200
    {
201
        $this->version = Version::fromString($version);
202
    }
203
204
205
    /**
206
     * @return int
207
     */
208
    public function apiVersion(): int
209
    {
210
        return $this->api_version;
211
    }
212
213
214
    /**
215
     * @return DomainInterface
216
     */
217
    public function domain(): DomainInterface
218
    {
219
        return $this->domain;
220
    }
221
222
223
    /**
224
     * @return Version|null
225
     */
226
    public function minCoreVersion(): ?Version
227
    {
228
        return $this->min_core_version;
229
    }
230
231
232
    /**
233
     * @return Version|null
234
     */
235
    public function minWpVersion(): ?Version
236
    {
237
        return $this->min_wp_version;
238
    }
239
240
241
    /**
242
     * @param string $min_wp_version
243
     */
244
    public function setMinWpVersion(string $min_wp_version = EE_MIN_WP_VER_REQUIRED): void
245
    {
246
        $this->min_wp_version = Version::fromString($min_wp_version);
247
    }
248
249
250
    /**
251
     * @return string
252
     */
253
    public function name(): string
254
    {
255
        return $this->name;
256
    }
257
258
259
    /**
260
     * FQCN for the domain's EE_Addon class
261
     *
262
     * @return string
263
     */
264
    public function fqcn(): string
265
    {
266
        return "{$this->addon_namespace}\\domain\\{$this->name}";
267
    }
268
269
270
    /**
271
     * @return string
272
     */
273
    public function getNamespace(): string
274
    {
275
        return $this->addon_namespace;
276
    }
277
278
279
    /**
280
     * @return FilePath
281
     */
282
    public function mainFile(): FilePath
283
    {
284
        return $this->main_file;
285
    }
286
287
288
    /**
289
     * @return string
290
     */
291
    public function slug(): string
292
    {
293
        return $this->slug;
294
    }
295
296
297
    /**
298
     * @return Version
299
     */
300
    public function version(): Version
301
    {
302
        return $this->version;
303
    }
304
}
305