NameParser::isCreate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace Salah3id\Domains\Repository\Generators\Migrations;
3
4
/**
5
 * Class NameParser
6
 * @package Salah3id\Domains\Repository\Generators\Migrations
7
 * @author Anderson Andrade <[email protected]>
8
 */
9
class NameParser
10
{
11
    /**
12
     * The migration name.
13
     *
14
     * @var string
15
     */
16
    protected $name;
17
    /**
18
     * The array data.
19
     *
20
     * @var array
21
     */
22
    protected $data = [];
23
    /**
24
     * The available schema actions.
25
     *
26
     * @var array
27
     */
28
    protected $actions = [
29
        'create' => [
30
            'create',
31
            'make'
32
        ],
33
        'delete' => [
34
            'delete',
35
            'remove'
36
        ],
37
        'add'    => [
38
            'add',
39
            'update',
40
            'append',
41
            'insert'
42
        ],
43
        'drop'   => [
44
            'destroy',
45
            'drop'
46
        ]
47
    ];
48
49
    /**
50
     * The constructor.
51
     *
52
     * @param string $name
53
     */
54
    public function __construct($name)
55
    {
56
        $this->name = $name;
57
        $this->data = $this->fetchData();
58
    }
59
60
    /**
61
     * Fetch the migration name to an array data.
62
     *
63
     * @return array
64
     */
65
    protected function fetchData()
66
    {
67
        return explode('_', $this->name);
68
    }
69
70
    /**
71
     * Get original migration name.
72
     *
73
     * @return string
74
     */
75
    public function getOriginalName()
76
    {
77
        return $this->name;
78
    }
79
80
    /**
81
     * Get table name.
82
     *
83
     * @return string
84
     */
85
    public function getTable()
86
    {
87
        return $this->getTableName();
88
    }
89
90
    /**
91
     * Get the table will be used.
92
     *
93
     * @return string
94
     */
95
    public function getTableName()
96
    {
97
        $matches = array_reverse($this->getMatches());
98
99
        return array_shift($matches);
100
    }
101
102
    /**
103
     * Get matches data from regex.
104
     *
105
     * @return array
106
     */
107
    public function getMatches()
108
    {
109
        preg_match($this->getPattern(), $this->name, $matches);
110
111
        return $matches;
112
    }
113
114
    /**
115
     * Get name pattern.
116
     *
117
     * @return string
118
     */
119
    public function getPattern()
120
    {
121
        switch ($action = $this->getAction()) {
122
            case 'add':
123
            case 'append':
124
            case 'update':
125
            case 'insert':
126
                return "/{$action}_(.*)_to_(.*)_table/";
127
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
128
129
            case 'delete':
130
            case 'remove':
131
            case 'alter':
132
                return "/{$action}_(.*)_from_(.*)_table/";
133
                break;
134
            default:
135
                return "/{$action}_(.*)_table/";
136
                break;
137
        }
138
    }
139
140
    /**
141
     * Get schema type or action.
142
     *
143
     * @return string
144
     */
145
    public function getAction()
146
    {
147
        return head($this->data);
148
    }
149
150
    /**
151
     * Get the array data.
152
     *
153
     * @return array
154
     */
155
    public function getData()
156
    {
157
        return $this->data;
158
    }
159
160
    /**
161
     * Determine whether the given type is same with the current schema action or type.
162
     *
163
     * @param $type
164
     *
165
     * @return bool
166
     */
167
    public function is($type)
168
    {
169
        return $type == $this->getAction();
170
    }
171
172
    /**
173
     * Determine whether the current schema action is a adding action.
174
     *
175
     * @return bool
176
     */
177
    public function isAdd()
178
    {
179
        return in_array($this->getAction(), $this->actions['add']);
180
    }
181
182
    /**
183
     * Determine whether the current schema action is a deleting action.
184
     *
185
     * @return bool
186
     */
187
    public function isDelete()
188
    {
189
        return in_array($this->getAction(), $this->actions['delete']);
190
    }
191
192
    /**
193
     * Determine whether the current schema action is a creating action.
194
     *
195
     * @return bool
196
     */
197
    public function isCreate()
198
    {
199
        return in_array($this->getAction(), $this->actions['create']);
200
    }
201
202
    /**
203
     * Determine whether the current schema action is a dropping action.
204
     *
205
     * @return bool
206
     */
207
    public function isDrop()
208
    {
209
        return in_array($this->getAction(), $this->actions['drop']);
210
    }
211
}
212