CartProduct::getModelDataAsEntity()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace QuickCheckout;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Relations\Relation;
7
use Illuminate\Support\Arr;
8
use QuickCheckout\Contracts\ProductInterface;
9
10
abstract class CartProduct implements ProductInterface
11
{
12
    protected string $title = '';
13
14
    protected int $price = 0;
15
16
    protected array $meta = [];
17
18
    /**
19
     * @param string $title
20
     * @param int $price
21
     * @param array $meta
22
     */
23 9
    public function __construct(string $title, int $price, array $meta = [])
24
    {
25 9
        $this->title = $title;
26 9
        $this->price = $price;
27 9
        $this->meta  = $meta;
28 9
    }
29
30
    /**
31
     * @inerhitDoc
32
     */
33 6
    public static function fromArray(array $data): ?ProductInterface
34
    {
35 6
        $class = (string) ($data['class'] ?? '');
36 6
        if ($class && is_a($class, ProductInterface::class, true)) {
37 6
            return new $class(
38 6
                (string) ($data['title'] ?? ''),
39 6
                (int) ($data['price'] ?? 0),
40 6
                (array) ($data['meta'] ?? [])
41
            );
42
        }
43
44 1
        return null;
45
    }
46
47 6
    public function toArray(): array
48
    {
49
        return [
50 6
            'class' => get_class($this),
51 6
            'title' => $this->title,
52 6
            'price' => $this->price,
53 6
            'meta'  => $this->meta,
54
        ];
55
    }
56
57
    /**
58
     * @inerhitDoc
59
     */
60 1
    public function title(): string
61
    {
62 1
        return $this->title;
63
    }
64
65
    /**
66
     * @inerhitDoc
67
     */
68 4
    public function price(): int
69
    {
70 4
        return $this->price;
71
    }
72
73
    /**
74
     * @inerhitDoc
75
     */
76 4
    public function meta(?string $key = null, mixed $default = null): mixed
77
    {
78 4
        if (!is_null($key)) {
79 4
            return Arr::get($this->meta, $key, $default);
80
        }
81
82 1
        return $this->meta;
83
    }
84
85
    /**
86
     * @inerhitDoc
87
     * @psalm-suppress InvalidMethodCall
88
     */
89 3
    public function checkoutEntity(): ?Model
90
    {
91 3
        if (!empty($class = $this->meta('entity_type')) &&
92 3
             !empty($key = $this->meta('entity_id'))
93
        ) {
94 2
            $class = Arr::get(Relation::morphMap() ?: [], $class, $class);
95 2
            if (is_a($class, Model::class, true)) {
96 2
                if (method_exists($class, 'checkoutProductFromId')) {
97 1
                    return $class::checkoutProductFromId($key);
98
                }
99
100 1
                return $class::find($key);
101
            }
102
        }
103
104 1
        return null;
105
    }
106
107 5
    public static function getModelDataAsEntity(Model $model): array
108
    {
109
        return [
110 5
            'entity_type' => $model->getMorphClass(),
111 5
            'entity_id'   => $model->getKey(),
112
        ];
113
    }
114
}
115