PuliFileLocator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the puli/symfony-bridge package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Puli\SymfonyBridge\Config;
13
14
use InvalidArgumentException;
15
use Puli\Repository\Api\Resource\FilesystemResource;
16
use Puli\Repository\Api\ResourceNotFoundException;
17
use Puli\Repository\Api\ResourceRepository;
18
use Puli\Repository\Resource\LinkResource;
19
use RuntimeException;
20
21
/**
22
 * @since  1.0
23
 *
24
 * @author Bernhard Schussek <[email protected]>
25
 */
26
class PuliFileLocator implements ChainableFileLocator
27
{
28
    /**
29
     * @var ResourceRepository
30
     */
31
    private $repo;
32
33 8
    public function __construct(ResourceRepository $repo)
34
    {
35 8
        $this->repo = $repo;
36 8
    }
37
38 1
    public function supports($path)
39
    {
40 1
        return isset($path[0]) && '/' === $path[0];
41
    }
42
43
    /**
44
     * Returns a full path for a given Puli path.
45
     *
46
     * @param mixed  $path        The Puli path to locate
47
     * @param string $currentPath The current path
0 ignored issues
show
Documentation introduced by
Should the type for parameter $currentPath not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
48
     * @param bool   $first       Whether to return the first occurrence or
49
     *                            an array of file names
50
     *
51
     * @return string|array The full path to the file|An array of file paths
52
     *
53
     * @throws InvalidArgumentException When the path is not found
54
     */
55 7
    public function locate($path, $currentPath = null, $first = true)
56
    {
57
        // Accept actual file paths
58 7
        if (file_exists($path)) {
59 1
            return $path;
60
        }
61
62 6
        if (null !== $currentPath && file_exists($currentPath.'/'.$path)) {
63 1
            throw new RuntimeException(sprintf(
64
                'You tried to load the file "%s" using a relative path. '.
65
                'This functionality is not supported due to a limitation in '.
66
                'Symfony, because then this file cannot be overridden anymore. '.
67 1
                'Please pass the absolute Puli path instead.',
68
                $path
69
            ));
70
        }
71
72
        try {
73 5
            $resource = $this->repo->get($path);
74
75 4
            while ($resource instanceof LinkResource) {
76 1
                $resource = $this->repo->get($resource->getTargetPath());
77
            }
78
79 4
            if (!$resource instanceof FilesystemResource) {
80 1
                throw new InvalidArgumentException(sprintf(
81 1
                    'The file "%s" is not a local file.',
82
                    $path
83
                ));
84
            }
85
86 3
            return $first
87 2
                ? $resource->getFilesystemPath()
88 3
                : array($resource->getFilesystemPath());
89 2
        } catch (ResourceNotFoundException $e) {
90 1
            throw new InvalidArgumentException(sprintf(
91 1
                'The file "%s" could not be found.',
92
                $path
93 1
            ), 0, $e);
94
        }
95
    }
96
}
97