Completed
Push — master ( f34924...c8c8f9 )
by Paweł
13:31
created

AbstractResourceFixture::getConfigTreeBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Bundle\CoreBundle\Fixture;
13
14
use Doctrine\Common\Persistence\ObjectManager;
15
use Sylius\Bundle\CoreBundle\Fixture\Factory\ExampleFactoryInterface;
16
use Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface;
17
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
18
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
19
use Symfony\Component\OptionsResolver\Options;
20
use Symfony\Component\OptionsResolver\OptionsResolver;
21
22
/**
23
 * @author Kamil Kokot <[email protected]>
24
 */
25
abstract class AbstractResourceFixture implements FixtureInterface
26
{
27
    /**
28
     * @var ObjectManager
29
     */
30
    private $objectManager;
31
32
    /**
33
     * @var ExampleFactoryInterface
34
     */
35
    private $exampleFactory;
36
37
    /**
38
     * @var OptionsResolver
39
     */
40
    private $optionsResolver;
41
42
    /**
43
     * @param ObjectManager $objectManager
44
     * @param ExampleFactoryInterface $exampleFactory
45
     */
46
    public function __construct(ObjectManager $objectManager, ExampleFactoryInterface $exampleFactory)
47
    {
48
        $this->objectManager = $objectManager;
49
        $this->exampleFactory = $exampleFactory;
50
51
        $this->optionsResolver =
52
            (new OptionsResolver())
53
                ->setDefault('random', 0)
54
                ->setAllowedTypes('random', 'int')
55
                ->setDefault('prototype', [])
56
                ->setAllowedTypes('prototype', 'array')
57
                ->setDefault('custom', [])
58
                ->setAllowedTypes('custom', 'array')
59
                ->setNormalizer('custom', function (Options $options, array $custom) {
60
                    if ($options['random'] <= 0) {
61
                        return $custom;
62
                    }
63
64
                    return array_merge($custom, array_fill(0, $options['random'], $options['prototype']));
65
                })
66
        ;
67
    }
68
69
    /**
70
     * @param array $options
71
     */
72
    final public function load(array $options)
73
    {
74
        $options = $this->optionsResolver->resolve($options);
75
76
        $i = 0;
77
        foreach ($options['custom'] as $resourceOptions) {
78
            $resource = $this->exampleFactory->create($resourceOptions);
79
80
            $this->objectManager->persist($resource);
81
82
            ++$i;
83
84
            if (0 === ($i % 10)) {
85
                $this->objectManager->flush();
86
                $this->objectManager->clear();
87
            }
88
        }
89
90
        $this->objectManager->flush();
91
        $this->objectManager->clear();
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    final public function getConfigTreeBuilder()
98
    {
99
        $treeBuilder = new TreeBuilder();
100
        $optionsNode = $treeBuilder->root($this->getName());
101
102
        $optionsNode->children()->integerNode('random')->min(0)->defaultValue(0);
103
104
        /** @var ArrayNodeDefinition $resourcesNode */
105
        $resourcesNode = $optionsNode->children()->arrayNode('custom');
106
107
        /** @var ArrayNodeDefinition $resourceNode */
108
        $resourceNode = $resourcesNode->requiresAtLeastOneElement()->prototype('array');
109
        $this->configureResourceNode($resourceNode);
110
111
        return $treeBuilder;
112
    }
113
114
    /**
115
     * @param ArrayNodeDefinition $resourceNode
116
     */
117
    protected function configureResourceNode(ArrayNodeDefinition $resourceNode)
118
    {
119
        // empty
120
    }
121
}
122