1 <?php
2 /**
3 * Copyright 2019 Klarna AB
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * File containing the Instant Shopping ButtonKeys class.
18 */
19
20 namespace Klarna\Rest\InstantShopping;
21
22 use GuzzleHttp\Exception\RequestException;
23 use Klarna\Rest\Resource;
24 use Klarna\Rest\Transport\ConnectorInterface;
25 use Klarna\Rest\Transport\Exception\ConnectorException;
26
27 /**
28 * Instant shopping ButtonKeys resource.
29 */
30 class ButtonKeys extends Resource
31 {
32 /**
33 * {@inheritDoc}
34 */
35 const ID_FIELD = 'button_key';
36
37 /**
38 * {@inheritDoc}
39 */
40 public static $path = '/instantshopping/v1/buttons';
41
42 /**
43 * Constructs a ButtonKey instance.
44 *
45 * @param ConnectorInterface $connector HTTP transport connector
46 * @param string $buttonKey Button identifier
47 * @param string $key Button key based on setup options
48 */
49 public function __construct(ConnectorInterface $connector, $buttonKey = null)
50 {
51 parent::__construct($connector);
52
53 if ($buttonKey !== null) {
54 $this->setLocation(self::$path . "/{$buttonKey}");
55 $this[static::ID_FIELD] = $buttonKey;
56 }
57 }
58
59 /**
60 * Creates a button key based on setup options.
61 *
62 * @param array $data Creation data
63 *
64 * @see https://developers.klarna.com/api/#instant-shopping-api-create-a-button-key-based-on-setup-options
65 *
66 * @throws ConnectorException When the API replies with an error response
67 * @throws RequestException When an error is encountered
68 * @throws \RuntimeException If the API replies with an unexpected response
69 * @throws \LogicException When Guzzle cannot populate the response
70 *
71 * @return array Button properties
72 */
73 public function create(array $data)
74 {
75 $response = $this->post(self::$path, $data)
76 ->expectSuccessfull()
77 ->status('201')
78 ->contentType('application/json');
79
80 $url = $response->getLocation();
81 $this->setLocation($url);
82
83 return $response->getJson();
84 }
85
86 /**
87 * Updates the setup options for a specific button key.
88 *
89 * @param array $data Update data
90 *
91 * @see https://developers.klarna.com/api/#instant-shopping-api-update-the-setup-options-for-a-specific-button-key
92 *
93 * @throws ConnectorException When the API replies with an error response
94 * @throws RequestException When an error is encountered
95 * @throws \RuntimeException If the API replies with an unexpected response
96 * @throws \RuntimeException If key was not specified when creating a resource
97 * @throws \LogicException When Guzzle cannot populate the response
98 *
99 * @return array Button properties
100 */
101 public function update(array $data)
102 {
103 if (empty($this[static::ID_FIELD])) {
104 throw new \RuntimeException(static::ID_FIELD . ' property is not defined');
105 }
106
107 return $this->put($this->getLocation(), $data)
108 ->expectSuccessfull()
109 ->status('200')
110 ->contentType('application/json')
111 ->getJson();
112 }
113
114 /**
115 * See the setup options for a specific button key.
116 *
117 * @see https://developers.klarna.com/api/#instant-shopping-api-see-the-setup-options-for-a-specific-button-key
118 *
119 * @throws ConnectorException When the API replies with an error response
120 * @throws RequestException When an error is encountered
121 * @throws \RuntimeException If key was not specified when creating a resource
122 * @throws \RuntimeException On an unexpected API response
123 * @throws \RuntimeException If the response content type is not JSON
124 * @throws \InvalidArgumentException If the JSON cannot be parsed
125 * @throws \LogicException When Guzzle cannot populate the response
126 *
127 * @return self
128 */
129 public function retrieve()
130 {
131 if (empty($this[static::ID_FIELD])) {
132 throw new \RuntimeException(static::ID_FIELD . ' property is not defined');
133 }
134
135 return $this->fetch();
136 }
137 }
138