Passed
Pull Request — master (#44)
by Ronan
10:09
created

Project::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace App\Model;
4
5
use App\Facades\Provider;
6
use App\Model\Finder\ProjectFinder;
7
use Respect\Validation\Validator;
8
use Ronanchilvers\Orm\Model;
9
use Ronanchilvers\Orm\Orm;
10
use Ronanchilvers\Orm\Traits\HasValidationTrait;
11
use Ronanchilvers\Utility\Str;
12
13
/**
14
 * Model representing a project
15
 *
16
 * @property int id
17
 * @property string name
18
 * @property string token
19
 * @property string key
20
 * @property string provider
21
 * @property string repository
22
 * @property string branch
23
 * @property string status
24
 * @property int last_number
25
 * @property null|\Carbon\Carbon last_deployment
26
 * @property string last_author
27
 * @property string last_sha
28
 * @property string last_status
29
 * @property null|\Carbon\Carbon created
30
 * @property null|\Carbon\Carbon updated
31
 * @author Ronan Chilvers <[email protected]>
32
 */
33
class Project extends Model
34
{
35
    use HasValidationTrait;
36
37
    static protected $finder       = ProjectFinder::class;
38
    static protected $columnPrefix = 'project';
39
40
    protected $data = [
41
        'project_branch' => 'master',
42
        'project_status' => 'active',
43
    ];
44
45
    /**
46
     * Boot the model
47
     *
48
     * @author Ronan Chilvers <[email protected]>
49
     */
50
    protected function boot()
51
    {
52
        $this->addType('datetime', 'last_deployment');
53
    }
54
55
    /**
56
     * @author Ronan Chilvers <[email protected]>
57
     */
58
    protected function setupValidation()
59
    {
60
        $providerKeys = array_keys(Provider::getOptions());
61
        $this->registerRules([
62
            'name'       => Validator::notEmpty(),
63
            'provider'   => Validator::notEmpty()->in($providerKeys),
64
            'repository' => Validator::notEmpty(),
65
            'branch'     => Validator::notEmpty(),
66
            'status'     => Validator::notEmpty()->in(['active', 'deploying']),
67
        ]);
68
    }
69
70
    /**
71
     * @author Ronan Chilvers <[email protected]>
72
     */
73
    protected function beforeCreate()
74
    {
75
        if (empty($this->token)) {
76
            $this->token = Str::token(64);
77
        }
78
        $key = preg_replace('#[^A-z0-9\-]#', '-', $this->name);
79
        $key = preg_replace('#[-]{2,}#', '-', $key);
80
        $key = strtolower($key);
81
        if (!Orm::finder(get_called_class())->keyIsUnique($key)) {
82
            $this->addError('name', 'Name must be unique');
83
            return false;
84
        }
85
        $this->key = $key;
86
        $this->status = 'active';
87
    }
88
89
    /**
90
     * Update the project data from a deployment
91
     *
92
     * @param \App\Model\Deployment $deployment
93
     * @return bool
94
     * @author Ronan Chilvers <[email protected]>
95
     */
96
    public function updateFromDeployment(Deployment $deployment)
97
    {
98
        $this->last_status = $deployment->status;
99
        if ($deployment->isDeployed()) {
100
            $this->last_number     = $deployment->number;
101
            $this->last_deployment = $deployment->finished;
102
            $this->last_sha        = $deployment->sha;
103
            $this->last_author     = $deployment->author;
104
        }
105
    }
106
107
    /**
108
     * Mark this project as deploying
109
     *
110
     * @return boolean
111
     * @author Ronan Chilvers <[email protected]>
112
     */
113
    public function markDeploying()
114
    {
115
        $this->status = 'deploying';
116
117
        return $this->save();
118
    }
119
120
    /**
121
     * Mark this project as active
122
     *
123
     * @return boolean
124
     * @author Ronan Chilvers <[email protected]>
125
     */
126
    public function markActive()
127
    {
128
        $this->status = 'active';
129
130
        return $this->save();
131
    }
132
133
    /**
134
     * Is this project deployable?
135
     *
136
     * @author Ronan Chilvers <[email protected]>
137
     */
138
    public function isDeployable()
139
    {
140
        return 'active' == $this->status;
141
    }
142
}
143