Passed
Push — scrutinizer-fix ( 3b85d4...1dbb83 )
by Pascal
03:07
created

PackageFinder::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 14
ccs 12
cts 12
cp 1
crap 4
rs 9.9332
1
<?php
2
3
namespace SSpkS\Package;
4
5
use \SSpkS\Package\Package;
6
7
/**
8
 * SPK PackageFinder class
9
 */
10
class PackageFinder
11
{
12
    private $config;
13
    private $fileGlob;
14
    private $baseFolder;
15
    private $fileList;
16
17
    /**
18
     * @param \SSpkS\Config $config Config object
19
     * @throws \Exception if $folder is not a folder.
20
     */
21 9
    public function __construct(\SSpkS\Config $config)
22
    {
23 9
        $this->config = $config;
24 9
        $folder = $this->config->paths['packages'];
25 9
        $glob = $this->config->packages['file_mask'];
26 9
        if (!file_exists($folder) || !is_dir($folder)) {
27 2
            throw new \Exception($folder . ' is not a folder!');
28
        }
29 7
        if (substr($folder, -1) != '/') {
30 2
            $folder .= '/';
31 2
        }
32 7
        $this->baseFolder = $folder;
33 7
        $this->fileGlob   = $glob;
34 7
        $this->searchPackageFiles();
35 7
    }
36
37
    /**
38
     * Searches the currently set folder with the set glob for package files.
39
     */
40 7
    private function searchPackageFiles()
41
    {
42 7
        $this->fileList = glob($this->baseFolder . $this->fileGlob);
43 7
    }
44
45
    /**
46
     * Returns all found package files.
47
     *
48
     * @return array List of package files.
49
     */
50 1
    public function getAllPackageFiles()
51
    {
52 1
        return $this->fileList;
53
    }
54
55
    /**
56
     * Returns all found packages as objects.
57
     *
58
     * @return \SSpkS\Package\Package[] List of packages as objects.
59
     */
60 6
    public function getAllPackages()
61
    {
62 6
        $packages = array();
63 6
        foreach ($this->fileList as $file) {
64 6
            $packages[] = new Package($this->config, $file);
65 6
        }
66 6
        return $packages;
67
    }
68
}
69