Completed
Push — development ( 2e60ea...09118d )
by Claudio
05:31 queued 02:42
created

Photo::getRoomIdAttribute()   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 URL of the Photo
94
     *
95
     * @return string
96
     */
97
    public function getUrlAttribute(): string
98
    {
99
        return str_replace('http', '', str_replace('https', '', $this->attributes['url']));
100
    }
101
102
    /**
103
     * Get the Version Attribute
104
     *
105
     * @return int
106
     */
107
    public function getVersionAttribute(): int
108
    {
109
        return 1;
110
    }
111
112
    /**
113
     * Get All Tags
114
     * Transforming it on an Array
115
     *
116
     * @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...
117
     */
118
    public function getTagsAttribute(): array
119
    {
120
        return [];
121
    }
122
123
    /**
124
     * Get Formatted Time
125
     * Convert Date to UNIX Timestamp
126
     *
127
     * @return int
128
     */
129
    public function getTimeAttribute(): int
130
    {
131
        return strtotime($this->attributes['timestamp']) * 1000;
132
    }
133
134
    /**
135
     * Get Photo Likes Directly as Username
136
     *
137
     * @return array
138
     */
139
    public function getLikesAttribute(): array
140
    {
141
        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...
142
    }
143
144
    /**
145
     * Get the Photo Type Attribute
146
     *
147
     * @return string
148
     */
149
    public function getTypeAttribute(): string
150
    {
151
        return 'PHOTO';
152
    }
153
154
    /**
155
     * Get Room Id
156
     *
157
     * @TODO: Add real RoomID
158
     *
159
     * @return int
160
     */
161
    public function getRoomIdAttribute(): int
162
    {
163
        return 1;
164
    }
165
166
    /**
167
     * Get User Name
168
     *
169
     * @return string
170
     */
171
    public function getCreatorNameAttribute(): string
172
    {
173
        return User::find($this->attributes['user_id'])->name;
174
    }
175
}
176