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::getDataLoadersFromDirectory()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 4
Bugs 2 Features 1
Metric Value
c 4
b 2
f 1
dl 0
loc 32
ccs 9
cts 9
cp 1
rs 8.439
cc 6
eloc 17
nc 10
nop 1
crap 6
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\Doctrine\Finder;
13
14
use Hautelook\AliceBundle\Doctrine\DataFixtures\LoaderInterface;
15
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
use Symfony\Component\Finder\Finder as SymfonyFinder;
18
use Symfony\Component\Finder\SplFileInfo;
19
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
20
21
/**
22
 * Extends its parent class to take into account doctrine data loaders.
23
 *
24
 * @author Théo FIDRY <[email protected]>
25
 */
26
class FixturesFinder extends \Hautelook\AliceBundle\Finder\FixturesFinder implements ContainerAwareInterface
27
{
28
    /**
29
     * @var ContainerInterface|null
30
     */
31
    private $container;
32 78
33
    /**
34 78
     * {@inheritdoc}
35
     */
36 78
    public function setContainer(ContainerInterface $container = null)
37 78
    {
38 63
        $this->container = $container;
39 78
    }
40
41
    /**
42 78
     * {@inheritdoc}
43 54
     *
44
     * Extended to look for data loaders. If a data loader is found, will take the fixtures from it instead of taking
45
     * all the fixtures files.
46 63
     */
47
    public function getFixturesFromDirectory($path)
48
    {
49
        $fixtures = [];
50
51
        $loaders = $this->getDataLoadersFromDirectory($path);
52
        foreach ($loaders as $loader) {
53
            $fixtures = array_merge($fixtures, $loader->getFixtures());
54
        }
55
56
        // If no data loader is found, takes all fixtures files
57
        if (0 === count($loaders)) {
58
            return parent::getFixturesFromDirectory($path);
59 51
        }
60
61 51
        return $fixtures;
62
    }
63
64 51
    /**
65 51
     * Gets all data loaders instances.
66 51
     *
67 51
     * For first get all the path for where to look for data loaders.
68
     *
69 51
     * @param BundleInterface[] $bundles
70
     * @param string            $environment
71
     *
72
     * @return LoaderInterface[] Fixtures files real paths.
73
     */
74
    public function getDataLoaders(array $bundles, $environment)
75
    {
76
        $loadersPaths = $this->getLoadersPaths($bundles, $environment);
77
78
        // Add all fixtures to the new Doctrine loader
79 87
        $loaders = [];
80
        foreach ($loadersPaths as $path) {
81 87
            $loaders = array_merge($loaders, $this->getDataLoadersFromDirectory($path));
82
        }
83
84 87
        return $loaders;
85 87
    }
86 87
87
    /**
88 69
     * Get data loaders inside the given directory.
89 69
     *
90 87
     * @param string $path Directory path
91
     *
92
     * @return LoaderInterface[]
93 87
     */
94 87
    private function getDataLoadersFromDirectory($path)
95 87
    {
96
        $loaders = [];
97 87
98 69
        // Get all PHP classes in given folder
99 69
        $phpClasses = [];
100 69
        $finder = SymfonyFinder::create()->depth(0)->in($path)->files()->name('*.php');
101 69
        foreach ($finder as $file) {
102 87
            /* @var SplFileInfo $file */
103
            $phpClasses[$file->getRealPath()] = true;
104 87
            require_once $file->getRealPath();
105
        }
106
107
        // Check if PHP classes are data loaders or not
108
        foreach (get_declared_classes() as $className) {
109
            $reflectionClass = new \ReflectionClass($className);
110
            $sourceFile = $reflectionClass->getFileName();
111
112
            if (true === isset($phpClasses[$sourceFile])) {
113
                if ($reflectionClass->implementsInterface('Hautelook\AliceBundle\Doctrine\DataFixtures\LoaderInterface')) {
114
                    $loader = new $className();
115
                    $loaders[$className] = $loader;
116
117
                    if ($loader instanceof ContainerAwareInterface) {
118
                        $loader->setContainer($this->container);
119
                    }
120
                }
121
            }
122
        }
123
124
        return $loaders;
125
    }
126
}
127