Issues (21)

src/Model/Deployment.php (2 issues)

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_author'        => '',
43
        'deployment_committer'     => '',
44
        'deployment_configuration' => '',
45
        'deployment_message'       => '',
46
        'deployment_sha'           => '',
47
        'deployment_status'        => 'pending',
48
    ];
49
50
    /**
51
     * Boot the model
52
     *
53
     * @author Ronan Chilvers <[email protected]>
54
     */
55
    protected function boot()
56
    {
57
        $this->addType('datetime', 'started');
58
        $this->addType('datetime', 'finished');
59
        $this->addType('datetime', 'failed');
60
        $this->addType('model', 'project', [
61
            'class' => Project::class
62
        ]);
63
        $this->addType('model', 'original', [
64
            'class' => static::class
65
        ]);
66
    }
67
68
    /**
69
     * @author Ronan Chilvers <[email protected]>
70
     */
71
    protected function clone()
72
    {
73
        $this->status   = 'pending';
74
        $this->started  = null;
75
        $this->finished = null;
76
        $this->failed   = null;
77
    }
78
79
    /**
80
     * @author Ronan Chilvers <[email protected]>
81
     */
82
    protected function setupValidation()
83
    {
84
        $this->registerRules([
85
            'project' => Validator::notEmpty()->intVal()->min(1),
86
            'hash'    => Validator::stringType(),
87
            'status'  => Validator::notEmpty()->in(['pending', 'deploying', 'deployed', 'failed']),
88
            'type'    => Validator::notEmpty()->in(['deployment', 'reactivation']),
89
        ]);
90
    }
91
92
    /**
93
     * Relationship with project
94
     *
95
     * @return \App\Model\Project
96
     * @author Ronan Chilvers <[email protected]>
97
     */
98
    // protected function relateProject()
99
    // {
100
    //     return $this->belongsTo(
101
    //         Project::class
102
    //     );
103
    // }
104
105
    /**
106
     * Relate events to this deployment
107
     *
108
     * @return array
109
     * @author Ronan Chilvers <[email protected]>
110
     */
111
    protected function relateEvents()
112
    {
113
        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\Model which is incompatible with the documented return type array.
Loading history...
114
            Event::class
115
        );
116
    }
117
118
    /**
119
     * Relate original deployment to this one for reactivations
120
     *
121
     * @return \App\Model\Deployment|null
122
     * @author Ronan Chilvers <[email protected]>
123
     */
124
    // protected function relateOriginal()
125
    // {
126
    //     return $this->belongsTo(
127
    //         Deployment::class,
128
    //         'original'
129
    //     );
130
    // }
131
132
    /**
133
     * Start the deployment
134
     *
135
     * @author Ronan Chilvers <[email protected]>
136
     */
137
    public function start()
138
    {
139
        $this->status  = 'deploying';
140
        $this->started = Carbon::now();
141
142
        return $this->save();
143
    }
144
145
    /**
146
     * Finish the deployment
147
     *
148
     * @author Ronan Chilvers <[email protected]>
149
     */
150
    public function finish()
151
    {
152
        $this->status   = 'deployed';
153
        $this->finished = Carbon::now();
154
155
        return $this->save();
156
    }
157
158
    /**
159
     * Mark the deployment as failed
160
     *
161
     * @author Ronan Chilvers <[email protected]>
162
     */
163
    public function fail()
164
    {
165
        $this->status = 'failed';
166
        $this->failed = Carbon::now();
167
168
        return $this->save();
169
    }
170
171
    /**
172
     * Is this a full deployment?
173
     *
174
     * @return boolean
175
     * @author Ronan Chilvers <[email protected]>
176
     */
177
    public function isReactivation()
178
    {
179
        return 0 < $this->getAttributeRaw('original');
180
    }
181
182
    /**
183
     * Is the deployment deployed?
184
     *
185
     * @return boolean
186
     * @author Ronan Chilvers <[email protected]>
187
     */
188
    public function isDeployed()
189
    {
190
        return 'deployed' == $this->status;
191
    }
192
193
    /**
194
     * Is the deployment deploying?
195
     *
196
     * @return boolean
197
     * @author Ronan Chilvers <[email protected]>
198
     */
199
    public function isDeploying()
200
    {
201
        return 'deploying' == $this->status;
202
    }
203
204
    /**
205
     * Is the deployment pending?
206
     *
207
     * @return boolean
208
     * @author Ronan Chilvers <[email protected]>
209
     */
210
    public function isPending()
211
    {
212
        return 'pending' == $this->status;
213
    }
214
215
    /**
216
     * Is the deployment failed?
217
     *
218
     * @return boolean
219
     * @author Ronan Chilvers <[email protected]>
220
     */
221
    public function isFailed()
222
    {
223
        return 'failed' == $this->status;
224
    }
225
226
    /**
227
     * Get the duration in seconds for this deployment
228
     *
229
     * @return int
230
     * @author Ronan Chilvers <[email protected]>
231
     */
232
    public function getDuration()
233
    {
234
        if ('deployed' != $this->status) {
235
            return null;
236
        }
237
        $finished = $this->finished;
238
        if ($finished instanceof Carbon) {
239
            return $this->finished->diffInSeconds($this->started);
0 ignored issues
show
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

239
            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...
240
        }
241
242
        return null;
243
    }
244
}
245