Passed
Push — master ( 61f6d4...d79925 )
by Neil
02:51
created

GetResourcesTrait::__call()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace neilherbertuk\celcatwebapi\Traits;
4
5
use neilherbertuk\celcatwebapi\CelcatWebAPI;
6
7
/**
8
 * Trait GetResourcesTrait
9
 * @package neilherbertuk\celcatwebapi\Traits
10
 */
11
trait GetResourcesTrait
12
{
13
    /**
14
     * @var
15
     */
16
    protected $celcatWebAPI;
17
18
    /**
19
     * @var array
20
     */
21
    protected $parameters = [];
22
23
    /**
24
     * GetResourcesTrait constructor.
25
     * @param CelcatWebAPI $celcatWebAPI
26
     */
27
    public function __construct(CelcatWebAPI $celcatWebAPI)
28
    {
29
        $this->celcatWebAPI = $celcatWebAPI;
30
    }
31
32
    /**
33
     * Get Operator - Performs a single request for a resource
34
     *
35
     * @param int $pageSize
36
     * @return mixed
37
     * @throws \ReflectionException
38
     */
39
    public function get($pageSize = 50)
40
    {
41
        if (empty($this->parameters['pageSize'])) {
42
            $this->parameters['pageSize'] = $pageSize;
43
        }
44
45
        $this->celcatWebAPI->log()->info('Getting '.(new \ReflectionClass($this))->getShortName());
46
        return $this->celcatWebAPI->get((empty($this->name) ? (new \ReflectionClass($this))->getShortName() : $this->name), $this->parameters);
47
    }
48
49
    /**
50
     * GetAll Operator - Uses the get operator and pagination results to request all results for a resource
51
     *
52
     * @param int $pageSize
53
     * @return array|mixed
54
     * @throws \ReflectionException
55
     */
56
    public function getAll($pageSize = 1000)
57
    {
58
        if (empty($this->parameters['pageSize'])) {
59
            $this->parameters['pageSize'] = $pageSize;
60
        }
61
62
        $results = $this->get();
63
64
        if ($this->hasPagination($results)) {
65
            for ($page = 1; $page < $results['pagination']['TotalPages']; $page++) {
66
                $this->parameters['page'] = $page;
67
                $pageResults = $this->get();
68
69
                $results['pagination']['CurrentPage'] = $pageResults['parameters']['page'] + 1;
70
                unset($pageResults['pagination'], $pageResults['parameters']);
71
                $results = array_merge_recursive($results, $pageResults);
72
            }
73
        }
74
        return $results;
75
    }
76
77
    /**
78
     * First Operator - Requests the first element of a resource
79
     *
80
     * @return mixed
81
     * @throws \ReflectionException
82
     */
83
    public function first()
84
    {
85
        $this->parameters['pageSize'] = 1;
86
        $this->parameters['page'] = 0;
87
        return $this->get();
88
    }
89
90
    /**
91
     * Where Operator - Chainable method to add parameters to requests
92
     *
93
     * @param array $parameters
94
     * @return $this
95
     */
96
    public function where($parameters = [])
97
    {
98
        $this->parameters = array_merge($this->parameters, $parameters);
99
        return $this;
100
    }
101
102
    /**
103
     * Does the result have pagination?
104
     *
105
     * @param $results
106
     * @return bool
107
     */
108
    protected function hasPagination($results)
109
    {
110
        return !empty($results['pagination']['TotalPages']);
111
    }
112
}