Passed
Push — master ( fbdae9...66097f )
by Divine Niiquaye
10:37
created

FilesystemStorage::load()   B

Complexity

Conditions 9
Paths 13

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 9.0164

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 9
eloc 16
c 2
b 1
f 0
nc 13
nop 1
dl 0
loc 34
ccs 16
cts 17
cp 0.9412
crap 9.0164
rs 8.0555
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Biurad opensource projects.
7
 *
8
 * PHP version 7.2 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Biurad\UI\Storage;
19
20
use Biurad\UI\Interfaces\StorageInterface;
21
use Psr\Log\LoggerInterface;
22
23
/**
24
 * FilesystemLoader is a loader that read templates from the filesystem.
25
 *
26
 * @author Divine Niiquaye Ibok <[email protected]>
27
 */
28
class FilesystemStorage implements StorageInterface
29
{
30
    /** @var LoggerInterface|null */
31
    protected $logger;
32
33
    /**
34
     * The array of active view paths.
35
     *
36
     * @var string[]
37
     */
38
    protected $paths;
39
40
    /**
41
     * @param string|string[] $templatePaths An array of paths to look for templates
42
     */
43 6
    public function __construct($templatePaths = [], LoggerInterface $logger = null)
44
    {
45 6
        $this->logger = $logger;
46 6
        $this->paths = \is_array($templatePaths) ? $templatePaths : [$templatePaths];
47 6
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 4
    public function addLocation(string $location): void
53
    {
54 4
        $this->paths[] = $location;
55 4
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 5
    public function load(string $template): ?string
61
    {
62 5
        $fileFailures = [];
63
64 5
        if (\file_exists($template)) {
65 1
            if (null !== $this->logger) {
66
                $this->logger->debug('Loaded template file.', ['file' => $template]);
67
            }
68
69 1
            return $template;
70
        }
71
72 5
        foreach ($this->paths as $path) {
73 5
            if (\file_exists($found = $path . '/' . $template)) {
74 5
                if (null !== $this->logger) {
75 1
                    $this->logger->debug('Loaded template file.', ['file' => $template]);
76
                }
77
78 5
                return $found;
79
            }
80
81 5
            if (null !== $this->logger) {
82 1
                $fileFailures[] = $template;
83
            }
84
        }
85
86
        // only log failures if no template could be loaded at all
87 5
        foreach ($fileFailures as $file) {
88 1
            if (null !== $this->logger) {
89 1
                $this->logger->debug('Failed loading template file.', ['file' => $file]);
90
            }
91
        }
92
93 5
        return null;
94
    }
95
}
96