UpdateDataFixturesFixture   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 47
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setDataFixtures() 0 4 1
B load() 0 24 5
1
<?php
2
3
namespace RDV\Bundle\MigrationBundle\Migration;
4
5
use Doctrine\Common\DataFixtures\AbstractFixture;
6
use Doctrine\Common\Persistence\ObjectManager;
7
8
use RDV\Bundle\MigrationBundle\Entity\DataFixture;
9
10
class UpdateDataFixturesFixture extends AbstractFixture
11
{
12
    /**
13
     * @var array
14
     *  key - class name
15
     *  value - current loaded version
16
     */
17
    protected $dataFixturesClassNames;
18
19
    /**
20
     * Set a list of data fixtures to be updated
21
     *
22
     * @param array $classNames
23
     */
24
    public function setDataFixtures($classNames)
25
    {
26
        $this->dataFixturesClassNames = $classNames;
27
    }
28
29
    /**
30
     * @inheritdoc
31
     */
32
    public function load(ObjectManager $manager)
33
    {
34
        if (!empty($this->dataFixturesClassNames)) {
35
            $loadedAt = new \DateTime('now', new \DateTimeZone('UTC'));
36
            foreach ($this->dataFixturesClassNames as $className => $version) {
37
                $dataFixture = null;
38
                if ($version !== null) {
39
                    $dataFixture = $manager
40
                        ->getRepository('RdvMigrationBundle:DataFixture')
41
                        ->findOneBy(['className' => $className]);
42
                }
43
                if (!$dataFixture) {
44
                    $dataFixture = new DataFixture();
45
                    $dataFixture->setClassName($className);
46
                }
47
48
                $dataFixture
49
                    ->setVersion($version)
50
                    ->setLoadedAt($loadedAt);
51
                $manager->persist($dataFixture);
52
            }
53
            $manager->flush();
54
        }
55
    }
56
}
57