ShopItem::store()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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