FilesystemStorage::load()   B
last analyzed

Complexity

Conditions 10
Paths 13

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 10.0203

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 10
eloc 16
c 2
b 1
f 0
nc 13
nop 2
dl 0
loc 34
ccs 16
cts 17
cp 0.9412
crap 10.0203
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    /** @var array<int,string> */
34
    protected $paths;
35
36
    /**
37
     * @param string|string[] $templatePaths An array of paths to look for templates
38
     */
39 6
    public function __construct($templatePaths = [], LoggerInterface $logger = null)
40
    {
41 6
        $this->logger = $logger;
42 6
        $this->paths = \is_array($templatePaths) ? $templatePaths : [$templatePaths];
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function addLocation(string $location): void
49
    {
50
        $this->paths[] = $location;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 5
    public function load(string $template, array $namespaces): ?string
57
    {
58 5
        $fileFailures = [];
59
60 5
        if (\file_exists($template)) {
61 2
            if (null !== $this->logger) {
62
                $this->logger->debug('Loaded template file.', ['file' => $template]);
63
            }
64
65 2
            return $template;
66
        }
67
68 5
        foreach (($namespaces ?: $this->paths) as $path) {
69 5
            if (\file_exists($found = $path . '/' . $template)) {
70 5
                if (null !== $this->logger) {
71 1
                    $this->logger->debug('Loaded template file.', ['file' => $template]);
72
                }
73
74 5
                return $found;
75
            }
76
77 5
            if (null !== $this->logger) {
78 1
                $fileFailures[] = $template;
79
            }
80
        }
81
82
        // only log failures if no template could be loaded at all
83 5
        foreach ($fileFailures as $file) {
84 1
            if (null !== $this->logger) {
85 1
                $this->logger->debug('Failed loading template file.', ['file' => $file]);
86
            }
87
        }
88
89 5
        return null;
90
    }
91
}
92