Completed
Push — master ( 3da805...48aa3a )
by Pavel
03:39
created

FileLoader   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 61.11%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 0
loc 88
rs 10
c 0
b 0
f 0
ccs 22
cts 36
cp 0.6111

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setCurrentDir() 0 4 1
D import() 0 44 11
A getLocator() 0 4 1
1
<?php
2
3
namespace Bankiru\Api\Rpc\Routing\Loader;
4
5
use Bankiru\Api\Rpc\Routing\Exception\CircularReferenceLoaderException;
6
use Bankiru\Api\Rpc\Routing\Exception\FileLoaderException;
7
use Bankiru\Api\Rpc\Routing\Exception\FileLoaderLoadException;
8
use Symfony\Component\Config\FileLocatorInterface;
9
10
abstract class FileLoader extends Loader
11
{
12
    /** @var array to track circular references */
13
    protected static $loading = [];
14
15
    /** @var  FileLocatorInterface */
16
    private $locator;
17
    /** @var  string */
18
    private $currentDir;
19
20 8
    public function __construct(FileLocatorInterface $locator)
21
    {
22 8
        $this->locator = $locator;
23 8
    }
24
25
    /**
26
     * @param string $currentDir
27
     */
28 8
    public function setCurrentDir($currentDir)
29
    {
30 8
        $this->currentDir = $currentDir;
31 8
    }
32
33
    /**
34
     * Imports a resource.
35
     *
36
     * @param mixed       $resource       A Resource
37
     * @param string|null $type           The resource type or null if unknown
38
     * @param bool        $ignoreErrors   Whether to ignore import errors or not
39
     * @param string|null $sourceResource The original resource importing the new resource
40
     *
41
     * @return mixed
42
     * @throws FileLoaderException
43
     * @throws CircularReferenceLoaderException
44
     */
45 8
    public function import($resource, $type = null, $ignoreErrors = false, $sourceResource = null)
46
    {
47
        try {
48 8
            $loader = $this->resolve($resource, $type);
49
50 8
            if ($loader instanceof self && null !== $this->currentDir) {
51
                $resource = $loader->getLocator()->locate($resource, $this->currentDir, false);
52
            }
53
54 8
            $resources = is_array($resource) ? $resource : [$resource];
55 8
            for ($i = 0; $i < $resourcesCount = count($resources); ++$i) {
56 8
                if (isset(self::$loading[$resources[$i]])) {
57
                    if ($i == $resourcesCount - 1) {
58
                        throw CircularReferenceLoaderException::fromPaths(array_keys(self::$loading));
59
                    }
60
                } else {
61 8
                    $resource = $resources[$i];
62 8
                    break;
63
                }
64
            }
65 8
            self::$loading[$resource] = true;
66
67
            try {
68 8
                $ret = $loader->load($resource, $type);
69 8
            } finally {
70 8
                unset(self::$loading[$resource]);
71 8
            }
72
73 8
            return $ret;
74
        } catch (CircularReferenceLoaderException $e) {
75
            throw $e;
76
        } catch (\Exception $e) {
77
            if (!$ignoreErrors) {
78
                // prevent embedded imports from nesting multiple exceptions
79
                if ($e instanceof FileLoaderException) {
80
                    throw $e;
81
                }
82
83
                throw new FileLoaderLoadException($resource, $sourceResource, null, $e);
84
            }
85
        }
86
87
        return null;
88
    }
89
90
    /**
91
     * @return FileLocatorInterface
92
     */
93 8
    public function getLocator()
94
    {
95 8
        return $this->locator;
96
    }
97
}
98