AbstractResourceFixture   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 2
A load() 0 20 3
A configureResourceNode() 0 2 1
A getConfigTreeBuilder() 0 17 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Setono\SyliusSchedulerPlugin\Fixture;
6
7
use Doctrine\Common\Persistence\ObjectManager;
8
use Sylius\Bundle\CoreBundle\Fixture\Factory\ExampleFactoryInterface;
9
use Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface;
10
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
11
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
12
use Symfony\Component\OptionsResolver\Options;
13
use Symfony\Component\OptionsResolver\OptionsResolver;
14
15
abstract class AbstractResourceFixture implements FixtureInterface
16
{
17
    /**
18
     * @var ObjectManager
19
     */
20
    private $objectManager;
21
22
    /**
23
     * @var ExampleFactoryInterface
24
     */
25
    private $exampleFactory;
26
27
    /**
28
     * @var OptionsResolver
29
     */
30
    private $optionsResolver;
31
32
    /**
33
     * @param ObjectManager $objectManager
34
     * @param ExampleFactoryInterface $exampleFactory
35
     */
36
    public function __construct(ObjectManager $objectManager, ExampleFactoryInterface $exampleFactory)
37
    {
38
        $this->objectManager = $objectManager;
39
        $this->exampleFactory = $exampleFactory;
40
41
        $this->optionsResolver =
42
            (new OptionsResolver())
43
                ->setDefault('random', 0)
44
                ->setAllowedTypes('random', 'int')
45
                ->setDefault('prototype', [])
46
                ->setAllowedTypes('prototype', 'array')
47
                ->setDefault('custom', [])
48
                ->setAllowedTypes('custom', 'array')
49
                ->setNormalizer('custom', function (Options $options, array $custom) {
50
                    if ($options['random'] <= 0) {
51
                        return $custom;
52
                    }
53
54
                    return array_merge($custom, array_fill(0, $options['random'], $options['prototype']));
55
                })
56
        ;
57
    }
58
59
    /**
60
     * @param array $options
61
     */
62
    final public function load(array $options): void
63
    {
64
        $options = $this->optionsResolver->resolve($options);
65
66
        $i = 0;
67
        foreach ($options['custom'] as $resourceOptions) {
68
            $resource = $this->exampleFactory->create($resourceOptions);
69
70
            $this->objectManager->persist($resource);
71
72
            ++$i;
73
74
            if (0 === ($i % 10)) {
75
                $this->objectManager->flush();
76
                $this->objectManager->clear();
77
            }
78
        }
79
80
        $this->objectManager->flush();
81
        $this->objectManager->clear();
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    final public function getConfigTreeBuilder(): TreeBuilder
88
    {
89
        $treeBuilder = new TreeBuilder();
90
91
        /** @var ArrayNodeDefinition $optionsNode */
92
        $optionsNode = $treeBuilder->root($this->getName());
93
        $optionsNode->children()->integerNode('random')->min(0)->defaultValue(0);
94
        $optionsNode->children()->variableNode('prototype');
95
96
        /** @var ArrayNodeDefinition $resourcesNode */
97
        $resourcesNode = $optionsNode->children()->arrayNode('custom');
98
99
        /** @var ArrayNodeDefinition $resourceNode */
100
        $resourceNode = $resourcesNode->requiresAtLeastOneElement()->arrayPrototype();
101
        $this->configureResourceNode($resourceNode);
102
103
        return $treeBuilder;
104
    }
105
106
    /**
107
     * @param ArrayNodeDefinition $resourceNode
108
     */
109
    protected function configureResourceNode(ArrayNodeDefinition $resourceNode): void
0 ignored issues
show
Unused Code introduced by
The parameter $resourceNode is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

109
    protected function configureResourceNode(/** @scrutinizer ignore-unused */ ArrayNodeDefinition $resourceNode): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
    {
111
        // empty
112
    }
113
}
114