MigrationManager   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 136
Duplicated Lines 42.65 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 2 Features 3
Metric Value
c 6
b 2
f 3
dl 58
loc 136
wmc 15
lcom 1
cbo 5
ccs 48
cts 48
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 3
A migrate() 0 10 3
A getKnownVersions() 0 4 1
B up() 29 29 4
B down() 29 29 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the webmozart/json package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
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 Webmozart\Json\Migration;
13
14
use stdClass;
15
use Webmozart\Assert\Assert;
16
use Webmozart\Json\Versioning\JsonVersioner;
17
use Webmozart\Json\Versioning\SchemaUriVersioner;
18
19
/**
20
 * Migrates a JSON object between different versions.
21
 *
22
 * The JSON object is expected to have the property "version" set.
23
 *
24
 * @since  1.3
25
 *
26
 * @author Bernhard Schussek <[email protected]>
27
 */
28
class MigrationManager
29
{
30
    /**
31
     * @var JsonVersioner
32
     */
33
    private $versioner;
34
35
    /**
36
     * @var JsonMigration[]
37
     */
38
    private $migrationsBySourceVersion = array();
39
40
    /**
41
     * @var JsonMigration[]
42
     */
43
    private $migrationsByTargetVersion = array();
44
45
    /**
46
     * @var string[]
47
     */
48
    private $knownVersions = array();
49
50
    /**
51
     * Creates a new migration manager.
52
     *
53
     * @param JsonMigration[]    $migrations The migrations migrating a JSON
54
     *                                       object between individual versions
55
     * @param JsonVersioner|null $versioner  The versioner that should be used
56
     */
57 11
    public function __construct(array $migrations, JsonVersioner $versioner = null)
58
    {
59 11
        Assert::allIsInstanceOf($migrations, __NAMESPACE__.'\JsonMigration');
60
61 11
        $this->versioner = $versioner ?: new SchemaUriVersioner();
62
63 11
        foreach ($migrations as $migration) {
64 11
            $this->migrationsBySourceVersion[$migration->getSourceVersion()] = $migration;
65 11
            $this->migrationsByTargetVersion[$migration->getTargetVersion()] = $migration;
66 11
            $this->knownVersions[] = $migration->getSourceVersion();
67 11
            $this->knownVersions[] = $migration->getTargetVersion();
68
        }
69
70 11
        $this->knownVersions = array_unique($this->knownVersions);
71
72 11
        uksort($this->migrationsBySourceVersion, 'version_compare');
73 11
        uksort($this->migrationsByTargetVersion, 'version_compare');
74 11
        usort($this->knownVersions, 'version_compare');
75 11
    }
76
77
    /**
78
     * Migrates a JSON object to the given version.
79
     *
80
     * @param stdClass $data          The JSON object
81
     * @param string   $targetVersion The version string
82
     */
83 9
    public function migrate(stdClass $data, $targetVersion)
84
    {
85 9
        $sourceVersion = $this->versioner->parseVersion($data);
86
87 9
        if (version_compare($targetVersion, $sourceVersion, '>')) {
88 4
            $this->up($data, $sourceVersion, $targetVersion);
89 5
        } elseif (version_compare($targetVersion, $sourceVersion, '<')) {
90 4
            $this->down($data, $sourceVersion, $targetVersion);
91
        }
92 5
    }
93
94
    /**
95
     * Returns all versions known to the manager.
96
     *
97
     * @return string[] The known version strings
98
     */
99 2
    public function getKnownVersions()
100
    {
101 2
        return $this->knownVersions;
102
    }
103
104 4 View Code Duplication
    private function up($data, $sourceVersion, $targetVersion)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106 4
        while (version_compare($sourceVersion, $targetVersion, '<')) {
107 4
            if (!isset($this->migrationsBySourceVersion[$sourceVersion])) {
108 1
                throw new MigrationFailedException(sprintf(
109 1
                    'No migration found to upgrade from version %s to %s.',
110
                    $sourceVersion,
111
                    $targetVersion
112
                ));
113
            }
114
115 3
            $migration = $this->migrationsBySourceVersion[$sourceVersion];
116
117
            // Final version too high
118 3
            if (version_compare($migration->getTargetVersion(), $targetVersion, '>')) {
119 1
                throw new MigrationFailedException(sprintf(
120 1
                    'No migration found to upgrade from version %s to %s.',
121
                    $sourceVersion,
122
                    $targetVersion
123
                ));
124
            }
125
126 3
            $migration->up($data);
127
128 3
            $this->versioner->updateVersion($data, $migration->getTargetVersion());
129
130 3
            $sourceVersion = $migration->getTargetVersion();
131
        }
132 2
    }
133
134 4 View Code Duplication
    private function down($data, $sourceVersion, $targetVersion)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135
    {
136 4
        while (version_compare($sourceVersion, $targetVersion, '>')) {
137 4
            if (!isset($this->migrationsByTargetVersion[$sourceVersion])) {
138 1
                throw new MigrationFailedException(sprintf(
139 1
                    'No migration found to downgrade from version %s to %s.',
140
                    $sourceVersion,
141
                    $targetVersion
142
                ));
143
            }
144
145 3
            $migration = $this->migrationsByTargetVersion[$sourceVersion];
146
147
            // Final version too low
148 3
            if (version_compare($migration->getSourceVersion(), $targetVersion, '<')) {
149 1
                throw new MigrationFailedException(sprintf(
150 1
                    'No migration found to downgrade from version %s to %s.',
151
                    $sourceVersion,
152
                    $targetVersion
153
                ));
154
            }
155
156 3
            $migration->down($data);
157
158 3
            $this->versioner->updateVersion($data, $migration->getSourceVersion());
159
160 3
            $sourceVersion = $migration->getSourceVersion();
161
        }
162 2
    }
163
}
164