Completed
Push — development ( 2fad51...a387f8 )
by Claudio
02:44
created

ShopItem   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 107
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A store() 0 10 1
A getPaymentMethodsAttribute() 0 13 2
A getCategoriesAttribute() 0 9 2
A getUniqueIdAttribute() 0 4 1
1
<?php
2
3
namespace App\Models;
4
5
/**
6
 * Class ShopItem
7
 * @property mixed uniqueId
8
 * @package App\Models
9
 */
10
class ShopItem extends ChocolateyModel
11
{
12
    /**
13
     * Disable Timestamps
14
     *
15
     * @var bool
16
     */
17
    public $timestamps = false;
18
19
    /**
20
     * The table associated with the model.
21
     *
22
     * @var string
23
     */
24
    protected $table = 'chocolatey_shop_items';
25
26
    /**
27
     * Primary Key of the Table
28
     *
29
     * @var string
30
     */
31
    protected $primaryKey = 'id';
32
33
    /**
34
     * The Appender(s) of the Model
35
     *
36
     * @var array
37
     */
38
    protected $appends = [
39
        'paymentMethods',
40
        'uniqueId'
41
    ];
42
43
    /**
44
     * The attributes excluded from the model's JSON form.
45
     *
46
     * @var array
47
     */
48
    protected $hidden = [
49
        'payment_methods'
50
    ];
51
52
    /**
53
     * Store an Shop Country
54
     *
55
     * @param string $itemName
56
     * @param string $countryCode
57
     * @param int $creditAmount
58
     * @param int $iconId
59
     * @param array $paymentMethods
60
     * @return ShopItem
61
     */
62
    public function store(string $itemName, string $countryCode, int $creditAmount, int $iconId, array $paymentMethods): ShopItem
63
    {
64
        $this->attributes['name'] = $itemName;
65
        $this->attributes['countryCode'] = $countryCode;
66
        $this->attributes['creditAmount'] = $creditAmount;
67
        $this->attributes['iconId'] = $iconId;
68
        $this->attributes['payment_methods'] = implode(',', $paymentMethods);
69
70
        return $this;
71
    }
72
73
    /**
74
     * Get Payment Methods
75
     *
76
     * @return array
77
     */
78
    public function getPaymentMethodsAttribute(): array
79
    {
80
        $paymentMethods = [];
81
82
        foreach (explode(',', $this->attributes['payment_methods']) as $shopCategory):
83
            $paymentMethod = PaymentMethod::where('localizationKey', $shopCategory)->first();
84
            $paymentMethod->setPurchaseParams([Country::where('countryCode', $this->attributes['countryCode'])->first()->uniqueId, $this->attributes['id']]);
85
86
            $paymentMethods[] = $paymentMethod;
87
        endforeach;
88
89
        return $paymentMethods;
90
    }
91
92
    /**
93
     * Get Shop Item Categories
94
     *
95
     * @return array
96
     */
97
    public function getCategoriesAttribute(): array
98
    {
99
        $shopCategories = [];
100
101
        foreach (explode(',', $this->attributes['categories']) as $shopCategory)
102
            $shopCategories[] = ShopCategory::find($shopCategory)->category;
103
104
        return $shopCategories;
105
    }
106
107
    /**
108
     * Get Unique Id
109
     *
110
     * @return int
111
     */
112
    public function getUniqueIdAttribute(): int
113
    {
114
        return $this->attributes['id'];
115
    }
116
}
117