Issues (16)

src/Traits/BatchTrait.php (1 issue)

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