1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sugarcrm\UpgradeSpec\Element; |
4
|
|
|
|
5
|
|
|
use Sugarcrm\UpgradeSpec\Spec\Context; |
6
|
|
|
|
7
|
|
|
class Provider |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
private $specElements = []; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Provider constructor. |
16
|
|
|
* |
17
|
|
|
* @param array $specElements |
18
|
|
|
*/ |
19
|
|
|
public function __construct($specElements = []) |
20
|
|
|
{ |
21
|
|
|
$this->addElements($specElements); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Adds elements to provider |
26
|
|
|
* |
27
|
|
|
* @param mixed $elements |
28
|
|
|
*/ |
29
|
|
View Code Duplication |
public function addElements($elements) |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
if (!is_array($elements) && !$elements instanceof \Traversable) { |
32
|
|
|
throw new \InvalidArgumentException(sprintf('Argument is not traversable: %s', $elements)); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$elements = is_array($elements) ? $elements : iterator_to_array($elements); |
|
|
|
|
36
|
|
|
|
37
|
|
|
foreach ($elements as $element) { |
38
|
|
|
if (!is_a($element, ElementInterface::class)) { |
39
|
|
|
throw new \InvalidArgumentException('Provider expects ElementInterface[]'); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->specElements = array_merge($this->specElements, $elements); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param Context $context |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
public function getSuitableElements(Context $context) |
52
|
|
|
{ |
53
|
|
|
$elements = array_filter($this->specElements, function (ElementInterface $element) use ($context) { |
54
|
|
|
return $element->isRelevantTo($context); |
55
|
|
|
}); |
56
|
|
|
|
57
|
|
|
if (!$elements) { |
|
|
|
|
58
|
|
|
throw new \DomainException(sprintf('No special steps required to upgrade from "%s" to "%s" (%s))', |
59
|
|
|
$context->getBuildVersion(), |
60
|
|
|
$context->getUpgradeVersion(), |
61
|
|
|
$context->getFlav() |
62
|
|
|
)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// sort elements (ASC) |
66
|
|
|
usort($elements, function (ElementInterface $a, ElementInterface $b) { |
67
|
|
|
return $a->getOrder() > $b->getOrder() ? 1 : ($a->getOrder() < $b->getOrder() ? -1 : 0); |
68
|
|
|
}); |
69
|
|
|
|
70
|
|
|
return $elements; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.