Passed
Push — master ( fcbfb0...64542d )
by
unknown
13:36
created

Dependency   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 131
rs 10
c 0
b 0
f 0
wmc 18

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getLowestVersion() 0 3 1
A getType() 0 3 1
A getIdentifier() 0 3 1
A getHighestVersionAsInteger() 0 6 2
A isVersionCompatible() 0 9 5
A getLowestVersionAsInteger() 0 6 2
A getHighestVersion() 0 3 1
A createFromEmConf() 0 20 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Extensionmanager\Domain\Model;
19
20
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
21
use TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException;
22
23
/**
24
 * Value Object of a single dependency of an extension
25
 * @internal This class is a specific domain model implementation and is not part of the Public TYPO3 API.
26
 */
27
class Dependency
28
{
29
    /**
30
     * @var string
31
     */
32
    protected $identifier = '';
33
34
    /**
35
     * @var string
36
     */
37
    protected $lowestVersion = '';
38
39
    /**
40
     * @var string
41
     */
42
    protected $highestVersion = '';
43
44
    /**
45
     * @var string
46
     */
47
    protected $type = '';
48
49
    /**
50
     * @var array
51
     */
52
    protected static $dependencyTypes = [
53
        'depends',
54
        'conflicts',
55
        'suggests'
56
    ];
57
58
    /**
59
     * @var array
60
     */
61
    public static $specialDependencies = [
62
        'typo3',
63
        'php'
64
    ];
65
66
    private function __construct(string $identifier, string $type, string $lowestVersion, string $highestVersion)
67
    {
68
        $this->identifier = $identifier;
69
        $this->type = $type;
70
        $this->lowestVersion = $lowestVersion;
71
        $this->highestVersion = $highestVersion;
72
    }
73
74
    /**
75
     * Use this factory when building dependencies of an extension, like ["depends"]["news"] => '1.0.0-2.6.9'
76
     *
77
     * @param string $identifier the extension name or "typo3" or "php" for TYPO3 Core / PHP version constraints
78
     * @param string $versionConstraint the actual version number. "1.0.0-2.0.0" or "1.0.0" which means "1.0.0 or higher"
79
     * @param string $dependencyType use "depends", "suggests" or "conflicts".
80
     * @return static
81
     * @throws ExtensionManagerException
82
     */
83
    public static function createFromEmConf(
84
        string $identifier,
85
        string $versionConstraint = '',
86
        string $dependencyType = 'depends'
87
    ): self {
88
        $versionNumbers = VersionNumberUtility::convertVersionsStringToVersionNumbers($versionConstraint);
89
        $lowest = $versionNumbers[0];
90
        if (count($versionNumbers) === 2) {
91
            $highest = $versionNumbers[1];
92
        } else {
93
            $highest = '';
94
        }
95
        if (!in_array($dependencyType, self::$dependencyTypes, true)) {
96
            throw new ExtensionManagerException($dependencyType . ' was not a valid dependency type.', 1476122402);
97
        }
98
        // dynamically migrate 'cms' dependency to 'core' dependency
99
        // see also \TYPO3\CMS\Core\Package\Package::getPackageMetaData
100
        $identifier = strtolower($identifier);
101
        $identifier = $identifier === 'cms' ? 'core' : $identifier;
102
        return new self($identifier, $dependencyType, (string)$lowest, (string)$highest);
103
    }
104
105
    public function getHighestVersion(): string
106
    {
107
        return $this->highestVersion;
108
    }
109
110
    public function getIdentifier(): string
111
    {
112
        return $this->identifier;
113
    }
114
115
    public function getLowestVersion(): string
116
    {
117
        return $this->lowestVersion;
118
    }
119
120
    public function getType(): string
121
    {
122
        return $this->type;
123
    }
124
125
    /**
126
     * Compares the given version number against the lowest and highest
127
     * possible version number of this dependency (e.g. "typo3") to
128
     * determine if the given version is "compatible".
129
     *
130
     * @param string $version
131
     * @return bool TRUE if the version number is compatible
132
     */
133
    public function isVersionCompatible(string $version): bool
134
    {
135
        if ($this->lowestVersion !== '' && version_compare($version, $this->lowestVersion) === -1) {
136
            return false;
137
        }
138
        if ($this->highestVersion !== '' && version_compare($this->highestVersion, $version) === -1) {
139
            return false;
140
        }
141
        return true;
142
    }
143
144
    public function getLowestVersionAsInteger(): int
145
    {
146
        if ($this->lowestVersion) {
147
            return VersionNumberUtility::convertVersionNumberToInteger($this->lowestVersion);
148
        }
149
        return 0;
150
    }
151
152
    public function getHighestVersionAsInteger(): int
153
    {
154
        if ($this->highestVersion) {
155
            return VersionNumberUtility::convertVersionNumberToInteger($this->highestVersion);
156
        }
157
        return 0;
158
    }
159
}
160