Passed
Push — scrutinizer-test ( 6f2f69...953bdc )
by Pascal
02:22
created

PackageFilter::isMatchingChannel()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 7
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 11
ccs 8
cts 8
cp 1
crap 5
rs 9.6111
1
<?php
2
3
namespace SSpkS\Package;
4
5
use \SSpkS\Device\DeviceList;
6
use \SSpkS\Package\Package;
7
8
/**
9
 * SPK PackageFinder class
10
 */
11
class PackageFilter
12
{
13
    private $config;
14
    private $pkgList;
15
    /** @var bool|string[] $filterArch Array of allowed architectures, or FALSE to ignore. */
16
    private $filterArch = false;
17
    /** @var bool|string $filterFwVersion Target firmware version, or FALSE to ignore. */
18
    private $filterFwVersion = false;
19
    /** @var bool|string $filterChannel Channel 'stable' or 'beta', or FALSE to ignore. */
20
    private $filterChannel = false;
21
    /** @var bool $filterOldVersions TRUE to return unique packages with latest version only. */
22
    private $filterOldVersions = false;
23
24
    /**
25
     * @param \SSpkS\Config $config Config object
26
     * @param \SSpkS\Package\Package[] $pkgList List of Package objects to filter
27
     */
28 5
    public function __construct(\SSpkS\Config $config, array $pkgList)
29
    {
30 5
        $this->config = $config;
31 5
        $this->pkgList = $pkgList;
32 5
    }
33
34
    /**
35
     * Sets the architecture to filter for.
36
     *
37
     * @param string $arch Architecture.
38
     */
39 1
    public function setArchitectureFilter($arch)
40
    {
41
        // Specific corner case
42 1
        if ($arch == '88f6282') {
43
            $arch = '88f6281';
44
        }
45
      
46 1
        $dl = new DeviceList($this->config);
47 1
        $family = $dl->getFamily($arch);
48 1
        $this->filterArch = array_unique(array('noarch', $arch, $family));
49 1
    }
50
51
    /**
52
     * Sets the firmware version to filter for.
53
     *
54
     * @param string|bool $version Firmware version in dotted notation ('1.2.3456') or FALSE to ignore.
55
     */
56 1
    public function setFirmwareVersionFilter($version)
57
    {
58 1
        $this->filterFwVersion = $version;
59 1
    }
60
61
    /**
62
     * Sets the channel to filter for.
63
     *
64
     * @param string $channel Channel ('stable' or 'beta')
65
     */
66 1
    public function setChannelFilter($channel)
67
    {
68 1
        $this->filterChannel = $channel;
69 1
    }
70
71
    /**
72
     * Enables or disables omitting older versions of the same package from the result set.
73
     *
74
     * @param bool $status TRUE to enable the filter, FALSE to disable.
75
     */
76 1
    public function setOldVersionFilter($status)
77
    {
78 1
        $this->filterOldVersions = $status;
79 1
    }
80
81
    /**
82
     * If filter is enabled, checks if architecture of $package is compatible to requested one.
83
     *
84
     * @param \SSpkS\Package\Package $package Package to test.
85
     * @return bool TRUE if matching, or FALSE.
86
     */
87 5
    public function isMatchingArchitecture($package)
88
    {
89 5
        if ($this->filterArch === false) {
90 4
            return true;
91
        }
92 1
        $matches = array_intersect($this->filterArch, $package->arch);
0 ignored issues
show
Bug introduced by
It seems like $this->filterArch can also be of type true; however, parameter $array of array_intersect() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

92
        $matches = array_intersect(/** @scrutinizer ignore-type */ $this->filterArch, $package->arch);
Loading history...
93 1
        return (count($matches) > 0);
94
    }
95
96
    /**
97
     * If filter is enabled, checks if minimal firmware required of $package is
98
     * smaller or equal to system firmware.
99
     *
100
     * @param \SSpkS\Package\Package $package Package to test.
101
     * @return bool TRUE if matching, or FALSE.
102
     */
103 5
    public function isMatchingFirmwareVersion($package)
104
    {
105 5
        if ($this->filterFwVersion === false) {
106 4
            return true;
107
        }
108 1
        return (version_compare($package->firmware, $this->filterFwVersion, '<='));
0 ignored issues
show
Bug introduced by
It seems like $this->filterFwVersion can also be of type true; however, parameter $version2 of version_compare() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

108
        return (version_compare($package->firmware, /** @scrutinizer ignore-type */ $this->filterFwVersion, '<='));
Loading history...
109
    }
110
111
    /**
112
     * If filter is enabled, checks if channel of $package matches requested one.
113
     * 'beta' will show ALL packages, also those from 'stable'.
114
     *
115
     * @param \SSpkS\Package\Package $package Package to test.
116
     * @return bool TRUE if matching, or FALSE.
117
     */
118 5
    public function isMatchingChannel($package)
119
    {
120 5
        if ($this->filterChannel === false) {
121 4
            return true;
122
        }
123 1
        if ($this->filterChannel == 'stable' && $package->isBeta() === false) {
124 1
            return true;
125 1
        } elseif ($this->filterChannel == 'beta') {
126 1
            return true;
127
        }
128 1
        return false;
129
    }
130
131
    /**
132
     * Removes older versions of same package from $pkgList.
133
     *
134
     * @param \SSpkS\Package\Package[] $pkgList List of packages
135
     * @return \SSpkS\Package\Package[] List of unique packages
136
     */
137 1
    public function removeObsoleteVersions($pkgList)
138
    {
139 1
        $uniqueList = array();
140 1
        foreach ($pkgList as $package) {
141 1
            $pkgId = $package->package;
142 1
            if (isset($uniqueList[$pkgId]) && version_compare($uniqueList[$pkgId]->version, $package->version, '>=')) {
143 1
                continue;
144
            }
145 1
            $uniqueList[$pkgId] = $package;
146 1
        }
147 1
        return array_values($uniqueList);
148
    }
149
150
    /**
151
     * Returns the list of packages matching the currently set filters.
152
     *
153
     * @return \SSpkS\Package\Package[] List of Package objects matching filters.
154
     */
155 5
    public function getFilteredPackageList()
156
    {
157 5
        $filteredPackages = array();
158 5
        foreach ($this->pkgList as $package) {
159 5
            if (!$this->isMatchingArchitecture($package)) {
160 1
                continue;
161
            }
162 5
            if (!$this->isMatchingFirmwareVersion($package)) {
163 1
                continue;
164
            }
165 5
            if (!$this->isMatchingChannel($package)) {
166 1
                continue;
167
            }
168 5
            $filteredPackages[] = $package;
169 5
        }
170 5
        if ($this->filterOldVersions) {
171
            // remove older versions of duplicate packages from $filteredPackages
172 1
            $filteredPackages = $this->removeObsoleteVersions($filteredPackages);
173 1
        }
174 5
        return $filteredPackages;
175
    }
176
}
177