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

ChainLoader::find()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
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