Completed
Push — development ( a928ed...ed05a0 )
by Claudio
02:29
created

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