Passed
Push — master ( 417605...635760 )
by Divine Niiquaye
11:55
created

ChainStorage::addStorage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

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 0
cts 2
cp 0
crap 2
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
 * ChainStorage is a loader that calls other storage loaders to load templates.
25
 *
26
 * @author Divine Niiquaye Ibok <[email protected]>
27
 */
28
class ChainStorage implements StorageInterface
29
{
30
    /** @var array<int,StorageInterface> */
31
    protected $loaders = [];
32
33
    /**
34
     * @param array<int,StorageInterface> $storages An array of storage instances
35
     */
36 3
    public function __construct(array $storages = [])
37
    {
38 3
        $this->loaders = $storages;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function addLocation(string $location): void
45
    {
46
        foreach ($this->loaders as $storage) {
47
            try {
48
                $storage->addLocation($location);
49
50
                return;
51
            } catch (LoaderException $e) {
52
                continue;
53
            }
54
        }
55
56
        throw new LoaderException(\sprintf('Failed to use [%s] for views loading', $location));
57
    }
58
59
    /**
60
     * Adds a storage loader instance.
61
     */
62
    public function addStorage(StorageInterface $storage): void
63
    {
64
        $this->loaders[] = $storage;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 3
    public function load(string $template, array $namespaces): ?string
71
    {
72 3
        foreach ($this->loaders as $loader) {
73 3
            if (null !== $storage = $loader->load($template, $namespaces)) {
74 3
                return $storage;
75
            }
76
        }
77
78 3
        return null;
79
    }
80
}
81