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

FileLoader::import()   D

Complexity

Conditions 11
Paths 142

Size

Total Lines 44
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 26.125

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 4.9629
c 0
b 0
f 0
ccs 14
cts 28
cp 0.5
cc 11
eloc 27
nc 142
nop 4
crap 26.125

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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