Device::activate()   A
last analyzed

Complexity

Conditions 3
Paths 8

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 9.344
c 0
b 0
f 0
cc 3
nc 8
nop 2
1
<?php
2
namespace CultureKings\Afterpay\Service\InStore;
3
4
use CultureKings\Afterpay\Exception\ApiException;
5
use CultureKings\Afterpay\Model;
6
use GuzzleHttp\Exception\BadResponseException;
7
use GuzzleHttp\HandlerStack;
8
use JMS\Serializer\SerializationContext;
9
10
/**
11
 * Class Device
12
 * @package CultureKings\Afterpay\Service\InStore
13
 */
14
class Device extends AbstractService
15
{
16
    /**
17
     * @param Model\InStore\Device $device
18
     * @param HandlerStack|null    $stack
19
     *
20
     * @return array|\JMS\Serializer\scalar|Model\InStore\Device
21
     */
22
    public function activate(Model\InStore\Device $device, HandlerStack $stack = null)
23
    {
24
        try {
25
            $params = [
26
                'headers' => [
27
                    'Accept' => 'application/json',
28
                    'Content-Type' => 'application/json',
29
                ],
30
                'body' => $this->getSerializer()->serialize(
31
                    $device,
32
                    'json',
33
                    SerializationContext::create()->setGroups(['activateDevice'])
34
                ),
35
            ];
36
            if ($stack !== null) {
37
                $params['handler'] = $stack;
38
            }
39
40
            $result = $this->getClient()->post('devices/activate', $params);
41
42
            return $this->getSerializer()->deserialize(
43
                (string) $result->getBody(),
44
                Model\InStore\Device::class,
45
                'json'
46
            );
47
        } catch (BadResponseException $e) {
48
            throw new ApiException(
49
                $this->getSerializer()->deserialize(
50
                    (string) $e->getResponse()->getBody(),
51
                    Model\ErrorResponse::class,
52
                    'json'
53
                ),
54
                $e
55
            );
56
        }
57
    }
58
59
    /**
60
     * @param Model\InStore\Device $device
61
     * @param HandlerStack|null    $stack
62
     *
63
     * @return array|\JMS\Serializer\scalar|Model\InStore\DeviceToken
64
     */
65
    public function createToken(Model\InStore\Device $device, HandlerStack $stack = null)
66
    {
67
        try {
68
            $params = [
69
                'headers' => [
70
                    'Accept' => 'application/json',
71
                    'Content-Type' => 'application/json',
72
                ],
73
                'body' => $this->getSerializer()->serialize(
74
                    $device,
75
                    'json',
76
                    SerializationContext::create()->setGroups(['createToken'])
77
                ),
78
            ];
79
            if ($stack !== null) {
80
                $params['handler'] = $stack;
81
            }
82
83
            $result = $this->getClient()->post(sprintf('devices/%d/token', $device->getDeviceId()), $params);
84
85
            return $this->getSerializer()->deserialize(
86
                (string) $result->getBody(),
87
                Model\InStore\DeviceToken::class,
88
                'json'
89
            );
90
        } catch (BadResponseException $e) {
91
            throw new ApiException(
92
                $this->getSerializer()->deserialize(
93
                    (string) $e->getResponse()->getBody(),
94
                    Model\ErrorResponse::class,
95
                    'json'
96
                ),
97
                $e
98
            );
99
        }
100
    }
101
}
102