Passed
Push — stage ( b1dd62...f179a0 )
by Jon
07:29
created

Release::getPublishedAtAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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