Completed
Push — master ( 1f3fa8...7d4b61 )
by Mike
03:08
created

Provider::getSuitableElements()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 2
nop 1
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $elements. This often makes code more readable.
Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $elements of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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