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

ChainStorage   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 43.75%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 15
c 2
b 0
f 0
dl 0
loc 51
ccs 7
cts 16
cp 0.4375
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addStorage() 0 3 1
A __construct() 0 3 1
A addLocation() 0 13 3
A load() 0 9 3
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