Completed
Push — development ( 88b422...7f868f )
by Claudio
05:12
created

Photo::getTypeAttribute()   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 Sofa\Eloquence\Eloquence;
6
use Sofa\Eloquence\Mappable;
7
use Sofa\Eloquence\Metable\InvalidMutatorException;
8
9
/**
10
 * Class Photo
11
 * @package App\Models
12
 */
13
class Photo extends ChocolateyModel
14
{
15
    use Eloquence, Mappable;
16
17
    /**
18
     * The table associated with the model.
19
     *
20
     * @var string
21
     */
22
    protected $table = 'camera_web';
23
24
    /**
25
     * Primary Key of the Table
26
     *
27
     * @var string
28
     */
29
    protected $primaryKey = 'id';
30
31
    /**
32
     * The attributes that will be mapped
33
     *
34
     * @var array
35
     */
36
    protected $maps = [
37
        'creator_id' => 'user_id',
38
        'previewUrl' => 'url',
39
        'creator_uniqueId' => 'user_id',
40
        'time' => 'timestamp'
41
    ];
42
43
    /**
44
     * The Appender(s) of the Model
45
     *
46
     * @var array
47
     */
48
    protected $appends = [
49
        'creator_uniqueId',
50
        'version',
51
        'previewUrl',
52
        'creator_id',
53
        'likes',
54
        'tags',
55
        'version',
56
        'type',
57
        'room_id',
58
        'creator_name'
59
    ];
60
61
    /**
62
     * The attributes that should be casted to native types.
63
     *
64
     * @var array
65
     */
66
    protected $casts = [
67
        'tags' => 'array',
68
        'creator_uniqueId' => 'string'
69
    ];
70
71
    /**
72
     * Store Function
73
     *
74
     * A photo can't be inserted by the CMS.
75
     * Only by the Emulator
76
     */
77
    public function store()
78
    {
79
        throw new InvalidMutatorException("You cannot store a Photo by Chocolatey. Photos need be created from the Server.");
80
    }
81
82
    /**
83
     * Get the Unique Id of the Photo
84
     *
85
     * @return string
86
     */
87
    public function getIdAttribute(): string
88
    {
89
        return "{$this->attributes['id']}";
90
    }
91
92
    /**
93
     * Get the Version Attribute
94
     *
95
     * @return int
96
     */
97
    public function getVersionAttribute(): int
98
    {
99
        return 1;
100
    }
101
102
    /**
103
     * Get All Tags
104
     * Transforming it on an Array
105
     *
106
     * @return array(string)
0 ignored issues
show
Documentation introduced by
The doc-type array(string) could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
107
     */
108
    public function getTagsAttribute(): array
109
    {
110
        return [];
111
    }
112
113
    /**
114
     * Get Formatted Time
115
     * Convert Date to UNIX Timestamp
116
     *
117
     * @return int
118
     */
119
    public function getTimeAttribute(): int
120
    {
121
        return strtotime($this->attributes['timestamp']) * 1000;
122
    }
123
124
    /**
125
     * Get Photo Likes Directly as Username
126
     *
127
     * @return array
128
     */
129
    public function getLikesAttribute(): array
130
    {
131
        return PhotoLike::query()->select('username')->where('photo_id', $this->attributes['id'])->get(['username'])->toArray();
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
132
    }
133
134
    /**
135
     * Get the Photo Type Attribute
136
     *
137
     * @return string
138
     */
139
    public function getTypeAttribute(): string
140
    {
141
        return 'PHOTO';
142
    }
143
144
    /**
145
     * Get Room Id
146
     *
147
     * @TODO: Add real RoomID
148
     *
149
     * @return int
150
     */
151
    public function getRoomIdAttribute(): int
152
    {
153
        return 1;
154
    }
155
156
    /**
157
     * Get User Name
158
     *
159
     * @return string
160
     */
161
    public function getCreatorNameAttribute(): string
162
    {
163
        return User::find($this->attributes['user_id'])->name;
164
    }
165
}
166