Completed
Push — master ( fecb59...e32f72 )
by Paweł
29:36
created

ChainLoader   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 54
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addLoader() 0 4 1
A load() 0 12 4
A isSupported() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of the Superdesk Web Publisher Templates System.
7
 *
8
 * Copyright 2015 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2015 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Component\TemplatesSystem\Gimme\Loader;
18
19
/**
20
 * ChainLoader is a loader that calls other loaders to load Meta objects.
21
 */
22
class ChainLoader implements LoaderInterface
23
{
24
    protected $loaders = [];
25
26
    /**
27
     * Adds a loader instance.
28
     *
29
     * @param LoaderInterface $loader A Loader instance
30
     */
31 106
    public function addLoader(LoaderInterface $loader)
32
    {
33 106
        $this->loaders[] = $loader;
34 106
    }
35
36
    /**
37
     * Loads a Meta class from given datasource.
38
     *
39
     * @param string     $type         object type
40
     * @param array|null $parameters   parameters needed to load required object type
41
     * @param int        $responseType response type: single meta (LoaderInterface::SINGLE) or collection of metas (LoaderInterface::COLLECTION)
42
     *
43
     * @return Meta|bool false if meta cannot be loaded, a Meta instance otherwise
44
     */
45 17
    public function load($type, $parameters = [], $responseType = LoaderInterface::SINGLE)
46
    {
47 17
        foreach ($this->loaders as $loader) {
48 17
            if ($loader->isSupported($type)) {
49 17
                if (false !== $meta = $loader->load($type, $parameters, $responseType)) {
50 17
                    return $meta;
51
                }
52
            }
53
        }
54
55 5
        return false;
56
    }
57
58
    /**
59
     * Checks if Loader supports provided type.
60
     *
61
     * @param string $type
62
     *
63
     * @return bool
64
     */
65 1
    public function isSupported(string $type) : bool
66
    {
67 1
        foreach ($this->loaders as $loader) {
68 1
            if ($loader->isSupported($type)) {
69 1
                return true;
70
            }
71
        }
72
73
        return false;
74
    }
75
}
76