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 (#218)
by Eric
12:08
created

FixturesFinder::resolveFixtures()   C

Complexity

Conditions 9
Paths 16

Size

Total Lines 49
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 14.9754

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 49
ccs 18
cts 31
cp 0.5806
rs 5.7446
cc 9
eloc 26
nc 16
nop 2
crap 14.9754
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
            $fixtures = array_merge($fixtures, $this->getFixturesFromDirectory($path));
48 135
        }
49
50 135
        if (0 === count($fixtures)) {
51
            throw new \InvalidArgumentException(
52
                sprintf('Could not find any fixtures to load in: %s', "\n\n- ".implode("\n- ", $loadersPaths))
53
            );
54
        }
55
56
        // Get real fixtures path
57
        // Note: Fixtures returned are guaranteed to be unique here
58 135
        return $this->resolveFixtures($kernel, $fixtures);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 156
    public function resolveFixtures(KernelInterface $kernel, array $fixtures)
65
    {
66 156
        $resolvedFixtures = [];
67
68
        // Get real fixtures path
69 156
        foreach ($fixtures as $index => $fixture) {
70 156
            if ($fixture instanceof \SplFileInfo) {
71 18
                $filePath = $fixture->getRealPath();
72
73 18
                if (false === $filePath) {
74
                    throw new \RuntimeException(
75
                        sprintf(
76
                            'The file %s pointed by a %s instance was not found.',
77
                            (string) $fixture,
78
                            get_class($fixture)
79
                        )
80
                    );
81
                }
82 18
                $fixture = $filePath;
83 18
            }
84
85 156
            if (false === is_string($fixture)) {
86
                throw new \InvalidArgumentException(
87
                    'Expected fixtures passed to be either strings or a SplFileInfo instances.'
88
                );
89
            }
90
91 156
            if ('@' === $fixture[0]) {
92
                // If $kernel fails to resolve the resource, will throw a \InvalidArgumentException exception
93 51
                $realPath = $kernel->locateResource($fixture, null, true);
94 51
            } else {
95 144
                $realPath = realpath($fixture);
96
            }
97
98 156
            if (false === $realPath || false === file_exists($realPath)) {
99
                throw new \InvalidArgumentException(sprintf('The file "%s" was not found', $fixture));
100
            }
101
102 156
            if (false === is_file($realPath)) {
103
                throw new \InvalidArgumentException(
104
                    sprintf('Expected "%s to be a fixture file, got a directory instead.', $fixture)
105
                );
106
            }
107
108 156
            $resolvedFixtures[$realPath] = true;
109 156
        }
110
111 156
        return array_keys($resolvedFixtures);
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117 96
    public function getFixturesFromDirectory($path)
118
    {
119 96
        $fixtures = [];
120
121 96
        $finder = SymfonyFinder::create()->in($path)->depth(0)->files()->name('*.yml');
122 96
        foreach ($finder as $file) {
123
            /* @var SplFileInfo $file */
124 96
            $fixtures[$file->getRealPath()] = true;
125 96
        }
126
127 96
        return array_keys($fixtures);
128
    }
129
130
    /**
131
     * Gets paths to directories containing loaders and fixtures files.
132
     *
133
     * @param BundleInterface[] $bundles
134
     * @param string            $environment
135
     *
136
     * @return string[] Real paths to loaders.
137
     */
138 147
    protected function getLoadersPaths(array $bundles, $environment)
139
    {
140
        $environments = [
141 147
            lcfirst($environment) => true,
142 147
            ucfirst($environment) => true,
143 147
        ];
144
145 147
        $paths = [];
146 147
        foreach ($bundles as $bundle) {
147 147
            $path = sprintf('%s/%s', $bundle->getPath(), $this->bundleFixturesPath);
148 147
            if (true === file_exists($path)) {
149 147
                $paths[$path] = true;
150
                try {
151 147
                    $files = SymfonyFinder::create()->directories()->in($path);
152 144
                    foreach ($files as $file) {
153
                        /** @var SplFileInfo $file */
154 117
                        if (true === isset($environments[$file->getRelativePathname()])) {
155 93
                            $paths[$file->getRealPath()] = true;
156 93
                        }
157 144
                    }
158 147
                } catch (\InvalidArgumentException $exception) {
159
                }
160 147
            }
161 147
        }
162
163 147
        return array_keys($paths);
164
    }
165
}
166