Release::findByTagName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Models;
4
5
use App\Models\Traits\BelongsToProject;
6
use App\Models\Traits\BelongsToUser;
7
use Backpack\CRUD\CrudTrait;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
10
use Illuminate\Database\Eloquent\SoftDeletes;
11
use Illuminate\Support\Carbon;
12
use Laracasts\Matryoshka\Cacheable;
13
14
/**
15
 * App\Models\Release
16
 *
17
 * @property int $id
18
 * @property \Carbon\Carbon|null $created_at
19
 * @property \Carbon\Carbon|null $updated_at
20
 * @property string|null $deleted_at
21
 * @property \Carbon\Carbon|null $published_at
22
 * @property int|null $user_id
23
 * @property int|null $agent_id
24
 * @property string $name
25
 * @property string|null $body
26
 * @property string $tag_name
27
 * @property string $target_commitish
28
 * @property bool $is_draft
29
 * @property bool $is_prerelease
30
 * @property array $github_response
31
 * @property int|null $github_id
32
 * @property-read mixed $github_created_at
33
 * @property-read mixed $html_url
34
 * @property-read mixed $project_id
35
 * @property-read mixed $tarball_url
36
 * @property-read mixed $zipball_url
37
 * @property-read \App\Models\Project|null $project
38
 * @property-read \App\Models\Access\User\User|null $user
39
 * @method static bool|null forceDelete()
40
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Release onlyTrashed()
41
 * @method static bool|null restore()
42
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereAgentId($value)
43
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereBody($value)
44
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereCreatedAt($value)
45
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereDeletedAt($value)
46
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereGithubId($value)
47
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereGithubResponse($value)
48
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereId($value)
49
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereIsDraft($value)
50
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereIsPrerelease($value)
51
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereName($value)
52
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release wherePublishedAt($value)
53
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereTagName($value)
54
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereTargetCommitish($value)
55
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereUpdatedAt($value)
56
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereUserId($value)
57
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Release withTrashed()
58
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Release withoutTrashed()
59
 * @mixin \Eloquent
60
 */
61
class Release extends Model
62
{
63
    use SoftDeletes, Cacheable;
0 ignored issues
show
Bug introduced by
The trait Laracasts\Matryoshka\Cacheable requires the property $timestamp which is not provided by App\Models\Release.
Loading history...
64
    use CrudTrait;
0 ignored issues
show
introduced by
The trait Backpack\CRUD\CrudTrait requires some properties which are not provided by App\Models\Release: $Type, $fakeColumns
Loading history...
65
    use BelongsToProject, BelongsToUser;
66
67
    /*
68
    |--------------------------------------------------------------------------
69
    | GLOBAL VARIABLES
70
    |--------------------------------------------------------------------------
71
    */
72
73
    public const TABLE    = 'releases';
74
    protected $table      = self::TABLE;
75
    protected $primaryKey = 'id';
76
    public $timestamps    = true;
77
    protected $guarded    = ['id'];
78
    protected $casts      = [
79
        'github_response' => 'array',
80
        'is_draft'        => 'bool',
81
        'is_prerelease'   => 'bool',
82
    ];
83
    protected $appends = [
84
        'tarball_url',
85
        'zipball_url',
86
        'html_url',
87
        'github_created_at',
88
    ];
89
    // protected $fillable = [];
90
    protected $hidden = ['github_response'];
91
    protected $dates  = [
92
        'github_created_at',
93
        'published_at',
94
    ];
95
96
    /*
97
    |--------------------------------------------------------------------------
98
    | FUNCTIONS
99
    |--------------------------------------------------------------------------
100
    */
101
102
    /**
103
     * @param $tagName
104
     *
105
     * @return \Illuminate\Database\Eloquent\Model|static
106
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
107
     */
108
    public static function findByTagName($tagName)
109
    {
110
        return static::where('tag_name', $tagName)->firstOrFail();
111
    }
112
113
    /*
114
    |--------------------------------------------------------------------------
115
    | RELATIONS
116
    |--------------------------------------------------------------------------
117
    */
118
119
    public function vocabularies(): ?BelongsToMany
120
    {
121
        return $this->morphedByMany(Vocabulary::class, 'releaseable')->withTimestamps();
122
    }
123
124
    public function elementsets(): ?BelongsToMany
125
    {
126
        return $this->morphedByMany(Elementset::class, 'releaseable')->withTimestamps();
127
    }
128
129
    /*
130
    |--------------------------------------------------------------------------
131
    | SCOPES
132
    |--------------------------------------------------------------------------
133
    */
134
135
    /*
136
    |--------------------------------------------------------------------------
137
    | ACCESORS
138
    |--------------------------------------------------------------------------
139
    */
140
141
    /*
142
    |--------------------------------------------------------------------------
143
    | MUTATORS
144
    |--------------------------------------------------------------------------
145
    */
146
147
    //these all retrieve data from the json github_response
148
    public function getTarballUrlAttribute()
149
    {
150
        return $this->getAttribute('github_response')['tarball_url'];
151
    }
152
153
    public function getHtmlUrlAttribute()
154
    {
155
        return $this->getAttribute('github_response')['html_url'];
156
    }
157
158
    public function getZipballUrlAttribute()
159
    {
160
        return $this->getAttribute('github_response')['zipball_url'];
161
    }
162
163
    public function getGithubCreatedAtAttribute()
164
    {
165
        return Carbon::parse($this->getAttribute('github_response')['created_at'])->toDateTimeString();
166
    }
167
}
168