Creatable   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 4
eloc 10
c 3
b 1
f 0
dl 0
loc 62
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getCreatePath() 0 3 1
A setCreatePath() 0 3 1
A create() 0 12 2
1
<?php
2
3
namespace Lyal\Checkr\Traits;
4
5
use Lyal\Checkr\Exceptions\ResourceNotCreated;
6
7
trait Creatable
8
{
9
    /**
10
     * Abstract functions to imppose requirements for the exhibiting class.
11
     */
12
    abstract public function getResourceName($object = null);
13
14
    abstract public function getAttributes($sanitized = true);
15
16
    abstract public function processPath($path = null, array $values = null);
17
18
    abstract public function postRequest($path, array $options = []);
19
20
    abstract public function getClient();
21
22
    abstract public function setValues($values);
23
24
    abstract public function setAttributes(array $values);
25
26
    protected $createPath;
27
28
    /**
29
     * Post a resource and update this parameters with the response from the API.
30
     *
31
     * @throws ResourceNotCreated
32
     *
33
     * @return $this
34
     */
35
    public function create()
36
    {
37
        $path = $this->getCreatePath() ?? $this->getResourceName();
38
39
        $response = $this->postRequest($path);
40
        $this->setAttributes([]);
41
        $this->setValues($response);
42
        if ($this->getClient()->getLastResponse()->getStatusCode() !== 201) {
43
            throw new ResourceNotCreated($this->getClient()->getLastResponse()->getBody());
44
        }
45
46
        return $this;
47
    }
48
49
    /**
50
     * Get the create path of the current object.
51
     *
52
     * @return string
53
     */
54
    public function getCreatePath()
55
    {
56
        return $this->createPath;
57
    }
58
59
    /**
60
     * Set the save path of the current object.
61
     *
62
     * @param $path
63
     *
64
     * @return mixed
65
     */
66
    public function setCreatePath($path)
67
    {
68
        return $this->createPath = $path;
69
    }
70
}
71