Passed
Push — webtestcase-extract-fixtures-m... ( 642655...e6c5e5 )
by Alexis
08:55 queued 01:03
created

FixturesLoader::loadFixtureClass()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 9.2
cc 4
eloc 9
nc 4
nop 2
crap 4
1
<?php
2
3
/*
4
 * This file is part of the Liip/FunctionalTestBundle
5
 *
6
 * (c) Lukas Kahwe Smith <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Liip\FunctionalTestBundle\Utils;
13
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
16
use Nelmio\Alice\Fixtures;
17
use Symfony\Bundle\DoctrineFixturesBundle\Common\DataFixtures\Loader;
18
19
class FixturesLoader
20
{
21
    /**
22
     * This function finds the time when the data blocks of a class definition
23
     * file were being written to, that is, the time when the content of the
24
     * file was changed.
25
     *
26
     * @param string $class The fully qualified class name of the fixture class to
27
     *                      check modification date on.
28
     *
29
     * @return \DateTime|null
30
     */
31 3
    protected static function getFixtureLastModified($class)
32
    {
33 3
        $lastModifiedDateTime = null;
34
35 3
        $reflClass = new \ReflectionClass($class);
36 3
        $classFileName = $reflClass->getFileName();
37
38 3
        if (file_exists($classFileName)) {
39 3
            $lastModifiedDateTime = new \DateTime();
40 3
            $lastModifiedDateTime->setTimestamp(filemtime($classFileName));
41 3
        }
42
43 3
        return $lastModifiedDateTime;
44
    }
45
46
    /**
47
     * Determine if the Fixtures that define a database backup have been
48
     * modified since the backup was made.
49
     *
50
     * @param array  $classNames The fixture classnames to check
51
     * @param string $backup     The fixture backup SQLite database file path
52
     *
53
     * @return bool TRUE if the backup was made since the modifications to the
54
     *              fixtures; FALSE otherwise
55
     */
56 7
    public static function isBackupUpToDate(array $classNames, $backup, $container)
57
    {
58 7
        $backupLastModifiedDateTime = new \DateTime();
59 7
        $backupLastModifiedDateTime->setTimestamp(filemtime($backup));
60
61
        /** @var \Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader $loader */
62 7
        $loader = self::getFixtureLoader($container, $classNames);
63
64
        // Use loader in order to fetch all the dependencies fixtures.
65 7
        foreach ($loader->getFixtures() as $className) {
66 3
            $fixtureLastModifiedDateTime = self::getFixtureLastModified($className);
67 3
            if ($backupLastModifiedDateTime < $fixtureLastModifiedDateTime) {
68 1
                return false;
69
            }
70 7
        }
71
72 7
        return true;
73
    }
74
75
    /**
76
     * Locate fixture files.
77
     *
78
     * @param array $paths
79
     *
80
     * @return array $files
81
     */
82 6
    public static function locateResources($paths, $container)
83
    {
84 6
        $files = array();
85
86 6
        $kernel = $container->get('kernel');
87
88 6
        foreach ($paths as $path) {
89 6
            if ($path[0] !== '@' && file_exists($path) === true) {
90 1
                $files[] = $path;
91 1
                continue;
92
            }
93
94 5
            $files[] = $kernel->locateResource($path);
95 6
        }
96
97 6
        return $files;
98
    }
99
100
    /**
101
     * Retrieve Doctrine DataFixtures loader.
102
     *
103
     * @param ContainerInterface $container
104
     * @param array              $classNames
105
     *
106
     * @return Loader
107
     */
108 32
    public static function getFixtureLoader(ContainerInterface $container, array $classNames)
109
    {
110 32
        $loaderClass = class_exists('Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader')
111 32
            ? 'Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader'
112 32
            : (class_exists('Doctrine\Bundle\FixturesBundle\Common\DataFixtures\Loader')
113
                // This class is not available during tests.
114
                // @codeCoverageIgnoreStart
115
                ? 'Doctrine\Bundle\FixturesBundle\Common\DataFixtures\Loader'
116
                // @codeCoverageIgnoreEnd
117 32
                : 'Symfony\Bundle\DoctrineFixturesBundle\Common\DataFixtures\Loader');
118
119 32
        $loader = new $loaderClass($container);
120
121 32
        foreach ($classNames as $className) {
122 10
            self::loadFixtureClass($loader, $className);
123 32
        }
124
125 32
        return $loader;
126
    }
127
128
    /**
129
     * Load a data fixture class.
130
     *
131
     * @param Loader $loader
132
     * @param string $className
133
     */
134 10
    protected static function loadFixtureClass($loader, $className)
135
    {
136 10
        $fixture = new $className();
137
138 10
        if ($loader->hasFixture($fixture)) {
139 2
            unset($fixture);
140
141 2
            return;
142
        }
143
144 10
        $loader->addFixture($fixture);
145
146 10
        if ($fixture instanceof DependentFixtureInterface) {
147 2
            foreach ($fixture->getDependencies() as $dependency) {
148 2
                self::loadFixtureClass($loader, $dependency);
149 2
            }
150 2
        }
151 10
    }
152
}
153