Passed
Pull Request — master (#19)
by Ronan
07:19
created

Deployment::relateOriginal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Model;
4
5
use App\Model\Event;
6
use App\Model\Finder\DeploymentFinder;
7
use App\Model\Project;
8
use Carbon\Carbon;
9
use Respect\Validation\Validator;
10
use Ronanchilvers\Orm\Model;
11
use Ronanchilvers\Orm\Traits\HasValidationTrait;
12
13
/**
14
 * Model representing a project deployment
15
 *
16
 * @property int id
17
 * @property \App\Model\Project project
18
 * @property int number
19
 * @property \App\Model\Deployment|null original
20
 * @property string sha
21
 * @property string author
22
 * @property string message
23
 * @property string configuration
24
 * @property string status
25
 * @property \Carbon\Carbon|null started
26
 * @property \Carbon\Carbon|null finished
27
 * @property \Carbon\Carbon|null failed
28
 * @property \Carbon\Carbon|null created
29
 * @property \Carbon\Carbon|null updated
30
 * @property string source
31
 * @property string committer
32
 * @author Ronan Chilvers <[email protected]>
33
 */
34
class Deployment extends Model
35
{
36
    use HasValidationTrait;
37
38
    static protected $finder       = DeploymentFinder::class;
39
    static protected $columnPrefix = 'deployment';
40
41
    protected $data = [
42
        'deployment_status' => 'pending'
43
    ];
44
45
    /**
46
     * Boot the model
47
     *
48
     * @author Ronan Chilvers <[email protected]>
49
     */
50
    protected function boot()
51
    {
52
        $this->addType('datetime', 'started');
53
        $this->addType('datetime', 'finished');
54
        $this->addType('datetime', 'failed');
55
        $this->addType('model', 'project', [
56
            'class' => Project::class
57
        ]);
58
        $this->addType('model', 'original', [
59
            'class' => static::class
60
        ]);
61
    }
62
63
    /**
64
     * @author Ronan Chilvers <[email protected]>
65
     */
66
    protected function clone()
67
    {
68
        $this->status   = 'pending';
69
        $this->started  = null;
70
        $this->finished = null;
71
        $this->failed   = null;
72
    }
73
74
    /**
75
     * @author Ronan Chilvers <[email protected]>
76
     */
77
    protected function setupValidation()
78
    {
79
        $this->registerRules([
80
            'project' => Validator::notEmpty()->intVal()->min(1),
81
            'hash'    => Validator::stringType(),
82
            'status'  => Validator::notEmpty()->in(['pending', 'deploying', 'deployed', 'failed']),
83
            'type'    => Validator::notEmpty()->in(['deployment', 'reactivation']),
84
        ]);
85
    }
86
87
    /**
88
     * Relationship with project
89
     *
90
     * @return \App\Model\Project
91
     * @author Ronan Chilvers <[email protected]>
92
     */
93
    protected function relateProject()
94
    {
95
        return $this->belongsTo(
96
            Project::class
97
98
        );
99
    }
100
101
    /**
102
     * Relate events to this deployment
103
     *
104
     * @return array
105
     * @author Ronan Chilvers <[email protected]>
106
     */
107
    protected function relateEvents()
108
    {
109
        return $this->hasMany(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->hasMany(App\Model\Event::class) returns the type Ronanchilvers\Orm\Featur...Ronanchilvers\Orm\Model which is incompatible with the documented return type array.
Loading history...
110
            Event::class
111
        );
112
    }
113
114
    /**
115
     * Relate original deployment to this one for reactivations
116
     *
117
     * @return \App\Model\Deployment|null
118
     * @author Ronan Chilvers <[email protected]>
119
     */
120
    // protected function relateOriginal()
121
    // {
122
    //     return $this->belongsTo(
123
    //         Deployment::class,
124
    //         'original'
125
    //     );
126
    // }
127
128
    /**
129
     * Start the deployment
130
     *
131
     * @author Ronan Chilvers <[email protected]>
132
     */
133
    public function start()
134
    {
135
        $this->status  = 'deploying';
136
        $this->started = Carbon::now();
137
138
        return $this->save();
139
    }
140
141
    /**
142
     * Finish the deployment
143
     *
144
     * @author Ronan Chilvers <[email protected]>
145
     */
146
    public function finish()
147
    {
148
        $this->status   = 'deployed';
149
        $this->finished = Carbon::now();
150
151
        return $this->save();
152
    }
153
154
    /**
155
     * Mark the deployment as failed
156
     *
157
     * @author Ronan Chilvers <[email protected]>
158
     */
159
    public function fail()
160
    {
161
        $this->status = 'failed';
162
        $this->failed = Carbon::now();
163
164
        return $this->save();
165
    }
166
167
    /**
168
     * Is this a full deployment?
169
     *
170
     * @return boolean
171
     * @author Ronan Chilvers <[email protected]>
172
     */
173
    public function isReactivation()
174
    {
175
        return 0 < $this->getAttributeRaw('original');
176
    }
177
178
    /**
179
     * Is the deployment deployed?
180
     *
181
     * @return boolean
182
     * @author Ronan Chilvers <[email protected]>
183
     */
184
    public function isDeployed()
185
    {
186
        return 'deployed' == $this->status;
187
    }
188
189
    /**
190
     * Is the deployment deploying?
191
     *
192
     * @return boolean
193
     * @author Ronan Chilvers <[email protected]>
194
     */
195
    public function isDeploying()
196
    {
197
        return 'deploying' == $this->status;
198
    }
199
200
    /**
201
     * Is the deployment pending?
202
     *
203
     * @return boolean
204
     * @author Ronan Chilvers <[email protected]>
205
     */
206
    public function isPending()
207
    {
208
        return 'pending' == $this->status;
209
    }
210
211
    /**
212
     * Is the deployment failed?
213
     *
214
     * @return boolean
215
     * @author Ronan Chilvers <[email protected]>
216
     */
217
    public function isFailed()
218
    {
219
        return 'failed' == $this->status;
220
    }
221
222
    /**
223
     * Get the duration in seconds for this deployment
224
     *
225
     * @return int
226
     * @author Ronan Chilvers <[email protected]>
227
     */
228
    public function getDuration()
229
    {
230
        if ('deployed' != $this->status) {
231
            return null;
232
        }
233
        $finished = $this->finished;
234
        if ($finished instanceof Carbon) {
235
            return $this->finished->diffInSeconds($this->started);
0 ignored issues
show
Bug introduced by
The method diffInSeconds() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

235
            return $this->finished->/** @scrutinizer ignore-call */ diffInSeconds($this->started);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
236
        }
237
238
        return null;
239
    }
240
}
241