Builder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace tbclla\Revolut\Builders;
4
5
use tbclla\Revolut\Interfaces\Buildable;
6
7
abstract Class Builder
8
{
9
    /**
10
     * The API resource
11
     *
12
     * @var \tbclla\Revolut\Interfaces\Buildable
13
     */
14
    protected $resource;
15
16
    /**
17
     * The unique ID of the request
18
     *
19
     * @var string
20
     */
21
    public $request_id;
22
23
    /**
24
     * Create a new builder instance
25
     *
26
     * @param \tbclla\Revolut\Resources\Resource $resource
27
     * @param string $requestId
28
     * @return void
29
     */
30
    public function __construct(Buildable $resource, string $requestId = null)
31
    {
32
        $this->resource = $resource;
33
        $this->request_id = $requestId;
34
    }
35
36
    /**
37
     * Execute the create request
38
     *
39
     * @return array
40
     */
41
    public function create()
42
    {
43
        return $this->resource->create($this->toArray());
44
    }
45
46
    /**
47
     * Set the unique equest ID
48
     *
49
     * @param string $id
50
     * @return self
51
     */
52
    public function requestId(string $id)
53
    {
54
        return $this->setAttribute('request_id', $id);
55
    }
56
57
    /**
58
     * Set the value of an attribute
59
     *
60
     * @param string $attribute
61
     * @param mixed $value
62
     * @return self
63
     */
64
    protected function setAttribute(string $attribute, $value)
65
    {
66
        $this->$attribute = $value;
67
68
        return $this;
69
    }
70
71
    /**
72
     * Build the request data array
73
     *
74
     * @return array
75
     */
76
    public function toArray()
77
    {
78
        $data = [];
79
80
        foreach (get_object_vars($this) as $attribute => $value) {
81
            if ($attribute != 'resource' and !empty($value)) {
82
                $data[$attribute] = $value;
83
            }
84
        }
85
86
        return $data;
87
    }
88
}
89