Completed
Push — master ( 4c7b9c...e919dc )
by Kamil
21:11
created

MongoDBPurgerListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 50
rs 10
c 1
b 0
f 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A beforeSuite() 0 10 2
A getName() 0 4 1
A configureOptionsNode() 0 9 1
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\Listener;
13
14
use Doctrine\Common\DataFixtures\Purger\MongoDBPurger;
15
use Doctrine\Common\Persistence\ManagerRegistry;
16
use Doctrine\ODM\MongoDB\DocumentManager;
17
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
18
19
/**
20
 * @author Kamil Kokot <[email protected]>
21
 */
22
final class MongoDBPurgerListener extends AbstractListener implements BeforeSuiteListenerInterface
23
{
24
    /**
25
     * @var ManagerRegistry
26
     */
27
    private $managerRegistry;
28
29
    /**
30
     * @param ManagerRegistry $managerRegistry
31
     */
32
    public function __construct(ManagerRegistry $managerRegistry)
33
    {
34
        $this->managerRegistry = $managerRegistry;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function beforeSuite(SuiteEvent $suiteEvent, array $options)
41
    {
42
        foreach ($options['managers'] as $managerName) {
43
            /** @var DocumentManager $manager */
44
            $manager = $this->managerRegistry->getManager($managerName);
45
46
            $purger = new MongoDBPurger($manager);
47
            $purger->purge();
48
        }
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getName()
55
    {
56
        return 'mongodb_purger';
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    protected function configureOptionsNode(ArrayNodeDefinition $optionsNode)
63
    {
64
        $optionsNode
65
            ->children()
66
                ->arrayNode('managers')
67
                    ->defaultValue([null])
68
                    ->prototype('scalar')
69
        ;
70
    }
71
}
72