Saveable   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 6
Bugs 2 Features 0
Metric Value
wmc 4
eloc 9
c 6
b 2
f 0
dl 0
loc 59
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setSavePath() 0 3 1
A getSavePath() 0 3 1
A save() 0 9 2
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