Passed
Push — main ( 383cbf...40aa9a )
by Dylan
02:01
created

Model   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A exists() 0 3 1
B __construct() 0 43 10
A toArray() 0 3 1
A save() 0 11 3
1
<?php
2
3
namespace Lifeboat\Models;
4
5
use Lifeboat\Connector;
6
use Lifeboat\Exceptions\ApiException;
7
use Lifeboat\Exceptions\InvalidArgumentException;
8
use Lifeboat\Exceptions\OAuthException;
9
use Lifeboat\Resource\ObjectResource;
10
use Lifeboat\Factory\ObjectFactory;
11
use Lifeboat\Utils\ArrayLib;
12
13
/**
14
 * Class Model
15
 * @package Lifeboat\Models
16
 *
17
 * @property int $ID
18
 * @property string $model
19
 */
20
abstract class Model extends ObjectResource {
21
22
    abstract protected function getSaveURL(): string;
23
24
    public function __construct(Connector $client, array $_object_data = [])
25
    {
26
        parent::__construct($client, $_object_data);
27
28
        if (!ArrayLib::is_associative($_object_data)) {
29
            throw new InvalidArgumentException("Model::__construct() expects an associative array");
30
        }
31
32
        if (array_key_exists('model', $_object_data)) $this->model = $_object_data['model'];
33
        $model = $this->model;
34
35
        foreach ($_object_data as $field => $value) {
36
            if (is_array($value)) {
37
                if (ArrayLib::is_associative($value)) {
38
                    // Model or Object
39
                    if (!array_key_exists('field_name', $value)) {
40
                        $this->$field = ObjectFactory::make($client, $value);
41
                    } else {
42
                        // Has One Relation - Deprecated
43
                        $name = $value['field_name'];
44
                        $this->$name    = $value['field_value'];
45
                        $this->$field   = ObjectFactory::make($client, $value['object_data']);
46
                    }
47
48
                    // Setup the reverse relationship unless it's already setup
49
                    if (!property_exists($this->$field, $model)) {
50
                        $this->$field->$model = $this;
51
                    }
52
                } else {
53
                    // HasMany / ManyMany Relation
54
                    $list = [];
55
                    foreach ($value as $item) {
56
                        if (ArrayLib::is_associative($item)) {
57
                            $list = ObjectFactory::make($client, $item);
58
                        } else {
59
                            $list = $item;
60
                        }
61
                    }
62
63
                    $this->$field = $list;
64
                }
65
            } else {
66
                $this->$field = $value;
67
            }
68
        }
69
    }
70
71
    /**
72
     * @return array
73
     */
74
    public function toArray(): array
75
    {
76
        return iterator_to_array($this->getIterator());
77
    }
78
79
    /**
80
     * @return bool
81
     */
82
    public function exists(): bool
83
    {
84
        return $this->ID > 0;
85
    }
86
87
    /**
88
     * @return Model|null
89
     * @throws ApiException
90
     * @throws OAuthException
91
     */
92
    public function save(): ?Model
93
    {
94
        $curl = $this->getClient()->curl_api($this->getSaveURL(), 'POST', $this->toArray());
95
96
        if ($curl->isValid() && $curl->isJSON()) {
97
            /** @var Model|null $model */
98
            $model = ObjectFactory::make($this->getClient(), $curl->getJSON());
0 ignored issues
show
Bug introduced by
It seems like $this->getClient() can also be of type null; however, parameter $connector of Lifeboat\Factory\ObjectFactory::make() does only seem to accept Lifeboat\Connector, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

98
            $model = ObjectFactory::make(/** @scrutinizer ignore-type */ $this->getClient(), $curl->getJSON());
Loading history...
99
            return $model;
100
        }
101
102
        throw new ApiException($curl->getError());
103
    }
104
}
105