Completed
Push — master ( d401ab...1f0fb6 )
by Łukasz
02:47
created

Candidate::isAlreadyDeployed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Tworzenieweb\SqlProvisioner\Model;
4
5
/**
6
 * @author Luke Adamczewski
7
 * @package Tworzenieweb\SqlProvisioner\Model
8
 */
9
class Candidate
10
{
11
    const STATUS_QUEUED = 'QUEUED';
12
    const STATUS_PENDING = 'PENDING';
13
    const STATUS_ALREADY_DEPLOYED = 'ALREADY_DEPLOYED';
14
    const STATUS_HAS_SYNTAX_ERROR = 'HAS_SYNTAX_ERROR';
15
16
    const FILES_MASK = '/^\d{3,}\_.*\.sql$/';
17
18
    private static $supportedStates = [self::STATUS_PENDING, self::STATUS_QUEUED, self::STATUS_ALREADY_DEPLOYED, self::STATUS_HAS_SYNTAX_ERROR];
19
20
    /** @var string */
21
    private $name;
22
23
    /** @var string */
24
    private $content;
25
26
    /** @var string */
27
    private $status;
28
29
    /** @var integer */
30
    private $index;
31
32
    /** @var int */
33
    private $number;
34
35
    /** @var boolean */
36
    private $ignored;
37
38
39
    /**
40
     * @param string $name
41
     * @param string $content
42
     * @throws Exception
43
     */
44 5
    public function __construct($name, $content)
45
    {
46
47 5
        if (!preg_match(self::FILES_MASK, $name)) {
48
            throw Exception::wrongFilename($name);
49
        }
50
51 5
        $this->name = $name;
52 5
        $this->number = (int) explode('_', $name)[0];
53 5
        $this->content = $content;
54 5
        $this->status = self::STATUS_PENDING;
55 5
        $this->ignored = false;
56 5
    }
57
58
59
    /**
60
     * @return string
61
     */
62 3
    public function getName()
63
    {
64 3
        return $this->name;
65
    }
66
67
68
    /**
69
     * @return void
70
     */
71 1
    public function markAsQueued()
72
    {
73 1
        $this->changeState(self::STATUS_QUEUED);
74 1
    }
75
76
77
78
    /**
79
     * @param string $causeStatus
80
     */
81 1
    public function markAsIgnored($causeStatus)
82
    {
83 1
        $this->ignored = true;
84 1
        $this->changeState($causeStatus);
85 1
        $this->content = null;
86 1
    }
87
88
89
90
    /**
91
     * @return string
92
     */
93
    public function getStatus()
94
    {
95
        return $this->status;
96
    }
97
98
99
100
    /**
101
     * @param int $index
102
     */
103
    public function setIndex($index)
104
    {
105
        $this->index = (int) $index;
106
    }
107
108
109
110
    /**
111
     * @return string
112
     */
113 3
    public function getContent()
114
    {
115 3
        return $this->content;
116
    }
117
118
119
120
    /**
121
     * @return int
122
     */
123
    public function getIndex()
124
    {
125
        return $this->index;
126
    }
127
128
129
130
    /**
131
     * @return int
132
     */
133 1
    public function getNumber()
134
    {
135 1
        return $this->number;
136
    }
137
138
139
    /**
140
     * @return bool
141
     */
142 3
    public function isIgnored()
143
    {
144 3
        return $this->ignored;
145
    }
146
147
    /**
148
     * @return bool
149
     */
150 1
    public function isQueued()
151
    {
152 1
        return $this->status === self::STATUS_QUEUED;
153
    }
154
155
    /**
156
     * @return bool
157
     */
158 1
    public function isPending()
159
    {
160 1
        return $this->status === self::STATUS_PENDING;
161
    }
162
163
    /**
164
     * @return bool
165
     */
166 1
    public function isAlreadyDeployed()
167
    {
168 1
        return $this->status === self::STATUS_ALREADY_DEPLOYED;
169
    }
170
171
    /**
172
     * @param string $newState
173
     */
174 2
    private function changeState($newState)
175
    {
176 2
        if (!in_array($newState, self::$supportedStates)) {
177
            throw Exception::unsupportedCandidateState($newState, self::$supportedStates);
178
        }
179
180 2
        $this->status = $newState;
181 2
    }
182
}
183