Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

Kunstmaan/FixturesBundle/Loader/FixtureLoader.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\FixturesBundle\Loader;
4
5
use Doctrine\Common\DataFixtures\FixtureInterface;
6
use Doctrine\Common\Persistence\ObjectManager;
7
use Faker\Factory;
8
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
use Symfony\Component\Yaml\Parser;
11
12
abstract class FixtureLoader implements FixtureInterface, ContainerAwareInterface
13
{
14
    /**
15
     * @var ObjectManager
16
     */
17
    protected $manager;
18
19
    /**
20
     * @var ContainerInterface
21
     */
22
    protected $container;
23
24
    /**
25
     * Load data fixtures with the passed EntityManager
26
     *
27
     * @param ObjectManager $manager
28
     */
29
    public function load(ObjectManager $manager)
30
    {
31
        $this->manager = $manager;
32
        $options = $this->getOptions();
33
34
        $parser = new Parser();
35
36
        $data = [];
37
        foreach ($this->getFixtures() as $fixture) {
38
            $data[] = $parser->parse(file_get_contents($fixture));
39
        }
40
41
        $fixtures = $this->initFixtures($data);
42
        $builder = $this->container->get('kunstmaan_fixtures.builder.builder');
43
        $locale = isset($options['locale']) ? $options['locale'] : 'en_US';
44
45
        foreach ($this->getProviders() as $provider) {
46
            $builder->addProvider($provider);
47
        }
48
49
        /*
50
         * because of faker's magic calls we'll want to add this as last provider
51
         */
52
        $builder->addProvider(Factory::create($locale));
53
        $builder->setFixtures($fixtures);
54
        $builder->buildFixtures($manager);
55
    }
56
57
    /**
58
     * Parse specs and initiate fixtures
59
     *
60
     * @param $data
61
     *
62
     * @return array|mixed
63
     */
64
    private function initFixtures($data)
65
    {
66
        $fixtures = [];
67
        $parser = $this->container->get('kunstmaan_fixtures.parser.parser');
68
69
        foreach ($data as $file) {
70
            foreach ($file as $class => $specs) {
71
                foreach ($specs as $name => $options) {
72
                    $fixture = new Fixture($name, $class, $options);
73
                    $fixtures = $parser->parseSpec($name, $fixture, $fixtures);
74
                }
75
            }
76
        }
77
78
        return $fixtures;
79
    }
80
81
    abstract protected function getFixtures();
0 ignored issues
show
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
82
83
    public function getOptions()
84
    {
85
        return ['locale' => 'en_US'];
86
    }
87
88
    public function getProviders()
89
    {
90
        return [$this];
91
    }
92
93
    /**
94
     * Sets the Container.
95
     *
96
     * @param ContainerInterface|null $container A ContainerInterface instance or null
97
     *
98
     * @api
99
     */
100
    public function setContainer(ContainerInterface $container = null)
101
    {
102
        $this->container = $container;
103
    }
104
}
105