Completed
Push — master ( 9226b3...e0bc2b )
by Mike
08:41
created

Manager::getListOfCustomizations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Sugarcrm\UpgradeSpec\Data;
4
5
use Sugarcrm\UpgradeSpec\Spec\Context;
6
7
class Manager
8
{
9
    /**
10
     * @var ProviderChain
11
     */
12
    private $provider;
13
14
    /**
15
     * Manager constructor.
16
     *
17
     * @param ProviderChain $provider
18
     */
19
    public function __construct(ProviderChain $provider)
20
    {
21
        $this->provider = $provider;
22
    }
23
24
    /**
25
     * Gets all available SugarCRM versions (sorted ASC).
26
     *
27
     * @param $flav
28
     *
29
     * @return mixed
30
     */
31
    public function getVersions($flav)
32
    {
33
        $versions = $this->provider->getVersions($flav);
34
        if (!$versions) {
35
            throw new \RuntimeException(sprintf('No %s versions available', $flav));
36
        }
37
38
        return $versions;
39
    }
40
41
    /**
42
     * Gets the latest available SugarCRM version with given flav and base version.
43
     *
44
     * Examples: 7.6.1 -> 7.6.1.0, 7.7 -> 7.7.1, 7.8 -> 7.8.0.0
45
     *
46
     * @param $flav
47
     * @param null $baseVersion
48
     *
49
     * @return mixed
50
     */
51
    public function getLatestVersion($flav, $baseVersion = null)
52
    {
53
        $versions = $this->getVersions($flav);
54
55
        // the latest available
56
        if (!$baseVersion) {
57
            return end($versions);
58
        }
59
60
        // is minor bugfix version
61
        $versionParts = explode('.', $baseVersion);
62
        if (isset($versionParts[3])) {
63
            if (!in_array($baseVersion, $versions)) {
64
                throw new \InvalidArgumentException(sprintf('Unknown version: %s', $baseVersion));
65
            }
66
67
            return $baseVersion;
68
        }
69
70
        /**
71
         * all versions with $baseVersion base
72
         * for example: 7.6.1 -> [7.6.1.0, 7.6.1.1], 7.6 -> [7.6.1.0, 7.6.1.1, 7.6.2].
73
         */
74
        $minors = array_filter($versions, function ($minor) use ($baseVersion) {
75
            return implode('.', array_slice(explode('.', $minor), 0, count(explode('.', $baseVersion)))) === $baseVersion;
76
        });
77
78
        if (empty($minors)) {
79
            throw new \InvalidArgumentException(sprintf('No minor versions available for version: %s', $baseVersion));
80
        }
81
82
        return end($minors);
83
    }
84
85
    /**
86
     * Gets release notes for all versions from given range.
87
     *
88
     * @param Context $context
89
     *
90
     * @return mixed
91
     */
92
    public function getReleaseNotes(Context $context)
93
    {
94
        $versions = $this->getVersionRange($context);
95
96
        return $this->provider->getReleaseNotes($context->getFlav(), $versions);
97
    }
98
99
    /**
100
     * Gets all required information to perform health check.
101
     *
102
     * @param $version
103
     *
104
     * @return mixed
105
     */
106
    public function getHealthCheckInfo($version)
107
    {
108
        return $this->provider->getHealthCheckInfo($version);
109
    }
110
111
    /**
112
     * Gets all required information to perform upgrade.
113
     *
114
     * @param $version
115
     *
116
     * @return mixed
117
     */
118
    public function getUpgraderInfo($version)
119
    {
120
        return $this->provider->getUpgraderInfo($version);
121
    }
122
123
    /**
124
     * Gets all available versions from given range ($from < version <= $to).
125
     *
126
     * @param Context $context
127
     *
128
     * @return array
129
     */
130
    private function getVersionRange(Context $context)
131
    {
132
        $versionParts = explode('.', $context->getBuildVersion());
133
        $oldVersion = implode('.', array_merge($versionParts, array_fill(0, 4 - count($versionParts), '0')));
134
        $newVersion = $context->getUpgradeVersion();
135
136
        $versions = [];
137
        foreach ($this->getVersions($context->getFlav()) as $version) {
138
            if (version_compare($version, $oldVersion, '>') && version_compare($version, $newVersion, '<=')) {
139
                $versions[] = $version;
140
            }
141
        }
142
143
        return $versions;
144
    }
145
}
146