Passed
Push — master ( 9e220a...448f9b )
by Petr
07:43
created

Afterload::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
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 3
    public static function run(): void
19
    {
20 3
        $self = new static();
21 3
        $self->process();
22 2
    }
23
24 3
    final public function __construct()
25
    {
26 3
    }
27
28
    /**
29
     * @throws AfterloadException
30
     */
31 3
    public function process(): void
32
    {
33 3
        $exception = null;
34 3
        $files = $this->listFiles();
35 3
        sort($files);
36 3
        foreach ($files as $file) {
37
            try {
38 2
                require_once $file;
39 1
            } catch (AfterloadException $ex) {
40 1
                $exception = $ex->addPrev($exception);
41
            }
42
        }
43 3
        if (!empty($exception)) {
44 1
            throw $exception;
45
        }
46 2
    }
47
48
    /**
49
     * @return array<string> array of paths
50
     */
51 3
    protected function listFiles(): array
52
    {
53 3
        $path = realpath(implode(DIRECTORY_SEPARATOR, $this->path()));
54 3
        if (!$path) {
55 1
            return [];
56
        }
57 2
        $available = [];
58 2
        $files = scandir($path);
59 2
        if (false !== $files) {
60 2
            foreach ($files as $file) {
61
                if (
62 2
                    is_file($path . DIRECTORY_SEPARATOR . $file)
63 2
                    && strpos($file, '.php')
64
                ) {
65 2
                    $available[] = $path . DIRECTORY_SEPARATOR . $file;
66
                }
67
            }
68
        }
69 2
        return $available;
70
    }
71
72
    /**
73
     * @return array<string>
74
     * @codeCoverageIgnore could be set outside - like from tests
75
     */
76
    protected function path(): array
77
    {
78
        return [__DIR__, '..', 'data'];
79
    }
80
}
81