Passed
Push — main ( e110ad...47f0cd )
by Dylan
02:09
created

Model   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
eloc 33
c 1
b 0
f 0
dl 0
loc 83
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A exists() 0 3 1
B __construct() 0 33 8
A toArray() 0 3 1
A getService() 0 7 3
A save() 0 10 4
1
<?php
2
3
namespace Lifeboat\Models;
4
5
use Lifeboat\Connector;
6
use Lifeboat\Exceptions\InvalidArgumentException;
7
use Lifeboat\Factory\ClassMap;
8
use Lifeboat\Resource\ObjectResource;
9
use Lifeboat\Factory\ObjectFactory;
10
use Lifeboat\Services\ApiService;
11
use Lifeboat\Utils\ArrayLib;
12
use ErrorException;
13
14
/**
15
 * Class Model
16
 * @package Lifeboat\Models
17
 *
18
 * @property int $ID
19
 */
20
abstract class Model extends ObjectResource {
21
22
    public function __construct(Connector $client, array $_object_data = [])
23
    {
24
        if (!ArrayLib::is_associative($_object_data)) {
25
            throw new InvalidArgumentException("Model::__construct() expects an associative array");
26
        }
27
28
        parent::__construct($client, $_object_data);
29
30
        // Mutate objects if needs be
31
        foreach ($this->toArray() as $field => $value) {
32
            if (is_array($value)) {
33
                if (ArrayLib::is_associative($value)) {
34
                    // Model or Object
35
                    if (!array_key_exists('field_name', $value)) {
36
                        $this->$field = ObjectFactory::make($client, $value);
37
                    } else {
38
                        // Has One Relation - Deprecated
39
                        $name = $value['field_name'];
40
                        $this->$name    = $value['field_value'];
41
                        $this->$field   = ObjectFactory::make($client, $value['object_data']);
42
                    }
43
                } else {
44
                    // HasMany / ManyMany Relation
45
                    $list = [];
46
                    foreach ($value as $item) {
47
                        if (ArrayLib::is_associative($item)) {
48
                            $list[] = ObjectFactory::make($client, $item);
49
                        } else {
50
                            $list[] = $item;
51
                        }
52
                    }
53
54
                    $this->$field = $list;
55
                }
56
            }
57
        }
58
    }
59
60
    /**
61
     * @return array
62
     */
63
    public function toArray(): array
64
    {
65
        return iterator_to_array($this->getIterator());
66
    }
67
68
    /**
69
     * @return bool
70
     */
71
    public function exists(): bool
72
    {
73
        return $this->ID > 0;
74
    }
75
76
    /**
77
     * @return ApiService
78
     * @throws ErrorException
79
     */
80
    public function getService(): ApiService
81
    {
82
        foreach (ClassMap::SERVICE_MODEL as $service => $model) {
83
            if ($model === static::class) return new $service($this->getClient());
84
        }
85
86
        throw new ErrorException("Could not determine which service to use for " . static::class);
87
    }
88
89
    /**
90
     * @return $this|null
91
     * @throws ErrorException
92
     */
93
    protected function save(): ?Model
94
    {
95
        $service = $this->getService();
96
97
        if ($this->exists()) {
98
            if (!method_exists($service, 'update')) return $this;
99
            return $service->update($this->ID, $this->toArray());
100
        } else {
101
            if (!method_exists($service, 'create')) return $this;
102
            return $service->create($this->toArray());
103
        }
104
    }
105
}
106