Passed
Push — master ( 756e7a...85d7cc )
by Gombos
02:17
created

Events::batchStore()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 4
nop 1
dl 0
loc 25
ccs 12
cts 12
cp 1
crap 4
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Glorand\Drip\Api;
4
5
use Glorand\Drip\Api\Response\ApiResponse;
6
use Glorand\Drip\Exceptions\DripException;
7
use Glorand\Drip\Models\Event;
8
9
class Events extends Api
10
{
11
    /**
12
     * @param int $page
13
     * @param int $perPage
14
     *
15
     * @throws DripException
16
     *
17
     * @return Response\ApiResponse
18
     */
19 2
    public function list(int $page = 1, int $perPage = 100): ApiResponse
20
    {
21 2
        if (1 > $perPage || 1000 < $perPage) {
22 2
            throw new DripException('Invalid per page; maximum 1000');
23
        }
24
25 1
        return $this->sendGet(
26 1
            ':account_id:/event_actions',
27
            [
28 1
                'page'     => $page,
29 1
                'per_page' => $perPage,
30
            ]
31
        );
32
    }
33
34
    /**
35
     * Record an event
36
     * @see https://developer.drip.com/?shell#events
37
     *
38
     * @param Event $event
39
     *
40
     * @return bool
41
     */
42 1
    public function store(Event $event): bool
43
    {
44 1
        $response = $this->sendPost(
45 1
            ':account_id:/events',
46 1
            $event->toDrip()
47
        );
48
49 1
        return $response->isSuccess();
50
    }
51
52
    /**
53
     * @param array $events
54
     *
55
     * @return bool
56
     */
57 1
    public function batchStore(array $events)
58
    {
59 1
        $data = [];
60 1
        foreach ($events as $event) {
61 1
            if (is_array($event)) {
62 1
                $data[] = $event;
63 1
            } elseif ($event instanceof Event) {
64 1
                $data[] = $event->jsonSerialize();
65
            }
66
        }
67
68
        $postData = [
69
            'batches' => [
70
                [
71 1
                    'events' => $data,
72
                ],
73
            ],
74
        ];
75
76 1
        $response = $this->sendPost(
77 1
            ':account_id:/events/batches',
78 1
            $postData
79
        );
80
81 1
        return $response->isSuccess();
82
    }
83
}
84