ChainLoader   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 68
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
     *  {@inheritdoc}
58
     */
59
    public function load($type, $parameters = [], $withoutParameters = [], $responseType = LoaderInterface::SINGLE)
60
    {
61
        foreach ($this->loaders as $loader) {
62
            if ($loader->isSupported($type)) {
63
                if (false !== $meta = $loader->load($type, $parameters, $withoutParameters, $responseType)) {
64
                    return $meta;
65
                }
66
            }
67
        }
68
69
        return false;
70
    }
71
72
    /**
73
     * Checks if Loader supports provided type.
74
     *
75
     * @param string $type
76
     *
77
     * @return bool
78
     */
79
    public function isSupported(string $type): bool
80
    {
81
        foreach ($this->loaders as $loader) {
82
            if ($loader->isSupported($type)) {
83
                return true;
84
            }
85
        }
86
87
        return false;
88
    }
89
}
90