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
35:16
created

FixturesFinder::resolveFixtures()   C

Complexity

Conditions 9
Paths 16

Size

Total Lines 49
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 9

Importance

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