Completed
Pull Request — master (#1087)
by Cas
01:49
created

LegacyLocator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 49
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B generate() 0 25 3
A locate() 0 14 2
1
<?php
2
/**
3
 * Phinx
4
 *
5
 * (The MIT license)
6
 * Copyright (c) 2015 Rob Morgan
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated * documentation files (the "Software"), to
10
 * deal in the Software without restriction, including without limitation the
11
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12
 * sell copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24
 * IN THE SOFTWARE.
25
 *
26
 * @package    Phinx
27
 * @subpackage Phinx\Migration\Locator
28
 */
29
namespace Phinx\Migration\Locator;
30
31
use Phinx\Migration\MigrationDefinition;
32
use Phinx\Util\Util;
33
34
/**
35
 * BC locator to support existing migrations
36
 *
37
 * @author Cas Leentfaar
38
 */
39
class LegacyLocator implements LocatorInterface
40
{
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function generate($targetDir, $name = null)
45
    {
46
        $version = Util::getCurrentTimestamp();
47
        $className = $name;
48
49
        if (!Util::isValidPhinxClassName($className)) {
50
            throw new \InvalidArgumentException(sprintf(
51
                'The migration class name "%s" is invalid. Please use CamelCase format.',
52
                $className
53
            ));
54
        }
55
56
        if (!Util::isUniqueMigrationClassName($className, $targetDir)) {
57
            throw new \InvalidArgumentException(sprintf(
58
                'The migration class name "%s" already exists',
59
                $className
60
            ));
61
        }
62
63
        // Compute the file path
64
        $fileName = Util::mapClassNameToFileName($className);
65
        $filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName;
66
67
        return new MigrationDefinition($version, $className, $filePath, $name);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function locate($filePath)
74
    {
75
        $fileName = basename($filePath);
76
77
        if (Util::isValidMigrationFileName($fileName)) {
78
            $version = Util::getVersionFromFileName($fileName);
79
            $name = substr($fileName, strpos($fileName, '_') + 1);
80
81
            // convert the filename to a class name
82
            $class = Util::mapFileNameToClassName($fileName);
83
84
            return new MigrationDefinition($version, $class, $filePath, $name);
85
        }
86
    }
87
}
88