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

ArrayStorage::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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\Exceptions\LoaderException;
21
use Biurad\UI\Interfaces\StorageInterface;
22
23
/**
24
 * Load templates from an array.
25
 *
26
 * @author Divine Niiquaye Ibok <[email protected]>
27
 */
28
class ArrayStorage implements StorageInterface
29
{
30
    /** @var string[] */
31
    protected $templates = [];
32
33
    /**
34
     * @param string[] $templates An array of templates
35
     */
36 5
    public function __construct(array $templates = [])
37
    {
38 5
        $this->templates = $templates;
39 5
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 1
    public function addLocation(string $location): void
45
    {
46 1
        throw new LoaderException(\sprintf('Cannot use [%s] for templates loading.', $location));
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 4
    public function load(string $template): ?string
53
    {
54 4
        $loadedTemplate = $this->templates[$template] ?? null;
55
56 4
        if ($loadedTemplate instanceof \Stringable || \is_string($loadedTemplate)) {
57 4
            return (string) $loadedTemplate;
58
        }
59
60 4
        if (null !== $loadedTemplate) {
0 ignored issues
show
introduced by
The condition null !== $loadedTemplate is always false.
Loading history...
61 1
            throw new LoaderException(\sprintf('Failed to load "%s" as it\'s source isn\'t stringable.', $template));
62
        }
63
64 3
        return $loadedTemplate;
65
    }
66
}
67