Passed
Push — master ( 69b4a5...1004b6 )
by Ronan
06:49
created

Project::getProviderOptions()   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 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
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
    ];
43
44
    /**
45
     * Boot the model
46
     *
47
     * @author Ronan Chilvers <[email protected]>
48
     */
49
    protected function boot()
50
    {
51
        $this->addType('datetime', 'last_deployment');
52
    }
53
54
    /**
55
     * @author Ronan Chilvers <[email protected]>
56
     */
57
    protected function setupValidation()
58
    {
59
        $providerKeys = array_keys(Provider::getOptions());
0 ignored issues
show
Bug introduced by
The method getOptions() does not exist on App\Facades\Provider. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

59
        $providerKeys = array_keys(Provider::/** @scrutinizer ignore-call */ getOptions());
Loading history...
60
        $this->registerRules([
61
            'name'       => Validator::notEmpty(),
62
            'provider'   => Validator::notEmpty()->in($providerKeys),
63
            'repository' => Validator::notEmpty(),
64
            'branch'     => Validator::notEmpty(),
65
            'status'     => Validator::notEmpty()->in(['active', 'deploying']),
66
        ]);
67
    }
68
69
    /**
70
     * @author Ronan Chilvers <[email protected]>
71
     */
72
    protected function beforeCreate()
73
    {
74
        if (empty($this->token)) {
75
            $this->token = Str::token(64);
76
        }
77
        $key = preg_replace('#[^A-z0-9\-]#', '-', $this->name);
78
        $key = preg_replace('#[-]{2,}#', '-', $key);
79
        $key = strtolower($key);
80
        if (!Orm::finder(get_called_class())->keyIsUnique($key)) {
81
            $this->addError('name', 'Name must be unique');
82
            return false;
83
        }
84
        $this->key = $key;
85
    }
86
87
    /**
88
     * Mark this project as deploying
89
     *
90
     * @return boolean
91
     * @author Ronan Chilvers <[email protected]>
92
     */
93
    public function markDeploying()
94
    {
95
        $this->status = 'deploying';
96
97
        return $this->save();
98
    }
99
100
    /**
101
     * Mark this project as active
102
     *
103
     * @return boolean
104
     * @author Ronan Chilvers <[email protected]>
105
     */
106
    public function markActive()
107
    {
108
        $this->status = 'active';
109
110
        return $this->save();
111
    }
112
113
    /**
114
     * Is this project deployable?
115
     *
116
     * @author Ronan Chilvers <[email protected]>
117
     */
118
    public function isDeployable()
119
    {
120
        return 'active' == $this->status;
121
    }
122
}
123