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