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

PackageFinder   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 56
rs 10
c 1
b 0
f 0
ccs 21
cts 21
cp 1
wmc 8
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 4
A searchPackageFiles() 0 4 1
A getAllPackageFiles() 0 4 1
A getAllPackages() 0 8 2
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 $fileGlob;
13
    private $baseFolder;
14
    private $fileList;
15
16
    /**
17
     * @param string $folder Folder to search for SPK files
18
     * @param string $glob Filemask for package files (default: '*.spk')
19
     * @throws \Exception if $folder is not a folder.
20
     */
21 9
    public function __construct($folder, $glob = '*.spk')
22
    {
23 9
        if (!file_exists($folder) || !is_dir($folder)) {
24 2
            throw new \Exception($folder . ' is not a folder!');
25
        }
26 7
        if (substr($folder, -1) != '/') {
27 2
            $folder .= '/';
28 2
        }
29 7
        $this->baseFolder = $folder;
30 7
        $this->fileGlob   = $glob;
31 7
        $this->searchPackageFiles();
32 7
    }
33
34
    /**
35
     * Searches the currently set folder with the set glob for package files.
36
     */
37 7
    private function searchPackageFiles()
38
    {
39 7
        $this->fileList = glob($this->baseFolder . $this->fileGlob);
40 7
    }
41
42
    /**
43
     * Returns all found package files.
44
     *
45
     * @return array List of package files.
46
     */
47 1
    public function getAllPackageFiles()
48
    {
49 1
        return $this->fileList;
50
    }
51
52
    /**
53
     * Returns all found packages as objects.
54
     *
55
     * @return \SSpkS\Package\Package[] List of packages as objects.
56
     */
57 6
    public function getAllPackages()
58
    {
59 6
        $packages = array();
60 6
        foreach ($this->fileList as $file) {
61 6
            $packages[] = new Package($file);
62 6
        }
63 6
        return $packages;
64
    }
65
}
66