Saveable::save()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 5
c 4
b 1
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Lyal\Checkr\Traits;
4
5
use Lyal\Checkr\Exceptions\IdMissing;
6
7
trait Saveable
8
{
9
    protected $savePath;
10
11
    /**
12
     * Abstract functions to imppose requirements for the exhibiting class.
13
     */
14
    abstract public function getResourceName($object = null);
15
16
    abstract public function getAttributes($sanitized = true);
17
18
    abstract public function processPath($path = null, array $values = null);
19
20
    abstract public function postRequest($path, array $options = []);
21
22
    abstract public function getClient();
23
24
    abstract public function setValues($values);
25
26
    abstract public function getAttribute($key);
27
28
    /**
29
     * Save an object.
30
     *
31
     * @throws \Lyal\Checkr\Exceptions\IdMissing
32
     *
33
     * @return $this
34
     */
35
    public function save()
36
    {
37
        if ($this->getAttribute('id') === null) {
38
            throw new IdMissing($this);
39
        }
40
        $path = $this->getSavePath() ?? $this->getResourceName().'/'.$this->getAttribute('id');
41
        $this->setValues($this->postRequest($path));
42
43
        return $this;
44
    }
45
46
    /**
47
     * Get the save path of the current object.
48
     *
49
     * @return string
50
     */
51
    public function getSavePath()
52
    {
53
        return $this->savePath;
54
    }
55
56
    /**
57
     * Set the save path of the current object.
58
     *
59
     * @param $path
60
     *
61
     * @return mixed
62
     */
63
    public function setSavePath($path)
64
    {
65
        return $this->savePath = $path;
66
    }
67
}
68