Manager::getLatestVersion()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 32
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 13
c 0
b 0
f 0
nc 5
nop 2
dl 0
loc 32
rs 8.439
1
<?php
2
3
namespace Sugarcrm\UpgradeSpec\Data;
4
5
use Sugarcrm\UpgradeSpec\Context\Upgrade;
6
use Sugarcrm\UpgradeSpec\Version\OrderedList;
7
use Sugarcrm\UpgradeSpec\Version\Version;
8
9
class Manager
10
{
11
    /**
12
     * @var ProviderChain
13
     */
14
    private $provider;
15
16
    /**
17
     * Manager constructor.
18
     *
19
     * @param ProviderChain $provider
20
     */
21
    public function __construct(ProviderChain $provider)
22
    {
23
        $this->provider = $provider;
24
    }
25
26
    /**
27
     * Gets all available SugarCRM versions (sorted ASC).
28
     *
29
     * @param string $flav
30
     *
31
     * @return OrderedList
32
     */
33
    public function getVersions($flav)
34
    {
35
        $versions = $this->provider->getVersions($flav);
36
        if (!count($versions)) {
37
            throw new \RuntimeException(sprintf('No "%s" versions available', $flav));
38
        }
39
40
        return $versions;
41
    }
42
43
    /**
44
     * Gets the latest available SugarCRM version with given flav and base version.
45
     *
46
     * Examples: 7.6.1 -> 7.6.1.0, 7.7 -> 7.7.1, 7.8 -> 7.8.0.0
47
     *
48
     * @param string $flav
49
     * @param Version|null $baseVersion
50
     *
51
     * @return Version
52
     */
53
    public function getLatestVersion($flav, Version $baseVersion = null)
54
    {
55
        $versions = $this->getVersions($flav);
56
57
        // the latest available
58
        if (is_null($baseVersion)) {
59
            return $versions->last();
60
        }
61
62
        // is minor bugfix version
63
        if ($baseVersion->isFull()) {
64
            if (!$versions->contains($baseVersion)) {
65
                throw new \InvalidArgumentException(sprintf('Unknown version: %s', $baseVersion));
66
            }
67
68
            return $baseVersion;
69
        }
70
71
        /**
72
         * all versions with $baseVersion base
73
         * for example: 7.6.1 -> [7.6.1, 7.6.1.0, 7.6.1.1], 7.6 -> [7.6.1.0, 7.6.1.1, 7.6.2].
74
         */
75
        $minors = $versions->filter(function (Version $version) use ($baseVersion) {
76
            return $version->isChildOf($baseVersion) || $version->isEqualTo($baseVersion);
77
        });
78
79
        if (!count($minors)) {
80
            throw new \InvalidArgumentException(sprintf('No minor versions available for version: %s', $baseVersion));
81
        }
82
83
        return $minors->last();
84
    }
85
86
    /**
87
     * Gets release notes for all versions from given range.
88
     *
89
     * @param Upgrade $context
90
     *
91
     * @return array
92
     */
93
    public function getReleaseNotes(Upgrade $context)
94
    {
95
        $versions = $this->getVersionRange($context);
96
97
        return $this->provider->getReleaseNotes($context->getBuildFlav(), $versions);
0 ignored issues
show
Documentation introduced by
$versions is of type object<Sugarcrm\UpgradeSpec\Version\OrderedList>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
98
    }
99
100
    /**
101
     * Gets all required information to perform health check.
102
     *
103
     * @param Version $version
104
     *
105
     * @return mixed
106
     */
107
    public function getHealthCheckInfo(Version $version)
108
    {
109
        return $this->provider->getHealthCheckInfo($version);
110
    }
111
112
    /**
113
     * Gets all required information to perform upgrade.
114
     *
115
     * @param Version $version
116
     *
117
     * @return mixed
118
     */
119
    public function getUpgraderInfo(Version $version)
120
    {
121
        return $this->provider->getUpgraderInfo($version);
122
    }
123
124
    /**
125
     * Gets the list of potentially broken customizations (changed and deleted files)
126
     *
127
     * @param Upgrade $context
128
     *
129
     * @return mixed
130
     */
131
    public function getPotentiallyBrokenCustomizations(Upgrade $context)
132
    {
133
        return $this->provider->getPotentiallyBrokenCustomizations($context);
0 ignored issues
show
Documentation Bug introduced by
The method getPotentiallyBrokenCustomizations does not exist on object<Sugarcrm\UpgradeSpec\Data\ProviderChain>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
134
    }
135
136
    /**
137
     * Gets the lists of upgrade steps for the given source
138
     *
139
     * @param Upgrade $context
140
     *
141
     * @return mixed
142
     */
143
    public function getUpgradeSteps(Upgrade $context)
144
    {
145
        return $this->provider->getUpgradeSteps($context);
0 ignored issues
show
Documentation Bug introduced by
The method getUpgradeSteps does not exist on object<Sugarcrm\UpgradeSpec\Data\ProviderChain>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
146
    }
147
148
    /**
149
     * Gets all available versions from given range ($from < version <= $to).
150
     *
151
     * @param Upgrade $context
152
     *
153
     * @return OrderedList
154
     */
155
    private function getVersionRange(Upgrade $context)
156
    {
157
        $from = (string) $context->getBuildVersion()->getFull();
0 ignored issues
show
Bug introduced by
The method getFull cannot be called on $context->getBuildVersion() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
158
        $to = (string) $context->getTargetVersion()->getFull();
0 ignored issues
show
Bug introduced by
The method getFull cannot be called on $context->getTargetVersion() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
159
160
        return $this->getVersions($context->getBuildFlav())->filter(function (Version $version) use ($from, $to) {
161
            return version_compare((string) $version, $from, '>')
162
                && version_compare((string) $version, $to, '<=');
163
        });
164
    }
165
}
166