Completed
Push — develop ( 0a4bcc...0131cc )
by Kirill
25:10
created

Karma::getCreatedAtAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 13
rs 9.4285
cc 1
eloc 6
nc 1
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A Karma.php$0 ➔ toArray() 0 7 1
1
<?php
2
/**
3
 * This file is part of GitterBot package.
4
 *
5
 * @author Serafim <[email protected]>
6
 * @date 09.10.2015 20:15
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace App;
12
13
use Carbon\Carbon;
14
use Illuminate\Contracts\Support\Arrayable;
15
16
/**
17
 * Class Karma
18
 * @package App
19
 *
20
 * @property-read int $id
21
 * @property string $room_id
22
 * @property string $message_id
23
 * @property int $user_id
24
 * @property int $user_target_id
25
 * @property string $status
26
 *
27
 * === Relations ===
28
 *
29
 * @property-read User $user
30
 * @property-read User $target
31
 *
32
 * === Accessors ===
33
 *
34
 * @property string $created_at
35
 *
36
 */
37
class Karma extends \Eloquent
38
{
39
    /**
40
     * @var string
41
     */
42
    protected $table = 'karma';
43
44
    /**
45
     * @var array
46
     */
47
    public $timestamps = false;
48
49
    /**
50
     * @var array
51
     */
52
    protected $guarded = ['id'];
53
54
    /**
55
     * Boot
56
     */
57
    public static function boot()
58
    {
59
        parent::boot();
60
61
        static::creating(function (Karma $karma) {
62
            if (!$karma->created_at) {
63
                $karma->created_at = $karma->freshTimestamp();
64
            }
65
66
            \Event::fire('karma.add', ['karma' => $karma]);
67
        });
68
69
    }
70
71
    /**
72
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
73
     */
74
    public function user()
75
    {
76
        return $this->belongsTo(User::class, 'user_id', 'gitter_id');
77
    }
78
79
    /**
80
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
81
     */
82
    public function target()
83
    {
84
        return $this->belongsTo(User::class, 'user_target_id', 'gitter_id');
85
    }
86
87
    /**
88
     * @param $time
89
     * @return Carbon
90
     */
91
    public function getCreatedAtAttribute($time)
92
    {
93
        return new class($time) extends Carbon implements Arrayable
94
        {
95
            public function toArray()
96
            {
97
                return [
98
                    'date'     => $this->toIso8601String(),
99
                    'timezone' => $this->timezone,
100
                ];
101
            }
102
        };
103
    }
104
}
105