Afterload::process()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 6
nop 1
dl 0
loc 14
ccs 10
cts 10
cp 1
crap 4
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace kalanis\kw_afterload;
4
5
6
/**
7
 * Class Afterload
8
 * @package kalanis\kw_afterload
9
 * Process all prepared files with settings in specified directory
10
 * Because Bootstrap is too static
11
 *
12
 * - list all files in datadir
13
 * - order them by their names
14
 * - process them one-by-one
15
 */
16
class Afterload
17
{
18
    /**
19
     * @param string[] $path
20
     */
21 3
    public static function run(array $path): void
22
    {
23 3
        $self = new static();
24 3
        $self->process($path);
25 2
    }
26
27 3
    final public function __construct()
28
    {
29 3
    }
30
31
    /**
32
     * @param string[] $path
33
     * @throws AfterloadException
34
     */
35 3
    public function process(array $path): void
36
    {
37 3
        $exception = null;
38 3
        $files = $this->listFiles($path);
39 3
        sort($files);
40 3
        foreach ($files as $file) {
41
            try {
42 2
                require_once $file;
43 1
            } catch (AfterloadException $ex) {
44 1
                $exception = $ex->addPrev($exception);
45
            }
46
        }
47 3
        if (!empty($exception)) {
48 1
            throw $exception;
49
        }
50 2
    }
51
52
    /**
53
     * @param string[] $passedPath
54
     * @return array<string> array of paths
55
     */
56 3
    protected function listFiles(array $passedPath): array
57
    {
58 3
        $path = realpath(implode(DIRECTORY_SEPARATOR, $passedPath));
59 3
        if (!$path) {
60 1
            return [];
61
        }
62 2
        $available = [];
63 2
        $files = scandir($path);
64 2
        if (false !== $files) {
65 2
            foreach ($files as $file) {
66
                if (
67 2
                    is_file($path . DIRECTORY_SEPARATOR . $file)
68 2
                    && strpos($file, '.php')
69
                ) {
70 2
                    $available[] = $path . DIRECTORY_SEPARATOR . $file;
71
                }
72
            }
73
        }
74 2
        return $available;
75
    }
76
}
77