Completed
Push — master ( bf375b...cc5661 )
by Rafał
07:07
created

ChainLoader   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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