ShopHistory::getCreationTimeAttribute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Support\Facades\DB;
6
use Sofa\Eloquence\Eloquence;
7
use Sofa\Eloquence\Mappable;
8
9
/**
10
 * Class ShopHistory.
11
 *
12
 * @property int transactionId
13
 */
14
class ShopHistory extends ChocolateyModel
15
{
16
    use Eloquence, Mappable;
17
18
    /**
19
     * The table associated with the model.
20
     *
21
     * @var string
22
     */
23
    protected $table = 'chocolatey_shop_history';
24
25
    /**
26
     * Primary Key of the Table.
27
     *
28
     * @var string
29
     */
30
    protected $primaryKey = 'id';
31
32
    /**
33
     * The attributes that will be mapped.
34
     *
35
     * @var array
36
     */
37
    protected $maps = ['transactionSystemName' => 'payment_method', 'transactionId' => 'id'];
38
39
    /**
40
     * The Appender(s) of the Model.
41
     *
42
     * @var array
43
     */
44
    protected $appends = ['creationTime', 'transactionSystemName', 'credits', 'price', 'transactionId', 'product'];
45
46
    /**
47
     * The attributes excluded from the model's JSON form.
48
     *
49
     * @var array
50
     */
51
    protected $hidden = ['created_at', 'updated_at', 'approved_by', 'user_id', 'item_id'];
52
53
    /**
54
     * Get Creation Time Attribute.
55
     *
56
     * @return string
57
     */
58
    public function getCreationTimeAttribute(): string
59
    {
60
        return date('Y-m-d', strtotime($this->attributes['created_at'])).'T'.
61
            date('H:i:s.ZZZZ+ZZZZ', strtotime($this->attributes['created_at']));
62
    }
63
64
    /**
65
     * Get Payment Method Name.
66
     *
67
     * @return string
68
     */
69
    public function getTransactionSystemNameAttribute(): string
70
    {
71
        return PaymentMethod::find($this->attributes['payment_method'])->name;
72
    }
73
74
    /**
75
     * Get Transaction Id.
76
     *
77
     * @return int
78
     */
79
    public function getTransactionIdAttribute(): int
80
    {
81
        return $this->attributes['id'];
82
    }
83
84
    /**
85
     * Get Amount of Given Credits.
86
     *
87
     * @return int
88
     */
89
    public function getCreditsAttribute(): int
90
    {
91
        return DB::table('chocolatey_shop_items')->where('id', $this->attributes['item_id'])->first()->creditAmount;
92
    }
93
94
    /**
95
     * Get Product Attribute.
96
     *
97
     * @return mixed|object|array
98
     */
99
    public function getProductAttribute()
100
    {
101
        return DB::table('chocolatey_shop_items')->where('id', $this->attributes['item_id'])->first(['name', 'description']);
102
    }
103
104
    /**
105
     * Get Price Attribute.
106
     *
107
     * @return string
108
     */
109
    public function getPriceAttribute(): string
110
    {
111
        return DB::table('chocolatey_shop_items')->where('id', $this->attributes['item_id'])->first()->price;
112
    }
113
114
    /**
115
     * Store a new Purchase.
116
     *
117
     * @param int $paymentMethod
118
     * @param int $userId
119
     * @param int $itemId
120
     *
121
     * @return ShopHistory
122
     */
123
    public function store(int $paymentMethod, int $userId, int $itemId): ShopHistory
124
    {
125
        $this->attributes['payment_method'] = $paymentMethod;
126
        $this->attributes['user_id'] = $userId;
127
        $this->attributes['item_id'] = $itemId;
128
129
        $this->save();
130
131
        return $this;
132
    }
133
}
134