Skip to content

Commit ebcfd30

Browse files
author
dormin
committed
change validation boolean arguments to enum
cleanup abstract transport a bit
1 parent 565e2b0 commit ebcfd30

File tree

4 files changed

+39
-37
lines changed

4 files changed

+39
-37
lines changed

src/Riskified/Common/Env.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,10 @@ class Env {
2222
const STAGING = 'staging';
2323
const SANDBOX = 'sandbox';
2424
const DEV = 'development';
25-
}
25+
}
26+
27+
class Validations {
28+
const SKIP = 'skip';
29+
const IGNORE_MISSING = 'ignore_missing';
30+
const ALL = 'all';
31+
}

src/Riskified/Common/Riskified.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* @package Riskified\Common
2121
*/
2222
class Riskified {
23-
const VERSION = '1.0.9';
23+
const VERSION = '1.1.0';
2424
const API_VERSION = '2';
2525

2626
/**
@@ -36,28 +36,31 @@ class Riskified {
3636
*/
3737
public static $env;
3838
/**
39-
* @var boolean indicates validation should ignore missing key errors
39+
* @var string validation mode [SKIP, IGNORE_MISSING, ALL]
4040
*/
41-
public static $ignore_missing_keys;
42-
/**
43-
* @var boolean skips all validations when true
44-
*/
45-
public static $skip_all_validations;
41+
public static $validations;
42+
4643

4744
/**
4845
* Sets up Riskified credentials. Must be called before any other method can be used.
4946
* @param $domain string Riskified Shop Domain
5047
* @param $auth_token string Riskified Auth_Token
5148
* @param $env string Riskified environment
52-
* @param $ignore_missing_keys boolean ignores missing keys when true
53-
* @param $skip_all_validations boolean skips all validations when true
49+
* @param $validations string SDK validation mode
5450
*/
55-
public static function init($domain, $auth_token, $env = Env::SANDBOX, $ignore_missing_keys = false, $skip_all_validations = false) {
51+
public static function init($domain, $auth_token, $env = Env::SANDBOX, $validations = Validations::ALL) {
5652
self::$domain = $domain;
5753
self::$auth_token = $auth_token;
5854
self::$env = $env;
59-
self::$ignore_missing_keys = $ignore_missing_keys;
60-
self::$skip_all_validations = $skip_all_validations;
55+
56+
// for backward compatibility (versions 1.0.*)
57+
if (is_bool($validations))
58+
$validations = ($validations) ? Validations::SKIP : Validations::ALL;
59+
60+
self::$validations = $validations;
61+
62+
// suppress timezone warnings:
63+
date_default_timezone_set(@date_default_timezone_get());
6164
}
6265

6366
public static function getHostByEnv(){

src/Riskified/OrderWebhook/Model/AbstractModel.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* permissions and limitations under the License.
1515
*/
1616

17-
use Riskified\Common\Riskified;
1817
use Riskified\OrderWebhook\Exception;
1918

2019
/**
@@ -116,7 +115,7 @@ public function toXml() {
116115
* @throws \Riskified\OrderWebhook\Exception\MultiplePropertiesException on any or multiple issues
117116
*/
118117
public function validate($enforce_required_keys=true) {
119-
$exceptions = $this->validation_exceptions($enforce_required_keys && !Riskified::$ignore_missing_keys);
118+
$exceptions = $this->validation_exceptions($enforce_required_keys);
120119
if ($exceptions)
121120
throw new Exception\MultiplePropertiesException($exceptions);
122121
return true;

src/Riskified/OrderWebhook/Transport/AbstractTransport.php

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
use Riskified\Common\Env;
1818
use Riskified\Common\Riskified;
19+
use Riskified\Common\Validations;
1920

2021
/**
2122
* Class AbstractTransport
@@ -60,9 +61,7 @@ public function __construct($signature, $url = null) {
6061
* @throws \Riskified\Common\Exception\BaseException on any issue
6162
*/
6263
public function submitOrder($order) {
63-
if ($this->validate_order($order))
64-
return $this->send_order($order, 'submit');
65-
return null;
64+
return $this->send_order($order, 'submit', true);
6665
}
6766

6867
/**
@@ -72,9 +71,7 @@ public function submitOrder($order) {
7271
* @throws \Riskified\Common\Exception\BaseException on any issue
7372
*/
7473
public function createOrder($order) {
75-
if ($this->validate_order($order))
76-
return $this->send_order($order, 'create');
77-
return null;
74+
return $this->send_order($order, 'create', true);
7875
}
7976

8077
/**
@@ -84,9 +81,7 @@ public function createOrder($order) {
8481
* @throws \Riskified\Common\Exception\BaseException on any issue
8582
*/
8683
public function updateOrder($order) {
87-
if ($this->validate_order($order, false))
88-
return $this->send_order($order, 'update');
89-
return null;
84+
return $this->send_order($order, 'update', false);
9085
}
9186

9287
/**
@@ -96,9 +91,7 @@ public function updateOrder($order) {
9691
* @throws \Riskified\Common\Exception\BaseException on any issue
9792
*/
9893
public function cancelOrder($order) {
99-
if ($this->validate_order($order, false))
100-
return $this->send_order($order, 'cancel');
101-
return null;
94+
return $this->send_order($order, 'cancel', false);
10295
}
10396

10497
/**
@@ -108,9 +101,7 @@ public function cancelOrder($order) {
108101
* @throws \Riskified\Common\Exception\BaseException on any issue
109102
*/
110103
public function refundOrder($order) {
111-
if ($this->validate_order($order, false))
112-
return $this->send_order($order, 'refund');
113-
return null;
104+
return $this->send_order($order, 'refund', false);
114105
}
115106

116107
public function sendHistoricalOrders($orders) {
@@ -119,15 +110,18 @@ public function sendHistoricalOrders($orders) {
119110
return $this->send_json_request($json, 'historical');
120111
}
121112

122-
protected function validate_order($order, $enforce_required_keys=true) {
123-
if (Riskified::$skip_all_validations)
124-
return true;
125-
return $order->validate($enforce_required_keys);
113+
protected function send_order($order, $endpoint, $enforce_required_keys) {
114+
if ($this->validate($order, $enforce_required_keys)) {
115+
$json = '{"order":' . $order->toJson() . '}';
116+
return $this->send_json_request($json, $endpoint);
117+
}
118+
return null;
126119
}
127120

128-
protected function send_order($order, $endpoint) {
129-
$json = '{"order":'.$order->toJson().'}';
130-
return $this->send_json_request($json, $endpoint);
121+
protected function validate($order, $enforce_required_keys=true) {
122+
if (Riskified::$validations == Validations::SKIP)
123+
return true;
124+
return $order->validate($enforce_required_keys && Riskified::$validations == Validations::ALL);
131125
}
132126

133127
/**

0 commit comments

Comments
 (0)