Project   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 12
eloc 40
c 7
b 0
f 0
dl 0
loc 121
ccs 0
cts 45
cp 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeCreate() 0 14 3
A markActive() 0 5 1
A beforeSave() 0 4 2
A markDeploying() 0 5 1
A updateFromDeployment() 0 8 2
A boot() 0 3 1
A setupValidation() 0 10 1
A isDeployable() 0 3 1
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
    const DEFAULT_KEEP_DEPLOYMENTS = 5;
38
39
    static protected $finder       = ProjectFinder::class;
40
    static protected $columnPrefix = 'project';
41
42
    protected $data = [
43
        'project_branch' => 'master',
44
        'project_status' => 'active',
45
    ];
46
47
    /**
48
     * Boot the model
49
     *
50
     * @author Ronan Chilvers <[email protected]>
51
     */
52
    protected function boot()
53
    {
54
        $this->addType('datetime', 'last_deployment');
55
    }
56
57
    /**
58
     * @author Ronan Chilvers <[email protected]>
59
     */
60
    protected function setupValidation()
61
    {
62
        $providerKeys = array_keys(Provider::getOptions());
63
        $this->registerRules([
64
            'name'             => Validator::notEmpty(),
65
            'provider'         => Validator::notEmpty()->in($providerKeys),
66
            'repository'       => Validator::notEmpty(),
67
            'branch'           => Validator::notEmpty(),
68
            'status'           => Validator::notEmpty()->in(['active', 'deploying']),
69
            'keep_deployments' => Validator::notEmpty()->intVal()->min(1),
70
        ]);
71
    }
72
73
    /**
74
     * @author Ronan Chilvers <[email protected]>
75
     */
76
    protected function beforeCreate()
77
    {
78
        if (empty($this->token)) {
79
            $this->token = Str::token(64);
80
        }
81
        $key = preg_replace('#[^A-z0-9\-]#', '-', $this->name);
82
        $key = preg_replace('#[-]{2,}#', '-', $key);
83
        $key = strtolower($key);
84
        if (!Orm::finder(get_called_class())->keyIsUnique($key)) {
85
            $this->addError('name', 'Name must be unique');
86
            return false;
87
        }
88
        $this->key = $key;
89
        $this->status = 'active';
90
    }
91
92
    /**
93
     * @author Ronan Chilvers <[email protected]>
94
     */
95
    public function beforeSave()
96
    {
97
        if (empty($this->keep_deployments)) {
98
            $this->keep_deployments = static::DEFAULT_KEEP_DEPLOYMENTS;
0 ignored issues
show
Bug Best Practice introduced by
The property keep_deployments does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
99
        }
100
    }
101
102
    /**
103
     * Update the project data from a deployment
104
     *
105
     * @param \App\Model\Deployment $deployment
106
     * @return bool
107
     * @author Ronan Chilvers <[email protected]>
108
     */
109
    public function updateFromDeployment(Deployment $deployment)
110
    {
111
        $this->last_status = $deployment->status;
112
        if ($deployment->isDeployed()) {
113
            $this->last_number     = $deployment->number;
114
            $this->last_deployment = $deployment->finished;
115
            $this->last_sha        = $deployment->sha;
116
            $this->last_author     = $deployment->author;
117
        }
118
    }
119
120
    /**
121
     * Mark this project as deploying
122
     *
123
     * @return boolean
124
     * @author Ronan Chilvers <[email protected]>
125
     */
126
    public function markDeploying()
127
    {
128
        $this->status = 'deploying';
129
130
        return $this->save();
131
    }
132
133
    /**
134
     * Mark this project as active
135
     *
136
     * @return boolean
137
     * @author Ronan Chilvers <[email protected]>
138
     */
139
    public function markActive()
140
    {
141
        $this->status = 'active';
142
143
        return $this->save();
144
    }
145
146
    /**
147
     * Is this project deployable?
148
     *
149
     * @author Ronan Chilvers <[email protected]>
150
     */
151
    public function isDeployable()
152
    {
153
        return 'active' == $this->status;
154
    }
155
}
156