LoadAliceFixtures   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 57
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B load() 0 21 5
B getAliceFiles() 0 22 4
A getAliceOptions() 0 4 1
A getAliceProviders() 0 4 1
1
<?php
2
3
namespace Knp\RadBundle\DataFixtures\ORM;
4
5
use Knp\RadBundle\DataFixtures\AbstractFixture;
6
7
use Symfony\Component\Finder\Finder;
8
9
use Doctrine\Common\Persistence\ObjectManager;
10
11
use Nelmio\Alice\Fixtures;
12
13
class LoadAliceFixtures extends AbstractFixture
14
{
15
    public function load(ObjectManager $manager)
16
    {
17
        $env     = $this->container->getParameter('kernel.environment');
18
        $bundles = $this->container->getParameter('kernel.bundles');
19
        if (!isset($bundles['App'])) {
20
            return;
21
        }
22
23
        $refl = new \ReflectionClass($bundles['App']);
24
        if (class_exists($customClass = $refl->getNamespaceName().'\\DataFixtures\\ORM\\LoadAliceFixtures') &&
25
            $customClass !== get_class($this)) {
26
            return;
27
        }
28
29
        $path = dirname($refl->getFileName()).DIRECTORY_SEPARATOR.'Resources'.
30
            DIRECTORY_SEPARATOR.'fixtures'.DIRECTORY_SEPARATOR.'orm';
31
32
        foreach ($this->getAliceFiles($path, $env) as $file) {
33
            Fixtures::load($file, $manager, $this->getAliceOptions());
34
        }
35
    }
36
37
    protected function getAliceFiles($path, $environment)
38
    {
39
        $paths = array();
40
        if (is_dir($path)) {
41
            $paths[] = $path;
42
        }
43
        if (is_dir($path.DIRECTORY_SEPARATOR.$environment)) {
44
            $paths[] = $path.DIRECTORY_SEPARATOR.$environment;
45
        }
46
47
        if (0 == count($paths)) {
48
            return array();
49
        }
50
51
        return Finder::create()
52
            ->files()
53
            ->name('*.yml')
54
            ->depth(0)
55
            ->sortByName()
56
            ->in($paths)
57
        ;
58
    }
59
60
    protected function getAliceOptions()
61
    {
62
        return array('providers' => $this->getAliceProviders());
63
    }
64
65
    protected function getAliceProviders()
66
    {
67
        return $this->container->get('knp_rad.alice.provider_collection')->getProviders();
68
    }
69
}
70