Completed
Push — upgrade-2.0-from-1.0 ( 918602 )
by David
13:04
created

ChainLoader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 49
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A find() 0 15 3
A getLoaderNamesString() 0 9 2
1
<?php
2
3
/*
4
 * This file is part of the `liip/LiipImagineBundle` project.
5
 *
6
 * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Liip\ImagineBundle\Binary\Loader;
13
14
use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
15
16
class ChainLoader implements LoaderInterface
17
{
18
    /**
19
     * @var LoaderInterface[]
20
     */
21
    private $loaders;
22
23
    /**
24
     * @param LoaderInterface[] $loaders
25
     */
26
    public function __construct(array $loaders)
27
    {
28
        $this->loaders = array_filter($loaders, function ($loader) {
29
            return $loader instanceof LoaderInterface;
30
        });
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function find($path)
37
    {
38
        foreach ($this->loaders as $loader) {
39
            try {
40
                return $loader->find($path);
41
            } catch (\Exception $loaderException) {
42
                // handle exception later
43
            }
44
        }
45
46
        throw new NotLoadableException(vsprintf('Source image not resolvable "%s" using "%s" loaders.', array(
47
            $path,
48
            $this->getLoaderNamesString(),
49
        )));
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    private function getLoaderNamesString()
56
    {
57
        $names = array();
58
        foreach ($this->loaders as $n => $l) {
59
            $names[] = sprintf('%s=[%s]', $n, get_class($l));
60
        }
61
62
        return implode(':', $names);
63
    }
64
}
65