Skip to content

Commit 6563d4f

Browse files
author
Alexander Birkner
committed
Update Mi 22 Jun 2016 16:04:29 CEST
1 parent 1e43b15 commit 6563d4f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1318
-581
lines changed

lib/Nitrapi/Common/Http/Client.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ public function dataGet($url, $headers = null, $options = array()) {
5454
$response = json_decode($e->getResponse()->getBody(), true);
5555
$msg = isset($response['message']) ? $response['message'] : 'Unknown error';
5656
if ($e->getResponse()->getStatusCode() == 503) {
57-
throw new NitrapiMaintenanceException();
57+
throw new NitrapiMaintenanceException($msg);
5858
}
5959
if ($e->getResponse()->getStatusCode() == 428) {
60-
throw new NitrapiConcurrencyException();
60+
throw new NitrapiConcurrencyException($msg);
6161
}
6262
throw new NitrapiHttpErrorException($msg);
6363
}
@@ -94,10 +94,10 @@ public function dataPost($url, $body = null, $headers = null, $options = array()
9494
$response = json_decode($e->getResponse()->getBody(), true);
9595
$msg = isset($response['message']) ? $response['message'] : 'Unknown error';
9696
if ($e->getResponse()->getStatusCode() == 503) {
97-
throw new NitrapiMaintenanceException();
97+
throw new NitrapiMaintenanceException($msg);
9898
}
9999
if ($e->getResponse()->getStatusCode() == 428) {
100-
throw new NitrapiConcurrencyException();
100+
throw new NitrapiConcurrencyException($msg);
101101
}
102102
throw new NitrapiHttpErrorException($msg);
103103
}
@@ -140,10 +140,10 @@ public function dataDelete($url, $body = null, $headers = null, $options = array
140140
$response = json_decode($e->getResponse()->getBody(), true);
141141
$msg = isset($response['message']) ? $response['message'] : 'Unknown error';
142142
if ($e->getResponse()->getStatusCode() == 503) {
143-
throw new NitrapiMaintenanceException();
143+
throw new NitrapiMaintenanceException($msg);
144144
}
145145
if ($e->getResponse()->getStatusCode() == 428) {
146-
throw new NitrapiConcurrencyException();
146+
throw new NitrapiConcurrencyException($msg);
147147
}
148148
throw new NitrapiHttpErrorException($msg);
149149
}
@@ -154,7 +154,11 @@ public function dataDelete($url, $body = null, $headers = null, $options = array
154154
}
155155

156156
protected function checkErrors(Response $response, $responseCode = 200) {
157-
$json = json_decode($response->getBody(), true);
157+
$json = @json_decode($response->getBody(), true);
158+
159+
if (is_array($json) && isset($json['status']) && $json['status'] == "error") {
160+
throw new NitrapiHttpErrorException($json["message"]);
161+
}
158162

159163
$allowedPorts = array();
160164
$allowedPorts[] = $responseCode;
@@ -165,9 +169,5 @@ protected function checkErrors(Response $response, $responseCode = 200) {
165169
if (!in_array($response->getStatusCode(), $allowedPorts)) {
166170
throw new NitrapiHttpErrorException("Invalid http status code " . $response->getStatusCode());
167171
}
168-
169-
if (isset($json['status']) && $json['status'] == "error") {
170-
throw new NitrapiHttpErrorException("Got Error from API " . $json["message"]);
171-
}
172172
}
173173
}

lib/Nitrapi/Customer/Customer.php

+26
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Nitrapi\Customer;
44

5+
use Nitrapi\Common\Exceptions\NitrapiHttpErrorException;
56
use Nitrapi\Nitrapi;
67

78
class Customer {
@@ -61,4 +62,29 @@ public function get($field = null) {
6162
if($field === null) return $this->data['user'];
6263
return $this->data['user'][$field];
6364
}
65+
66+
/**
67+
* Returns a webinterface token
68+
*
69+
* @return string
70+
*/
71+
public function getWebinterfaceToken() {
72+
$token = $this->api->dataGet('user/webinterface_token');
73+
return $token['token']['token'];
74+
}
75+
76+
/**
77+
* Deletes all webinterface tokens. This logs out all users from your webinterface.
78+
*
79+
* @return string
80+
*/
81+
public function deleteWebinterfaceTokens() {
82+
try {
83+
$this->api->dataDelete('user/webinterface_token');
84+
} catch (NitrapiHttpErrorException $e) {
85+
return false;
86+
}
87+
88+
return true;
89+
}
6490
}

lib/Nitrapi/Nitrapi.php

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ public function __construct($accessToken, $options = array(), $url = NITRAPI_LIV
2323
if (isset($options['user_id']) && !empty($options['user_id']))
2424
$query['user_id'] = (int)$options['user_id'];
2525

26+
if (isset($options['user_ip']) && filter_var($options['user_ip'], FILTER_VALIDATE_IP))
27+
$query['user_ip'] = $options['user_ip'];
28+
29+
if (isset($options['locale']) && !empty($options['locale']))
30+
$query['locale'] = (string)$options['locale'];
31+
2632
$options['query'] = $query;
2733
parent::__construct($url, $options);
2834
}

lib/Nitrapi/Order/Order.php

-28
This file was deleted.

lib/Nitrapi/Order/Price.php

-67
This file was deleted.

lib/Nitrapi/Order/PricePart.php

-73
This file was deleted.

lib/Nitrapi/Order/Pricing/DimensionPricing.php

+48-7
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,55 @@
22

33
namespace Nitrapi\Order\Pricing;
44

5+
use Nitrapi\Services\Service;
6+
57
abstract class DimensionPricing extends Pricing {
6-
7-
public function getPrices($location_id) {
8-
return $this->nitrapi->dataGet("/order/pricing/" . $this->type, null, [
9-
'query' => [
10-
'location' => $location_id
11-
]
12-
])['prices'];
8+
9+
protected $dimensions = null;
10+
11+
public function addDimenstion($dimension, $value) {
12+
if ($this->dimensions === null) {
13+
$this->getDimensions();
14+
}
15+
16+
if (!array_key_exists($dimension, $this->dimensions)) {
17+
throw new PricingException("Dimension " . $dimension . " is not available for this product.");
18+
}
19+
20+
$this->dimensions[$dimension] = $value;
21+
}
22+
23+
public function getDimensions() {
24+
if ($this->dimensions === null) {
25+
$prices = $this->getPrices();
26+
$this->dimensions = [];
27+
foreach ($prices['dimensions'] as $dimension) {
28+
$this->dimensions[$dimension['id']] = null;
29+
}
30+
}
31+
32+
return $this->dimensions;
1333
}
1434

35+
public function getPrice($rentalTime, Service &$service = null) {
36+
$information = $this->getPrices($service);
37+
$dimensions = $this->getDimensions();
38+
$dimensions['rental_time'] = $rentalTime;
39+
40+
$prices = $information['prices'];
41+
foreach ($dimensions as $key => $value) {
42+
if ($value === null) continue;
43+
if (array_key_exists($value, $prices)) {
44+
$prices = $prices[$value];
45+
} else {
46+
throw new PricingException("No dimension information for " . $value . " found.");
47+
}
48+
}
49+
50+
if (is_array($prices) && isset($prices['price'])) {
51+
return (int)$prices['price'];
52+
}
53+
54+
throw new PricingException("No price for selected dimensions not found.");
55+
}
1556
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Nitrapi\Order\Pricing;
4+
5+
class Location {
6+
7+
protected $data = [];
8+
9+
public function __construct(array $data) {
10+
$this->data = $data;
11+
}
12+
13+
public function getId() {
14+
return $this->data['id'];
15+
}
16+
17+
public function getCountry() {
18+
return $this->data['country'];
19+
}
20+
21+
public function getCity() {
22+
return $this->data['city'];
23+
}
24+
25+
public function getProducts() {
26+
return $this->data['products'];
27+
}
28+
}

0 commit comments

Comments
 (0)