Completed
Push — master ( 50a9e6...dccb70 )
by Steven
02:20
created

BatchTrait::addBatchUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php namespace Stevenmaguire\Services\Trello\Traits;
2
3
use Stevenmaguire\Services\Trello\Exceptions\Exception;
4
5
trait BatchTrait
6
{
7
    /**
8
     * Batch urls
9
     *
10
     * @var array
11
     */
12
    private $batchUrls = [];
13
14
    /**
15
     * Retrieves currently configured http broker.
16
     *
17
     * @return Stevenmaguire\Services\Trello\Http
18
     * @codeCoverageIgnore
19
     */
20
    abstract public function getHttp();
21
22
    /**
23
     * Adds single url to batch collection.
24
     *
25
     * @param string $url
26
     *
27
     * @return $this
28
     */
29 8
    public function addBatchUrl($url)
30
    {
31 8
        $this->batchUrls[] = (string) $url;
32
33 8
        return $this;
34
    }
35
36
37
    /**
38
     * Adds multiple urls to batch collection.
39
     *
40
     * @param array  $urls
41
     *
42
     * @return void
43
     */
44 2
    public function addBatchUrls(array $urls)
45
    {
46 2
        array_map([$this, 'addBatchUrl'], $urls);
47 2
    }
48
49
    /**
50
     * Retrieves http response from Trello api for batch collection.
51
     *
52
     * @param  array  $attributes
53
     *
54
     * @return object
55
     * @throws Exception
56
     */
57 8
    public function getBatch($attributes = [])
58
    {
59 8
        $this->parseBatchAttributes($attributes);
60
61
        try {
62 8
            $result = $this->getHttp()->get('batch', ['urls' => $this->batchUrls]);
63 6
            $this->batchUrls = [];
64
65 6
            return $result;
66 2
        } catch (Exception $e) {
67 2
            throw $e;
68
        }
69
    }
70
71
    /**
72
     * Retrieves batch urls currently queued for request.
73
     *
74
     * @return array
75
     */
76 2
    public function getBatchUrls()
77
    {
78 2
        return $this->batchUrls;
79
    }
80
81
    /**
82
     * Attempts to parse attributes to pull valid urls.
83
     *
84
     * @param  array   $attributes
85
     *
86
     * @return void
87
     */
88 8
    protected function parseBatchAttributes($attributes = [])
89
    {
90 8
        if (isset($attributes['urls'])) {
91 4
            if (is_array($attributes['urls'])) {
92 2
                $this->addBatchUrls($attributes['urls']);
93 3
            } elseif (is_string($attributes['urls'])) {
94 2
                $this->addBatchUrl($attributes['urls']);
95 1
            }
96 2
        }
97 8
    }
98
}
99