Completed
Push — master ( ab8175...d69783 )
by Kamil
15s
created

SuiteFactory::createSuite()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 3
eloc 9
nc 4
nop 2
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\FixturesBundle\Suite;
13
14
use Sylius\Bundle\FixturesBundle\Fixture\FixtureRegistryInterface;
15
use Sylius\Bundle\FixturesBundle\Listener\ListenerRegistryInterface;
16
use Symfony\Component\Config\Definition\Processor;
17
use Webmozart\Assert\Assert;
18
19
/**
20
 * @author Kamil Kokot <[email protected]>
21
 */
22
final class SuiteFactory implements SuiteFactoryInterface
23
{
24
    /**
25
     * @var FixtureRegistryInterface
26
     */
27
    private $fixtureRegistry;
28
29
    /**
30
     * @var ListenerRegistryInterface
31
     */
32
    private $listenerRegistry;
33
34
    /**
35
     * @var Processor
36
     */
37
    private $optionsProcessor;
38
39
    /**
40
     * @param FixtureRegistryInterface $fixtureRegistry
41
     * @param ListenerRegistryInterface $listenerRegistry
42
     * @param Processor $optionsProcessor
43
     */
44
    public function __construct(
45
        FixtureRegistryInterface $fixtureRegistry,
46
        ListenerRegistryInterface $listenerRegistry,
47
        Processor $optionsProcessor
48
    ) {
49
        $this->fixtureRegistry = $fixtureRegistry;
50
        $this->listenerRegistry = $listenerRegistry;
51
        $this->optionsProcessor = $optionsProcessor;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function createSuite($name, array $configuration)
58
    {
59
        Assert::keyExists($configuration, 'fixtures');
60
        Assert::keyExists($configuration, 'listeners');
61
62
        $suite = new Suite($name);
63
64
        foreach ($configuration['fixtures'] as $fixtureName => $fixtureAttributes) {
65
            $this->addFixtureToSuite($suite, $fixtureName, $fixtureAttributes);
66
        }
67
68
        foreach ($configuration['listeners'] as $listenerName => $listenerAttributes) {
69
            $this->addListenerToSuite($suite, $listenerName, $listenerAttributes);
70
        }
71
72
        return $suite;
73
    }
74
75
    /**
76
     * @param Suite $suite
77
     * @param string $fixtureName
78
     * @param array $fixtureAttributes
79
     */
80
    private function addFixtureToSuite(Suite $suite, $fixtureName, array $fixtureAttributes)
81
    {
82
        Assert::keyExists($fixtureAttributes, 'options');
83
84
        $fixture = $this->fixtureRegistry->getFixture($fixtureName);
85
        $fixtureOptions = $this->optionsProcessor->processConfiguration($fixture, $fixtureAttributes['options']);
86
        $fixturePriority = isset($fixtureAttributes['priority']) ? $fixtureAttributes['priority'] : 0;
87
88
        $suite->addFixture($fixture, $fixtureOptions, $fixturePriority);
89
    }
90
91
    /**
92
     * @param Suite $suite
93
     * @param string $listenerName
94
     * @param array $listenerAttributes
95
     */
96
    private function addListenerToSuite(Suite $suite, $listenerName, array $listenerAttributes)
97
    {
98
        Assert::keyExists($listenerAttributes, 'options');
99
100
        $listener = $this->listenerRegistry->getListener($listenerName);
101
        $listenerOptions = $this->optionsProcessor->processConfiguration($listener, $listenerAttributes['options']);
102
        $listenerPriority = isset($listenerAttributes['priority']) ? $listenerAttributes['priority'] : 0;
103
104
        $suite->addListener($listener, $listenerOptions, $listenerPriority);
105
    }
106
}
107