Passed
Branch master (b0d099)
by Markus
03:49
created

PackageFilter   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 156
rs 10
c 0
b 0
f 0
ccs 56
cts 56
cp 1
wmc 25
lcom 1
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setArchitectureFilter() 0 4 1
A setFirmwareVersionFilter() 0 4 1
A setChannelFilter() 0 4 1
A setOldVersionFilter() 0 4 1
A isMatchingArchitecture() 0 8 2
A isMatchingFirmwareVersion() 0 7 2
B isMatchingChannel() 0 12 6
A removeObsoleteVersions() 0 12 4
B getFilteredPackageList() 0 21 6
1
<?php
2
3
namespace SSpkS\Package;
4
5
use \SSpkS\Package\Package;
6
7
/**
8
 * SPK PackageFinder class
9
 */
10
class PackageFilter
11
{
12
    private $pkgList;
13
    /** @var bool|string[] $filterArch Array of allowed architectures, or FALSE to ignore. */
14
    private $filterArch = false;
15
    /** @var bool|string $filterFwVersion Target firmware version, or FALSE to ignore. */
16
    private $filterFwVersion = false;
17
    /** @var bool|string $filterChannel Channel 'stable' or 'beta', or FALSE to ignore. */
18
    private $filterChannel = false;
19
    /** @var bool $filterOldVersions TRUE to return unique packages with latest version only. */
20
    private $filterOldVersions = false;
21
22
    /**
23
     * @param \SSpkS\Package\Package[] $pkgList List of Package objects to filter
24
     */
25 5
    public function __construct(array $pkgList)
26
    {
27 5
        $this->pkgList = $pkgList;
28 5
    }
29
30
    /**
31
     * Sets the architecture to filter for.
32
     *
33
     * @param string $arch Architecture.
34
     */
35 1
    public function setArchitectureFilter($arch)
36
    {
37 1
        $this->filterArch = array('noarch', $arch);
38 1
    }
39
40
    /**
41
     * Sets the firmware version to filter for.
42
     *
43
     * @param string|bool $version Firmware version in dotted notation ('1.2.3456') or FALSE to ignore.
44
     */
45 1
    public function setFirmwareVersionFilter($version)
46
    {
47 1
        $this->filterFwVersion = $version;
48 1
    }
49
50
    /**
51
     * Sets the channel to filter for.
52
     *
53
     * @param string $channel Channel ('stable' or 'beta')
54
     */
55 1
    public function setChannelFilter($channel)
56
    {
57 1
        $this->filterChannel = $channel;
58 1
    }
59
60
    /**
61
     * Enables or disables omitting older versions of the same package from the result set.
62
     *
63
     * @param bool $status TRUE to enable the filter, FALSE to disable.
64
     */
65 1
    public function setOldVersionFilter($status)
66
    {
67 1
        $this->filterOldVersions = $status;
68 1
    }
69
70
    /**
71
     * If filter is enabled, checks if architecture of $package is compatible to requested one.
72
     *
73
     * @param \SSpkS\Package\Package $package Package to test.
74
     * @return bool TRUE if matching, or FALSE.
75
     */
76 5
    public function isMatchingArchitecture($package)
77
    {
78 5
        if ($this->filterArch === false) {
79 4
            return true;
80
        }
81 1
        $matches = array_intersect($this->filterArch, $package->arch);
82 1
        return (count($matches) > 0);
83
    }
84
85
    /**
86
     * If filter is enabled, checks if minimal firmware required of $package is
87
     * smaller or equal to system firmware.
88
     *
89
     * @param \SSpkS\Package\Package $package Package to test.
90
     * @return bool TRUE if matching, or FALSE.
91
     */
92 5
    public function isMatchingFirmwareVersion($package)
93
    {
94 5
        if ($this->filterFwVersion === false) {
95 4
            return true;
96
        }
97 1
        return (version_compare($package->firmware, $this->filterFwVersion, '<='));
98
    }
99
100
    /**
101
     * If filter is enabled, checks if channel of $package matches requested one.
102
     * 'beta' will show ALL packages, also those from 'stable'.
103
     *
104
     * @param \SSpkS\Package\Package $package Package to test.
105
     * @return bool TRUE if matching, or FALSE.
106
     */
107 5
    public function isMatchingChannel($package)
108
    {
109 5
        if ($this->filterChannel === false) {
110 4
            return true;
111
        }
112 1
        if ($this->filterChannel == 'stable' && (!isset($package->beta) || $package->beta === false)) {
113 1
            return true;
114 1
        } elseif ($this->filterChannel == 'beta') {
115 1
            return true;
116
        }
117 1
        return false;
118
    }
119
120
    /**
121
     * Removes older versions of same package from $pkgList.
122
     *
123
     * @param \SSpkS\Package\Package[] $pkgList List of packages
124
     * @return \SSpkS\Package\Package[] List of unique packages
125
     */
126 1
    public function removeObsoleteVersions($pkgList)
127
    {
128 1
        $uniqueList = array();
129 1
        foreach ($pkgList as $package) {
130 1
            $pkgId = $package->package;
131 1
            if (isset($uniqueList[$pkgId]) && version_compare($uniqueList[$pkgId]->version, $package->version, '>=')) {
132 1
                continue;
133
            }
134 1
            $uniqueList[$pkgId] = $package;
135 1
        }
136 1
        return array_values($uniqueList);
137
    }
138
139
    /**
140
     * Returns the list of packages matching the currently set filters.
141
     *
142
     * @return \SSpkS\Package\Package[] List of Package objects matching filters.
143
     */
144 5
    public function getFilteredPackageList()
145
    {
146 5
        $filteredPackages = array();
147 5
        foreach ($this->pkgList as $package) {
148 5
            if (!$this->isMatchingArchitecture($package)) {
149 1
                continue;
150
            }
151 5
            if (!$this->isMatchingFirmwareVersion($package)) {
152 1
                continue;
153
            }
154 5
            if (!$this->isMatchingChannel($package)) {
155 1
                continue;
156
            }
157 5
            $filteredPackages[] = $package;
158 5
        }
159 5
        if ($this->filterOldVersions) {
160
            // remove older versions of duplicate packages from $filteredPackages
161 1
            $filteredPackages = $this->removeObsoleteVersions($filteredPackages);
162 1
        }
163 5
        return $filteredPackages;
164
    }
165
}
166