Order::_setShipmentDetails()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 12
rs 10
1
<?php
2
3
namespace bSecure;
4
use bSecure\Helpers\Constant;
5
6
/**
7
 * Class bSecure.
8
 */
9
class Order
10
{
11
    /** @var string The merchant order id to be used for Create Order requests. */
12
    private static $orderId = null;
13
    private static $orderIdDefinition = false;
14
15
    /** @var array The customer object to be used for Create Order requests. */
16
    private static $customer = [];
17
    private static $customerDefinition = false;
18
19
    /** @var array The products object to be used for Create Order requests. */
20
    private static $products = [];
21
    private static $productsDefinition = false;
22
23
    /** @var string The order charges to be used for Create Order requests. */
24
    private static $sub_total_amount = null;
25
    private static $discount_amount = null;
26
    private static $total_amount = null;
27
28
    private static $chargesDefinition = false;
29
30
    /** @var array The shipment object to be used for Create Order requests. */
31
    private static $shipment = [
32
      "charges" => '',
33
      "method_name" => '',
34
    ];
35
36
    /* @var string $orderPayload this variable is used for, setting payload for create order API call to bSecure server */
37
    private static $orderPayload = [
38
      'order_id' => null,
39
      'customer' => null,
40
      'products' => null,
41
      'shipment_charges' => null,
42
      'shipment_method_name' => null,
43
      'sub_total_amount' => null,
44
      'discount_amount' => null,
45
      'total_amount' => null
46
    ];
47
48
    /**
49
     * Sets the orderId to be used for Create Order requests.
50
     *
51
     * @param string $orderId
52
     */
53
    public static function setOrderId($orderId)
54
    {
55
        self::$orderIdDefinition =true;
56
        self::$orderId = $orderId;
57
    }
58
    /**
59
     * Sets the customer object to be used for Create Order requests.
60
     *
61
     * @param array $customer
62
     */
63
    public static function setCustomer($customerData)
64
    {
65
        self::$customerDefinition =true;
66
        $customer = self::_setCustomer($customerData);
67
        self::$customer = $customer;
68
    }
69
70
    /**
71
     * Sets the shipment object to be used for Create Order requests.
72
     *
73
     * @param array $shipment
74
     */
75
    public static function setShipmentDetails($shipmentData)
76
    {
77
        $shipmentObject = [];
78
        $shipment = self::_setShipmentDetails($shipmentData);
79
        $shipmentObject['charges'] = $shipment['charges'];
80
        $shipmentObject['method_name'] = $shipment['method_name'];
81
        self::$shipment = $shipmentObject;
82
    }
83
84
    /**
85
     * Sets the products object to be used for Create Order requests.
86
     *
87
     * @param array $products
88
     */
89
    public static function setCartItems($products)
90
    {
91
        $orderItems = [];
92
93
        if(!empty($products))
94
        {
95
            foreach ($products as $key => $product) {
96
                //Product Price
97
                $price = array_key_exists('price',$product) ? $product['price'] : 0;
98
                $sale_price = array_key_exists('sale_price',$product) ? $product['sale_price'] : 0;
99
                $quantity = array_key_exists('quantity',$product) ? $product['quantity'] : 1;
100
101
                //Product options
102
                $product_options = self::_setProductOptionsDataStructure($product);
103
104
                $options_price = $product_options['price'];
105
                $options = $product_options['options'];
106
107
108
                #Product charges
109
                $discount = ( $price - $sale_price ) * $quantity;
110
                $product_price = ( $price + $options_price ) * $quantity;
111
                $product_sub_total = ( $price + $options_price ) * $quantity;
112
113
                $orderItems[] = [
114
                  "id" => array_key_exists('id',$product) ? $product['id'] : null,
115
                  "name" => array_key_exists('name',$product) ? $product['name'] : null,
116
                  "sku" => array_key_exists('sku',$product) ? $product['sku'] : null,
117
                  "quantity" => $quantity,
118
                  "price" => $product_price,
119
                  "sale_price" => $sale_price,
120
                  "discount" => $discount,
121
                  "sub_total" => $product_sub_total,
122
                  "image" => array_key_exists('image',$product) ? $product['image'] : null,
123
                  "short_description" => array_key_exists('short_description',$product) ? $product['short_description'] : null,
124
                  "description" => array_key_exists('description',$product) ? $product['description'] : null,
125
                  "product_options" => $options
126
                ];
127
            }
128
129
        }
130
        self::$products = $orderItems;
131
        self::$productsDefinition =true;
132
    }
133
134
    /**
135
     * Sets the sub_total_amount, discount_amount and total_amount to be used for Create Order requests.
136
     *
137
     * @param string $orderCharges
138
     */
139
    public static function setCharges($orderCharges)
140
    {
141
        self::$sub_total_amount = array_key_exists('sub_total', $orderCharges) ? $orderCharges['sub_total'] : 0;
0 ignored issues
show
Bug introduced by
$orderCharges of type string is incompatible with the type ArrayObject|array expected by parameter $array of array_key_exists(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

141
        self::$sub_total_amount = array_key_exists('sub_total', /** @scrutinizer ignore-type */ $orderCharges) ? $orderCharges['sub_total'] : 0;
Loading history...
142
        self::$discount_amount = array_key_exists('discount', $orderCharges) ? $orderCharges['discount'] : 0;
143
        self::$total_amount = array_key_exists('total', $orderCharges) ? $orderCharges['total'] : 0;
144
        self::$chargesDefinition =true;
145
    }
146
147
    private static function _setCustomer($customerData)
148
    {
149
        $customer = [];
150
        if(!empty($customerData))
151
        {
152
            $auth_code = array_key_exists('auth_code',$customerData) ? $customerData['auth_code'] : '' ;
153
154
            if( !empty( $auth_code ) )
155
            {
156
                $customer = [
157
                  "auth_code" => $auth_code,
158
                ];;
159
            }
160
            else{
161
                $customer = [
162
                  "country_code" => array_key_exists('country_code',$customerData) ? $customerData['country_code'] : '',
163
                  "phone_number" => array_key_exists('phone_number',$customerData) ? $customerData['phone_number'] : '',
164
                  "name" => array_key_exists('name',$customerData) ? $customerData['name'] : '',
165
                  "email" => array_key_exists('email',$customerData) ? $customerData['email'] : '',
166
                ];
167
            }
168
        }
169
170
        return $customer;
171
    }
172
173
    private static function _setShipmentDetails($shipmentData)
174
    {
175
        $shipmentDetail = [
176
          "charges" => '',
177
          "method_name" => '',
178
        ];
179
        if(!empty($shipmentData))
180
        {
181
            $shipmentDetail['charges'] = array_key_exists('charges',$shipmentData) ? $shipmentData['charges'] : '';
182
            $shipmentDetail['method_name'] = array_key_exists('method_name',$shipmentData) ? $shipmentData['method_name'] : '';
183
        }
184
        return  $shipmentDetail;
185
    }
186
187
    private static function _setProductOptionsDataStructure($product)
188
    {
189
        $product_options = array_key_exists('product_options',$product) ? $product['product_options'] : [];
190
191
        $price = 0;
192
        if( isset($product_options) && !empty($product_options) )
193
        {
194
            foreach( $product_options as $productOption )
195
            {
196
                $productValue = array_key_exists('value',$productOption) ? $productOption['value'] : [];
197
                foreach( $productValue as $key => $optionValue )
198
                {
199
                    $optionPrice = array_key_exists('price',$optionValue) ? $optionValue['price'] : [];
200
                    if(!empty($optionPrice))
201
                    {
202
                        #Price ++
203
                        $price += $optionPrice;
204
                    }
205
                }
206
            }
207
        }
208
209
        return [
210
          'price' => $price,
211
          'options'  => $product_options
212
        ];
213
    }
214
215
    private static function _setOrderPayload()
216
    {
217
        if(!self::$chargesDefinition)
218
        {
219
            $msg = 'No charges provided.  (HINT: set your sub_total, discount and total amount using '
220
              . '"bSecure::setCharges(<ARRAY>). See"'
221
              . Constant::DOCUMENTATION_LINK.' for details, '
222
              . 'or email '.Constant::SUPPORT_EMAIL.' if you have any questions.';
223
            throw new Exception\UnexpectedValueException($msg);
224
        }else if(!self::$productsDefinition)
225
        {
226
            $msg = 'No cart_items provided.  (HINT: set your cart_items using '
227
              . '"bSecure::setCartItems(<ARRAY>). See"'
228
              . Constant::DOCUMENTATION_LINK.' for details, '
229
              . 'or email '.Constant::SUPPORT_EMAIL.' if you have any questions.';
230
            throw new Exception\UnexpectedValueException($msg);
231
        }else if(!self::$customerDefinition)
232
        {
233
            $msg = 'No customer_details provided.  (HINT: set your customer_details using '
234
              . '"bSecure::setCustomer(<ARRAY>). See"'
235
              . Constant::DOCUMENTATION_LINK.' for details, '
236
              . 'or email '.Constant::SUPPORT_EMAIL.' if you have any questions.';
237
            throw new Exception\UnexpectedValueException($msg);
238
        }else{
239
            self::$orderPayload = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('order_id' => self... => self::total_amount) of type array<string,array|mixed|string> is incompatible with the declared type string of property $orderPayload.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
240
              'order_id' => self::$orderId,
241
              'customer' => self::$customer,
242
              'products' => self::$products,
243
              'shipment_charges' => self::$shipment['charges'],
244
              'shipment_method_name' => self::$shipment['method_name'],
245
              'sub_total_amount' => self::$sub_total_amount,
246
              'discount_amount' => self::$discount_amount,
247
              'total_amount' => self::$total_amount
248
            ];
249
            return self::$orderPayload;
250
        }
251
252
    }
253
254
    /**
255
     * @param null
256
     *
257
     * @throws \bSecure\ApiResponse if the request fails
258
     * @throws \bSecure\Exception\AuthenticationException if authentication fails
259
     * @throws \bSecure\Exception\UnexpectedValueException if authentication fails
260
     *
261
     * @return \bSecure\ApiResponse  Returns create order API response
262
     */
263
    public static function createOrder()
264
    {
265
        self::_setOrderPayload();
266
267
        $msg = 'No auth_token provided.  (HINT: set your auth_token using '
268
          . '"bSecure::setAuthToken()".  See '
269
          . Constant::DOCUMENTATION_LINK.', for details, '
270
          . 'or email '.Constant::SUPPORT_EMAIL.' if you have any questions.';
271
        $access_token = bSecure::getAuthToken();
272
        if($access_token == null)
273
            throw new Exception\AuthenticationException($msg);
274
        else{
275
            return OrderController::createOrder(self::$orderPayload);
0 ignored issues
show
Bug introduced by
self::orderPayload of type string is incompatible with the type array expected by parameter $orderPayload of bSecure\OrderController::createOrder(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

275
            return OrderController::createOrder(/** @scrutinizer ignore-type */ self::$orderPayload);
Loading history...
276
        }
277
    }
278
279
    /**
280
     * @param order_ref
281
     *
282
     * @throws \bSecure\ApiResponse if the request fails
283
     * @throws \bSecure\Exception\AuthenticationException if authentication fails
284
     * @throws \bSecure\Exception\UnexpectedValueException if authentication fails
285
     *
286
     * @return \bSecure\ApiResponse  Returns create order API response
287
     */
288
    public static function orderStatus($order_ref)
289
    {
290
        if($order_ref == null || $order_ref == "")
291
        {
292
            $msg = 'No order_ref provided. See"'
293
              . Constant::DOCUMENTATION_LINK.' for details, '
294
              . 'or email '.Constant::SUPPORT_EMAIL.' if you have any questions.';
295
            throw new Exception\UnexpectedValueException($msg);
296
        }
297
298
        return OrderController::orderStatus($order_ref);
299
    }
300
}