GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — 1.x (#257)
by Théo
107:31 queued 72:30
created

FixturesFinder   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 77.46%

Importance

Changes 0
Metric Value
wmc 24
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 153
ccs 55
cts 71
cp 0.7746
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getFixtures() 0 24 4
C resolveFixtures() 0 49 9
A getFixturesFromDirectory() 0 12 2
C getLoadersPaths() 0 32 8
1
<?php
2
3
/*
4
 * This file is part of the Hautelook\AliceBundle package.
5
 *
6
 * (c) Baldur Rensch <[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 Hautelook\AliceBundle\Finder;
13
14
use Symfony\Component\Finder\Finder as SymfonyFinder;
15
use Symfony\Component\Finder\SplFileInfo;
16
use Symfony\Component\HttpKernel\KernelInterface;
17
18
/**
19
 * @author Théo FIDRY <[email protected]>
20
 */
21
class FixturesFinder implements FixturesFinderInterface
22
{
23
    /**
24
     * @var string
25
     */
26
    private $bundleFixturesPath;
27
28
    /**
29
     * @param string $bundleFixturesPath Path in which fixtures files or loaders are expected to be found.
30
     */
31
    public function __construct($bundleFixturesPath)
32 210
    {
33
        $this->bundleFixturesPath = $bundleFixturesPath;
34 210
    }
35 210
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getFixtures(KernelInterface $kernel, array $bundles, $environment)
40 138
    {
41
        $loadersPaths = $this->getLoadersPaths($bundles, $environment);
42 138
43
        // Add all fixtures to the new Doctrine loader
44
        $fixtures = [];
45 138
        foreach ($loadersPaths as $path) {
46 138
            if (is_file($path)) {
47 138
                $fixtures[] = $path;
48 135
            } else {
49
                $fixtures = array_merge($fixtures, $this->getFixturesFromDirectory($path));
50 135
            }
51
        }
52
53
        if (0 === count($fixtures)) {
54
            throw new \InvalidArgumentException(
55
                sprintf('Could not find any fixtures to load in: %s', "\n\n- ".implode("\n- ", $loadersPaths))
56
            );
57
        }
58 135
59
        // Get real fixtures path
60
        // Note: Fixtures returned are guaranteed to be unique here
61
        return $this->resolveFixtures($kernel, $fixtures);
62
    }
63
64 156
    /**
65
     * {@inheritdoc}
66 156
     */
67
    public function resolveFixtures(KernelInterface $kernel, array $fixtures)
68
    {
69 156
        $resolvedFixtures = [];
70 156
71 18
        // Get real fixtures path
72
        foreach ($fixtures as $index => $fixture) {
73 18
            if ($fixture instanceof \SplFileInfo) {
74
                $filePath = $fixture->getRealPath();
75
76
                if (false === $filePath) {
77
                    throw new \RuntimeException(
78
                        sprintf(
79
                            'The file %s pointed by a %s instance was not found.',
80
                            (string) $fixture,
81
                            get_class($fixture)
82 18
                        )
83 18
                    );
84
                }
85 156
                $fixture = $filePath;
86
            }
87
88
            if (false === is_string($fixture)) {
89
                throw new \InvalidArgumentException(
90
                    'Expected fixtures passed to be either strings or a SplFileInfo instances.'
91 156
                );
92
            }
93 51
94 51
            if ('@' === $fixture[0]) {
95 144
                // If $kernel fails to resolve the resource, will throw a \InvalidArgumentException exception
96
                $realPath = $kernel->locateResource($fixture, null, true);
97
            } else {
98 156
                $realPath = realpath($fixture);
99
            }
100
101
            if (false === $realPath || false === file_exists($realPath)) {
102 156
                throw new \InvalidArgumentException(sprintf('The file "%s" was not found', $fixture));
103
            }
104
105
            if (false === is_file($realPath)) {
106
                throw new \InvalidArgumentException(
107
                    sprintf('Expected "%s to be a fixture file, got a directory instead.', $fixture)
108 156
                );
109 156
            }
110
111 156
            $resolvedFixtures[$realPath] = true;
112
        }
113
114
        return array_keys($resolvedFixtures);
115
    }
116
117 96
    /**
118
     * {@inheritdoc}
119 96
     */
120
    public function getFixturesFromDirectory($path)
121 96
    {
122 96
        $fixtures = [];
123
124 96
        $finder = SymfonyFinder::create()->in($path)->depth(0)->files()->name('*.yml')->name('*.php');
125 96
        foreach ($finder as $file) {
126
            /* @var SplFileInfo $file */
127 96
            $fixtures[$file->getRealPath()] = true;
128
        }
129
130
        return array_keys($fixtures);
131
    }
132
133
    /**
134
     * Gets paths to directories containing loaders and fixtures files.
135
     *
136
     * @param array<string, BundleInterface> $bundles
137
     * @param string            $environment
138 147
     *
139
     * @return string[] Real paths to loaders.
140
     */
141 147
    protected function getLoadersPaths(array $bundles, $environment)
142 147
    {
143 147
        $environments = [
144
            lcfirst($environment) => true,
145 147
            ucfirst($environment) => true,
146 147
        ];
147 147
148 147
        $paths = [];
149 147
        foreach ($bundles as $bundle) {
150
            if (is_string($bundle) && file_exists($bundle)) {
151 147
                    $paths[$bundle] = true;
152 144
                    continue;
153
            }
154 117
155 93
            $path = sprintf('%s/%s', $bundle->getPath(), $this->bundleFixturesPath);
156 93
            if (true === file_exists($path)) {
157 144
                $paths[$path] = true;
158 147
                try {
159
                    $files = SymfonyFinder::create()->directories()->in($path);
160 147
                    foreach ($files as $file) {
161 147
                        /** @var SplFileInfo $file */
162
                        if (true === isset($environments[$file->getRelativePathname()])) {
163 147
                            $paths[$file->getRealPath()] = true;
164
                        }
165
                    }
166
                } catch (\InvalidArgumentException $exception) {
167
                }
168
            }
169
        }
170
171
        return array_keys($paths);
172
    }
173
}
174