Passed
Push — master ( bd92b3...0f6b9d )
by
unknown
18:59
created

Extension::getDistributionImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Extensionmanager\Domain\Model;
17
18
use TYPO3\CMS\Core\Core\Environment;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
use TYPO3\CMS\Core\Utility\MathUtility;
21
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
22
23
/**
24
 * Main extension model
25
 * @internal This class is a specific domain model implementation and is not part of the Public TYPO3 API.
26
 */
27
class Extension extends AbstractEntity
28
{
29
    /**
30
     * Category index for distributions
31
     */
32
    const DISTRIBUTION_CATEGORY = 10;
33
34
    /**
35
     * Contains default categories.
36
     *
37
     * @var array
38
     */
39
    protected static $defaultCategories = [
40
        0 => 'be',
41
        1 => 'module',
42
        2 => 'fe',
43
        3 => 'plugin',
44
        4 => 'misc',
45
        5 => 'services',
46
        6 => 'templates',
47
        8 => 'doc',
48
        9 => 'example',
49
        self::DISTRIBUTION_CATEGORY => 'distribution'
50
    ];
51
52
    /**
53
     * Contains default states.
54
     *
55
     * @var array
56
     */
57
    protected static $defaultStates = [
58
        0 => 'alpha',
59
        1 => 'beta',
60
        2 => 'stable',
61
        3 => 'experimental',
62
        4 => 'test',
63
        5 => 'obsolete',
64
        6 => 'excludeFromUpdates',
65
        7 => 'deprecated',
66
        999 => 'n/a'
67
    ];
68
69
    /**
70
     * @var string
71
     */
72
    protected $extensionKey = '';
73
74
    /**
75
     * @var string
76
     */
77
    protected $version = '';
78
79
    /**
80
     * @var int
81
     */
82
    protected $integerVersion = 0;
83
84
    /**
85
     * @var string
86
     */
87
    protected $title = '';
88
89
    /**
90
     * @var string
91
     */
92
    protected $description = '';
93
94
    /**
95
     * @var int
96
     */
97
    protected $state = 0;
98
99
    /**
100
     * @var int
101
     */
102
    protected $category = 0;
103
104
    /**
105
     * @var \DateTime
106
     */
107
    protected $lastUpdated;
108
109
    /**
110
     * @var string
111
     */
112
    protected $updateComment = '';
113
114
    /**
115
     * @var string
116
     */
117
    protected $authorName = '';
118
119
    /**
120
     * @var string
121
     */
122
    protected $authorEmail = '';
123
124
    /**
125
     * @var bool
126
     */
127
    protected $currentVersion = false;
128
129
    /**
130
     * @var string
131
     */
132
    protected $md5hash = '';
133
134
    /**
135
     * @var int
136
     */
137
    protected $reviewState;
138
139
    /**
140
     * @var int
141
     */
142
    protected $alldownloadcounter;
143
144
    /**
145
     * @var string
146
     */
147
    protected $serializedDependencies = '';
148
149
    /**
150
     * @var \SplObjectStorage<Dependency>
151
     */
152
    protected $dependencies;
153
154
    /**
155
     * @var string
156
     */
157
    protected $documentationLink = '';
158
159
    /**
160
     * @var string
161
     */
162
    protected $distributionImage = '';
163
164
    /**
165
     * @var string
166
     */
167
    protected $remote;
168
169
    /**
170
     * @internal
171
     * @var int
172
     */
173
    protected $position = 0;
174
175
    /**
176
     * @param string $authorEmail
177
     */
178
    public function setAuthorEmail($authorEmail)
179
    {
180
        $this->authorEmail = $authorEmail;
181
    }
182
183
    /**
184
     * @return string
185
     */
186
    public function getAuthorEmail()
187
    {
188
        return $this->authorEmail;
189
    }
190
191
    /**
192
     * @param string $authorName
193
     */
194
    public function setAuthorName($authorName)
195
    {
196
        $this->authorName = $authorName;
197
    }
198
199
    /**
200
     * @return string
201
     */
202
    public function getAuthorName()
203
    {
204
        return $this->authorName;
205
    }
206
207
    /**
208
     * @param int $category
209
     */
210
    public function setCategory($category)
211
    {
212
        $this->category = $category;
213
    }
214
215
    /**
216
     * @return int
217
     */
218
    public function getCategory()
219
    {
220
        return $this->category;
221
    }
222
223
    /**
224
     * Get Category String
225
     *
226
     * @return string
227
     */
228
    public function getCategoryString()
229
    {
230
        $categoryString = '';
231
        if (isset(self::$defaultCategories[$this->getCategory()])) {
232
            $categoryString = self::$defaultCategories[$this->getCategory()];
233
        }
234
        return $categoryString;
235
    }
236
237
    /**
238
     * Returns category index from a given string or an integer.
239
     * Fallback to 4 - 'misc' in case string is not found or integer ist out of range.
240
     *
241
     * @param string|int $category Category string or integer
242
     * @return int Valid category index
243
     */
244
    public function getCategoryIndexFromStringOrNumber($category)
245
    {
246
        $categoryIndex = 4;
247
        if (MathUtility::canBeInterpretedAsInteger($category)) {
248
            $categoryIndex = (int)$category;
249
            if ($categoryIndex < 0 || $categoryIndex > 10) {
250
                $categoryIndex = 4;
251
            }
252
        } elseif (is_string($category)) {
253
            $categoryIndex = array_search($category, self::$defaultCategories);
254
            if ($categoryIndex === false) {
255
                $categoryIndex = 4;
256
            }
257
        }
258
        return $categoryIndex;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $categoryIndex also could return the type string which is incompatible with the documented return type integer.
Loading history...
259
    }
260
261
    /**
262
     * @param string $description
263
     */
264
    public function setDescription($description)
265
    {
266
        $this->description = $description;
267
    }
268
269
    /**
270
     * @return string
271
     */
272
    public function getDescription()
273
    {
274
        return $this->description;
275
    }
276
277
    /**
278
     * @param string $extensionKey
279
     */
280
    public function setExtensionKey($extensionKey)
281
    {
282
        $this->extensionKey = $extensionKey;
283
    }
284
285
    /**
286
     * @return string
287
     */
288
    public function getExtensionKey()
289
    {
290
        return $this->extensionKey;
291
    }
292
293
    /**
294
     * @param \DateTime $lastUpdated
295
     */
296
    public function setLastUpdated(\DateTime $lastUpdated)
297
    {
298
        $this->lastUpdated = $lastUpdated;
299
    }
300
301
    /**
302
     * @return \DateTime
303
     */
304
    public function getLastUpdated()
305
    {
306
        return $this->lastUpdated;
307
    }
308
309
    /**
310
     * @param int $state
311
     */
312
    public function setState($state)
313
    {
314
        $this->state = $state;
315
    }
316
317
    /**
318
     * @return int
319
     */
320
    public function getState()
321
    {
322
        return $this->state;
323
    }
324
325
    /**
326
     * Get State string
327
     *
328
     * @return string
329
     */
330
    public function getStateString()
331
    {
332
        $stateString = '';
333
        if (isset(self::$defaultStates[$this->getState()])) {
334
            $stateString = self::$defaultStates[$this->getState()];
335
        }
336
        return $stateString;
337
    }
338
339
    /**
340
     * Returns either array with all default states or index/title
341
     * of a state entry.
342
     *
343
     * @param mixed $state state title or state index
344
     * @return mixed
345
     */
346
    public function getDefaultState($state = null)
347
    {
348
        $defaultState = '';
349
        if ($state === null) {
350
            $defaultState = self::$defaultStates;
351
        } else {
352
            if (is_string($state)) {
353
                $stateIndex = array_search(strtolower($state), self::$defaultStates);
354
                if ($stateIndex === false) {
355
                    // default state
356
                    $stateIndex = 999;
357
                }
358
                $defaultState = $stateIndex;
359
            } else {
360
                if (is_int($state) && $state >= 0) {
361
                    if (array_key_exists($state, self::$defaultStates)) {
362
                        $stateTitle = self::$defaultStates[$state];
363
                    } else {
364
                        // default state
365
                        $stateTitle = 'n/a';
366
                    }
367
                    $defaultState = $stateTitle;
368
                }
369
            }
370
        }
371
        return $defaultState;
372
    }
373
374
    /**
375
     * @param string $title
376
     */
377
    public function setTitle($title)
378
    {
379
        $this->title = $title;
380
    }
381
382
    /**
383
     * @return string
384
     */
385
    public function getTitle()
386
    {
387
        return $this->title;
388
    }
389
390
    /**
391
     * @param string $updateComment
392
     */
393
    public function setUpdateComment($updateComment)
394
    {
395
        $this->updateComment = $updateComment;
396
    }
397
398
    /**
399
     * @return string
400
     */
401
    public function getUpdateComment()
402
    {
403
        return $this->updateComment;
404
    }
405
406
    /**
407
     * @param string $version
408
     */
409
    public function setVersion($version)
410
    {
411
        $this->version = $version;
412
    }
413
414
    /**
415
     * @return string
416
     */
417
    public function getVersion()
418
    {
419
        return $this->version;
420
    }
421
422
    /**
423
     * @param bool $currentVersion
424
     */
425
    public function setCurrentVersion($currentVersion)
426
    {
427
        $this->currentVersion = $currentVersion;
428
    }
429
430
    /**
431
     * @return bool
432
     */
433
    public function getCurrentVersion()
434
    {
435
        return $this->currentVersion;
436
    }
437
438
    /**
439
     * @param string $md5hash
440
     */
441
    public function setMd5hash($md5hash)
442
    {
443
        $this->md5hash = $md5hash;
444
    }
445
446
    /**
447
     * @return string
448
     */
449
    public function getMd5hash()
450
    {
451
        return $this->md5hash;
452
    }
453
454
    /**
455
     * Possible install paths
456
     *
457
     * @static
458
     * @return array
459
     */
460
    public static function returnInstallPaths()
461
    {
462
        $installPaths = [
463
            'System' => Environment::getFrameworkBasePath() . '/',
464
            'Global' => Environment::getBackendPath() . '/ext/',
465
            'Local' => Environment::getExtensionsPath() . '/'
466
        ];
467
        return $installPaths;
468
    }
469
470
    /**
471
     * Allowed install paths
472
     *
473
     * @static
474
     * @return array
475
     */
476
    public static function returnAllowedInstallPaths()
477
    {
478
        $installPaths = self::returnInstallPaths();
479
        if (empty($GLOBALS['TYPO3_CONF_VARS']['EXT']['allowGlobalInstall'])) {
480
            unset($installPaths['Global']);
481
        }
482
        if (empty($GLOBALS['TYPO3_CONF_VARS']['EXT']['allowLocalInstall'])) {
483
            unset($installPaths['Local']);
484
        }
485
        return $installPaths;
486
    }
487
488
    /**
489
     * Allowed install names: System, Global, Local
490
     *
491
     * @static
492
     * @return array
493
     */
494
    public static function returnAllowedInstallTypes()
495
    {
496
        $installPaths = self::returnAllowedInstallPaths();
497
        return array_keys($installPaths);
498
    }
499
500
    /**
501
     * @param string $dependencies
502
     */
503
    public function setSerializedDependencies($dependencies)
504
    {
505
        $this->serializedDependencies = $dependencies;
506
    }
507
508
    /**
509
     * @return string
510
     */
511
    public function getSerializedDependencies()
512
    {
513
        return $this->serializedDependencies;
514
    }
515
516
    /**
517
     * @param \SplObjectStorage<Dependency> $dependencies
518
     */
519
    public function setDependencies($dependencies)
520
    {
521
        $this->dependencies = $dependencies;
522
    }
523
524
    /**
525
     * @return \SplObjectStorage<Dependency>
526
     */
527
    public function getDependencies()
528
    {
529
        if (!is_object($this->dependencies)) {
530
            $this->setDependencies($this->convertDependenciesToObjects($this->getSerializedDependencies()));
531
        }
532
        return $this->dependencies;
533
    }
534
535
    public function getTypo3Dependency(): ?Dependency
536
    {
537
        foreach ($this->getDependencies() as $dependency) {
538
            if ($dependency->getIdentifier() === 'typo3') {
539
                return $dependency;
540
            }
541
        }
542
        return null;
543
    }
544
545
    /**
546
     * @param Dependency $dependency
547
     */
548
    public function addDependency(Dependency $dependency)
549
    {
550
        $this->dependencies->attach($dependency);
551
    }
552
553
    /**
554
     * @param int $integerVersion
555
     */
556
    public function setIntegerVersion($integerVersion)
557
    {
558
        $this->integerVersion = $integerVersion;
559
    }
560
561
    /**
562
     * @return int
563
     */
564
    public function getIntegerVersion()
565
    {
566
        return $this->integerVersion;
567
    }
568
569
    /**
570
     * @param int $reviewState
571
     */
572
    public function setReviewState($reviewState)
573
    {
574
        $this->reviewState = $reviewState;
575
    }
576
577
    /**
578
     * @return int
579
     */
580
    public function getReviewState()
581
    {
582
        return $this->reviewState;
583
    }
584
585
    /**
586
     * @param int $position
587
     */
588
    public function setPosition($position)
589
    {
590
        $this->position = $position;
591
    }
592
593
    /**
594
     * @return int
595
     */
596
    public function getPosition()
597
    {
598
        return $this->position;
599
    }
600
601
    /**
602
     * @param int $alldownloadcounter
603
     */
604
    public function setAlldownloadcounter($alldownloadcounter)
605
    {
606
        $this->alldownloadcounter = $alldownloadcounter;
607
    }
608
609
    /**
610
     * @return int
611
     */
612
    public function getAlldownloadcounter()
613
    {
614
        return $this->alldownloadcounter;
615
    }
616
617
    /**
618
     * @return string
619
     */
620
    public function getDocumentationLink(): string
621
    {
622
        return $this->documentationLink;
623
    }
624
625
    /**
626
     * @param string $documentationLink
627
     */
628
    public function setDocumentationLink(string $documentationLink): void
629
    {
630
        $this->documentationLink = $documentationLink;
631
    }
632
633
    public function getRemoteIdentifier(): string
634
    {
635
        return $this->remote;
636
    }
637
638
    /**
639
     * Map a legacy extension array to an object
640
     *
641
     * @param array $extensionArray
642
     * @return Extension
643
     */
644
    public static function createFromExtensionArray(array $extensionArray): self
645
    {
646
        /** @var Extension $extension */
647
        $extension = GeneralUtility::makeInstance(self::class);
648
        $extension->setExtensionKey($extensionArray['key']);
649
        if (isset($extensionArray['version'])) {
650
            $extension->setVersion($extensionArray['version']);
651
        }
652
        if (isset($extensionArray['remote'])) {
653
            $extension->remote = $extensionArray['remote'];
654
        }
655
        if (isset($extensionArray['constraints'])) {
656
            $extension->setDependencies($extension->convertDependenciesToObjects(is_array($extensionArray['constraints']) ? serialize($extensionArray['constraints']) : $extensionArray['constraints']));
657
        }
658
        return $extension;
659
    }
660
661
    /**
662
     * Converts string dependencies to an object storage of dependencies
663
     *
664
     * @param string $dependencies
665
     * @return \SplObjectStorage<Dependency>
666
     */
667
    protected function convertDependenciesToObjects(string $dependencies): \SplObjectStorage
668
    {
669
        $dependenciesObject = new \SplObjectStorage();
670
        $unserializedDependencies = unserialize($dependencies, ['allowed_classes' => false]);
671
        if (!is_array($unserializedDependencies)) {
672
            return $dependenciesObject;
673
        }
674
        foreach ($unserializedDependencies as $dependencyType => $dependencyValues) {
675
            // Dependencies might be given as empty string, e.g. conflicts => ''
676
            if (!is_array($dependencyValues)) {
677
                continue;
678
            }
679
            if (!$dependencyType) {
680
                continue;
681
            }
682
            foreach ($dependencyValues as $dependency => $versionConstraint) {
683
                if ($dependency) {
684
                    $dependencyObject = Dependency::createFromEmConf((string)$dependency, $versionConstraint, (string)$dependencyType);
685
                    $dependenciesObject->attach($dependencyObject);
686
                }
687
            }
688
        }
689
        return $dependenciesObject;
690
    }
691
692
    /**
693
     * @param string $distributionImage
694
     */
695
    public function setDistributionImage(string $distributionImage): void
696
    {
697
        $this->distributionImage = $distributionImage;
698
    }
699
700
    /**
701
     * @return string
702
     */
703
    public function getDistributionImage(): string
704
    {
705
        return $this->distributionImage;
706
    }
707
}
708