Passed
Pull Request — master (#26)
by Csaba
07:32
created

RestModel::useResource()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
crap 2
1
<?php
2
namespace Fathomminds\Rest;
3
4
use Fathomminds\Rest\Helpers\ReflectionHelper;
5
use Fathomminds\Rest\Exceptions\RestException;
6
use Fathomminds\Rest\Objects\RestObject;
7
use Fathomminds\Rest\Contracts\IRestModel;
8
9
abstract class RestModel implements IRestModel
10
{
11
    protected $restObjectClass;
12
    protected $restObject;
13
14 25
    public function __construct($restObject = null)
15
    {
16 25
        if ($restObject === null) {
17 1
            $reflectionHelper = new ReflectionHelper;
18 1
            $restObject = $reflectionHelper->createInstance($this->restObjectClass, []);
19
        }
20 25
        $this->restObject = $restObject;
21 25
    }
22
23 16
    protected function useResource($obj)
24
    {
25
        try {
26 16
            $this->restObject = $this->restObject->createFromObject($obj);
27 1
        } catch (\Exception $ex) {
28 1
            throw new RestException(
29 1
                'Setting model resource failed',
30 1
                ['originalException' => $ex]
31
            );
32
        }
33 15
        return $this;
34
    }
35
36 21
    public function resource($resource = null)
37
    {
38 21
        if ($resource !== null) {
39 16
            $this->useResource($resource);
40
        }
41 20
        return $this->restObject->resource();
42
    }
43
44 3
    public function one($resourceId)
45
    {
46 3
        $this->restObject->get($resourceId);
47 3
        if ($this->restObject->getPrimaryKeyValue() !== $resourceId) {
48 2
            throw new RestException(
49 2
                'Resource does not exist',
50
                [
51 2
                    'resourceName' => $this->restObject->getResourceName(),
52 2
                    'resourceId' => $resourceId,
53
                ]
54
            );
55
        }
56 1
        return $this;
57
    }
58
59 1
    public function all()
60
    {
61 1
        $list = $this->restObject->get();
62 1
        return $list;
63
    }
64
65 3
    public function create()
66
    {
67 3
        $this->restObject->updateMode = false;
68 3
        $this->restObject->setFieldDefaults();
69 3
        $this->validate();
70 2
        $this->restObject->post($this->resource());
71 1
        return $this;
72
    }
73
74 2
    public function update()
75
    {
76 2
        $this->restObject->updateMode = true;
77 2
        $this->restObject->setFieldDefaults();
78 2
        $this->validate();
79 2
        $this->restObject->put($this->restObject->getPrimaryKeyValue(), $this->resource());
80 1
        return $this;
81
    }
82
83 2
    public function delete()
84
    {
85 2
        $resourceId = $this->restObject->getPrimaryKeyValue();
86 2
        $this->restObject->delete($resourceId);
87 1
        return $resourceId;
88
    }
89
90 12
    public function validate()
91
    {
92 12
        $this->restObject->validateSchema($this->resource());
93 7
        $this->restObject->validate();
94 6
    }
95
96 1
    public function toArray()
97
    {
98 1
        return $this->restObject->toArray();
99
    }
100
}
101