UpdateByPatchs::patchOldVersions()   B
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
rs 8.439
cc 5
eloc 20
nc 7
nop 1
1
<?php
2
3
namespace kujaff\VersionsBundle\Model;
4
5
use kujaff\VersionsBundle\Entity\BundleVersion;
6
use Symfony\Component\Finder\Finder;
7
use kujaff\VersionsBundle\Entity\Version;
8
use kujaff\VersionsBundle\Entity\Patch;
9
10
/**
11
 * Search for patchs in Patch
12
 * CurrentClass.php
13
 *     - Patch
14
 *         - Version_X_Y_Z
15
 *             - PatchYYYYmmDDhhIIss.php
16
 *         - Current
17
 *             - PatchYYYYmmDDhhIIss.php
18
 */
19
trait UpdateByPatchs
20
{
21
22
    /**
23
     * Sort dirs with version in name
24
     *
25
     * @param string $versionA
26
     * @param string  $versionB
27
     * @return int
28
     */
29
    private function compareDirVersion(&$versionA, &$versionB)
30
    {
31
        return $this->container->get('versions.version')->compare($versionA, $versionB);
0 ignored issues
show
Bug introduced by
The property container does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
    }
33
34
    /**
35
     * Find patch files in good order
36
     *
37
     * @param string $dir
38
     * @return array
39
     */
40
    protected function findPatchsFiles($dir)
41
    {
42
        if (is_dir($dir) === false) {
43
            return array();
44
        }
45
46
        $finderFiles = new Finder();
47
        $finderFiles->files();
48
        $finderFiles->in($dir);
49
        $finderFiles->name('Patch*.php');
50
        $finderFiles->sortByName();
51
        // we want first patch in first, but sortByName sort first patch in last
52
        $return = array();
53
        foreach ($finderFiles as $file) {
54
            $return = array_merge($return, array($file));
55
        }
56
        return $return;
57
    }
58
59
    /**
60
     * Call update
61
     *
62
     * @param string $className Fully qualified class name
63
     * @param BundleVersion $bundleVersion
64
     */
65
    protected function callUpdate($className, BundleVersion $bundleVersion)
66
    {
67
        $update = new $className();
68
        $update->update($bundleVersion);
69
    }
70
71
    /**
72
     * Try to patch all older versions, in Patch\Version_X_Y_Z dirs
73
     *
74
     * @param BundleVersion $bundleVersion
75
     * @return Version
76
     */
77
    protected function patchOldVersions(BundleVersion $bundleVersion)
78
    {
79
        $reflection = new \ReflectionClass(get_called_class());
80
        $fileInfos = new \SplFileInfo($reflection->getFileName());
81
        $patchPath = $fileInfos->getPath() . DIRECTORY_SEPARATOR . 'Patch';
82
83
        // directory doesn't exists, no patch to call
84
        if (is_dir($patchPath) === false) {
85
            return $bundleVersion->getInstalledVersion();
86
        }
87
88
        // dirs like Update\Version_X_Y_Z
89
        $finderVersions = new Finder();
90
        $finderVersions->directories()->in($patchPath)->name('Version_*')->sortByName();
91
        $dirsVersions = array();
92
        foreach ($finderVersions as $dir) {
93
            $dirsVersions[] = str_replace('_', '.', substr($dir->getFilename(), 8));
94
        }
95
        usort($dirsVersions, array($this, 'compareDirVersion'));
96
97
        // now that we have dirs in right order, let's find patch files
98
        $return = $bundleVersion->getInstalledVersion();
99
        foreach ($dirsVersions as $dir) {
100
            $files = $this->findPatchsFiles($patchPath . DIRECTORY_SEPARATOR . 'Version_' . str_replace('.', '_', $dir));
101
            foreach ($files as $file) {
102
                $className = $reflection->getNamespaceName() . '\\Patch\\Version_' . str_replace('.', '_', $dir) . '\\' . $file->getBasename('.' . $file->getExtension());
103
                $this->callUpdate($className, $bundleVersion);
104
            }
105
106
            $return = new Version($dir);
107
        }
108
109
        return $return;
110
    }
111
112
    /**
113
     * Try to patch current versions, in Patch\Current dir
114
     *
115
     * @param BundleVersion $bundleVersion
116
     * @return Version
117
     */
118
    protected function patchCurrentVersion(BundleVersion $bundleVersion)
119
    {
120
        $reflection = new \ReflectionClass(get_called_class());
121
        $fileInfos = new \SplFileInfo($reflection->getFileName());
122
        $manager = $this->container->get('doctrine')->getManager();
123
        $patchPath = $fileInfos->getPath() . DIRECTORY_SEPARATOR . 'Patch' . DIRECTORY_SEPARATOR . 'Current';
124
        $files = $this->findPatchsFiles($patchPath);
125
126
        foreach ($files as $file) {
127
            $className = $reflection->getNamespaceName() . '\\Patch\\Current\\' . $file->getBasename('.' . $file->getExtension());
128
            $this->callUpdate($className, $bundleVersion);
129
130
            // insert this patch into Patch, to know that we have already called it
131
            $patch = new Patch();
132
            $patch->setBundle($bundleVersion->getName());
133
            $patch->setDate(new \DateTime());
134
            $manager->persist($patch);
135
            $manager->flush();
136
        }
137
    }
138
139
    /**
140
     * Update bundle
141
     *
142
     * @param BundleVersion $bundleVersion
143
     * @return Version
144
     */
145
    public function update(BundleVersion $bundleVersion)
146
    {
147
        $return = $this->patchOldVersions($bundleVersion);
148
        $this->patchCurrentVersion($bundleVersion);
149
150
        return $return;
151
    }
152
}
153