Completed
Push — master ( 83f82b...f3d39e )
by Rafał
01:39
created

ChainLoader   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 74
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addLoader() 0 8 2
A removeLoader() 0 10 2
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
    public function addLoader(LoaderInterface $loader)
32
    {
33
        if (false !== $key = array_search($loader, $this->loaders)) {
34
            $this->loaders[$key] = $loader;
35
        } else {
36
            $this->loaders[] = $loader;
37
        }
38
    }
39
40
    /**
41
     * @param LoaderInterface $loader
42
     *
43
     * @return bool
44
     */
45
    public function removeLoader(LoaderInterface $loader)
46
    {
47
        if (false !== $key = array_search($loader, $this->loaders)) {
48
            unset($this->loaders[$key]);
49
50
            return true;
51
        }
52
53
        return false;
54
    }
55
56
    /**
57
     * Loads a Meta class from given datasource.
58
     *
59
     * @param string     $type         object type
60
     * @param array|null $parameters   parameters needed to load required object type
61
     * @param int        $responseType response type: single meta (LoaderInterface::SINGLE) or collection of metas (LoaderInterface::COLLECTION)
62
     *
63
     * @return Meta|bool false if meta cannot be loaded, a Meta instance otherwise
64
     */
65
    public function load($type, $parameters = [], $responseType = LoaderInterface::SINGLE)
66
    {
67
        foreach ($this->loaders as $loader) {
68
            if ($loader->isSupported($type)) {
69
                if (false !== $meta = $loader->load($type, $parameters, $responseType)) {
70
                    return $meta;
71
                }
72
            }
73
        }
74
75
        return false;
76
    }
77
78
    /**
79
     * Checks if Loader supports provided type.
80
     *
81
     * @param string $type
82
     *
83
     * @return bool
84
     */
85
    public function isSupported(string $type): bool
86
    {
87
        foreach ($this->loaders as $loader) {
88
            if ($loader->isSupported($type)) {
89
                return true;
90
            }
91
        }
92
93
        return false;
94
    }
95
}
96