NameParser::isAdd()   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
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Rawilk\LaravelModules\Support\Migrations;
4
5
class NameParser
6
{
7
    /** @var array */
8
    protected static $actions = [
9
        'add'    => ['add', 'update', 'append', 'insert'],
10
        'create' => ['create', 'make'],
11
        'delete' => ['delete', 'remove'],
12
        'drop'   => ['destroy', 'drop']
13
    ];
14
15
    /** @var array */
16
    protected $data = [];
17
18
    /** @var string */
19
    protected $name;
20
21
    /**
22
     * @param string $name
23
     */
24
    public function __construct(string $name)
25
    {
26
        $this->name = $name;
27
        $this->data = $this->fetchData();
28
    }
29
30
    public function getAction(): string
31
    {
32
        return head($this->data);
33
    }
34
35
    public function getData(): array
36
    {
37
        return $this->data;
38
    }
39
40
    public function getMatches(): array
41
    {
42
        preg_match($this->getPattern(), $this->name, $matches);
43
44
        return $matches;
45
    }
46
47
    public function getOriginalName(): string
48
    {
49
        return $this->name;
50
    }
51
52
    public function getPattern(): string
53
    {
54
        switch ($action = $this->getAction()) {
55
            case 'add':
56
            case 'append':
57
            case 'update':
58
            case 'insert':
59
                return "/{$action}_(.*)_to_(.*)_table/";
60
            case 'delete':
61
            case 'remove':
62
            case 'alter':
63
                return "/{$action}_(.*)_from_(.*)_table/";
64
            default:
65
                return "/{$action}_(.*)_table/";
66
        }
67
    }
68
69
    public function getTableName(): string
70
    {
71
        $matches = array_reverse($this->getMatches());
72
73
        return array_shift($matches);
74
    }
75
76
    public function is(string $type): bool
77
    {
78
        return $type === $this->getAction();
79
    }
80
81
    public function isAdd(): bool
82
    {
83
        return in_array($this->getAction(), static::$actions['add'], true);
84
    }
85
86
    public function isCreate(): bool
87
    {
88
        return in_array($this->getAction(), static::$actions['create'], true);
89
    }
90
91
    public function isDelete(): bool
92
    {
93
        return in_array($this->getAction(), static::$actions['delete'], true);
94
    }
95
96
    public function isDrop(): bool
97
    {
98
        return in_array($this->getAction(), static::$actions['drop'], true);
99
    }
100
101
    protected function fetchData(): array
102
    {
103
        return explode('_', $this->name);
104
    }
105
}
106