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

FilesystemStorage   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 95.83%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 22
c 3
b 1
f 0
dl 0
loc 66
ccs 23
cts 24
cp 0.9583
rs 10
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addLocation() 0 3 1
A __construct() 0 4 2
B load() 0 34 9
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