Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
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
    public function load(ObjectManager $manager)
28
    {
29
        $this->manager = $manager;
30
        $options = $this->getOptions();
31
32
        $parser = new Parser();
33
34
        $data = [];
35
        foreach ($this->getFixtures() as $fixture) {
36
            $data[] = $parser->parse(file_get_contents($fixture));
37
        }
38
39
        $fixtures = $this->initFixtures($data);
40
        $builder = $this->container->get('kunstmaan_fixtures.builder.builder');
41
        $locale = isset($options['locale']) ? $options['locale'] : 'en_US';
42
43
        foreach ($this->getProviders() as $provider) {
44
            $builder->addProvider($provider);
45
        }
46
47
        /*
48
         * because of faker's magic calls we'll want to add this as last provider
49
         */
50
        $builder->addProvider(Factory::create($locale));
51
        $builder->setFixtures($fixtures);
52
        $builder->buildFixtures($manager);
53
    }
54
55
    /**
56
     * Parse specs and initiate fixtures
57
     *
58
     * @param $data
59
     *
60
     * @return array|mixed
61
     */
62
    private function initFixtures($data)
63
    {
64
        $fixtures = [];
65
        $parser = $this->container->get('kunstmaan_fixtures.parser.parser');
66
67
        foreach ($data as $file) {
68
            foreach ($file as $class => $specs) {
69
                foreach ($specs as $name => $options) {
70
                    $fixture = new Fixture($name, $class, $options);
71
                    $fixtures = $parser->parseSpec($name, $fixture, $fixtures);
72
                }
73
            }
74
        }
75
76
        return $fixtures;
77
    }
78
79
    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...
80
81
    public function getOptions()
82
    {
83
        return ['locale' => 'en_US'];
84
    }
85
86
    public function getProviders()
87
    {
88
        return [$this];
89
    }
90
91
    /**
92
     * Sets the Container.
93
     *
94
     * @param ContainerInterface|null $container A ContainerInterface instance or null
95
     *
96
     * @api
97
     */
98
    public function setContainer(ContainerInterface $container = null)
99
    {
100
        $this->container = $container;
101
    }
102
}
103