Passed
Pull Request — master (#26)
by
unknown
01:43
created

confirmOrders()   A

Complexity

Conditions 3
Paths 16

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 21
c 1
b 0
f 0
nc 16
nop 0
dl 0
loc 31
rs 9.584
1
<?php
2
3
4
require_once('../vendor/autoload.php');
5
require_once('../examples/utils/Helpers.php');
6
7
/**
8
 * PLEASE FILL YOUR PUBLIC KEY AND PRIVATE KEY
9
 */
10
const PUBLIC_KEY = ''; //Set your public key
11
const PRIVATE_KEY = ''; //Set your public key
12
13
14
try {
15
    call_user_func('confirmOrders');
16
} catch (\Exception $e) {
17
    echo $e->getMessage();
18
    exit;
19
}
20
21
function confirmOrders()
22
{
23
    try {
24
        $fileName = basename(__FILE__);
25
        $withDate = true;
26
        writeLog('Creating Client', $fileName,$withDate);
27
        $orderApiClient = getClient();
28
        writeLog('Client Created', $fileName,$withDate);
29
        writeLog('Fetching Authorized Orders', $fileName,$withDate);
30
        $authorizedOrders = $orderApiClient->listOrders(array(
31
            'status' => \Pagantis\OrdersApiClient\Model\Order::STATUS_AUTHORIZED
32
        ));
33
34
        if (!isAuthorizedOrderCountAboveZero($authorizedOrders)) {
35
            $createdOrders = $orderApiClient->listOrders(array(
36
                'status' => \Pagantis\OrdersApiClient\Model\Order::STATUS_CREATED));
37
            print("<pre>" . print_r("Number of Created Orders: ". count($createdOrders)."\n"."Number of Authorized Orders: " . count($authorizedOrders)."\n"."", true) . "</pre>");
38
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
39
        }
40
        writeLog('Confirming all Authorized Orders', $fileName,$withDate);
41
42
        $confirmedOrders = getConfirmedOrdersRecursively($authorizedOrders,$orderApiClient);
43
44
45
        writeLog('Orders Confirmed', $fileName,$withDate);
46
        writeLog(jsonEncoded($confirmedOrders), $fileName,$withDate);
47
        /** WARNING: orders must be confirmed on your back office or you will get a empty object */
48
        print("<pre>" . print_r($confirmedOrders, true) . "</pre>");
49
50
    } catch (\Exception $exception) {
51
        $exception->getMessage();
52
    }
53
}
54
55
/**
56
 * @return \Pagantis\OrdersApiClient\Client
57
 * @throws \Httpful\Exception\ConnectionErrorException
58
 * @throws \Pagantis\OrdersApiClient\Exception\ClientException
59
 * @throws Exception
60
 */
61
function getClient()
62
{
63
    if (PUBLIC_KEY === '' || PRIVATE_KEY === '') {
0 ignored issues
show
introduced by
The condition PUBLIC_KEY === '' is always true.
Loading history...
64
        throw new \Exception('You need set the public and private key');
65
    }
66
    try {
67
        $orderClient = new \Pagantis\OrdersApiClient\Client(PUBLIC_KEY, PRIVATE_KEY);
68
    } catch (\Exception $e) {
69
        $e->getMessage();
70
    }
71
72
    return $orderClient;
73
}
74
75
76
/**
77
 * @param                                  $authorizedOrders
78
 *
79
 * @param \Pagantis\OrdersApiClient\Client $orderApiClient
80
 *
81
 * @return array
82
 * @throws \Pagantis\OrdersApiClient\Exception\ClientException
83
 * @throws Exception
84
 */
85
function getConfirmedOrdersRecursively(
86
    $authorizedOrders,
87
    \Pagantis\OrdersApiClient\Client $orderApiClient
88
) {
89
90
    if (!$orderApiClient instanceof \Pagantis\OrdersApiClient\Client) {
0 ignored issues
show
introduced by
$orderApiClient is always a sub-type of Pagantis\OrdersApiClient\Client.
Loading history...
91
        throw new \Pagantis\OrdersApiClient\Exception\ClientException('Client Instance Error');
92
    }
93
    $confirmedOrders = array();
94
    foreach ($authorizedOrders as $order) {
95
        $orderConfirmed = $orderApiClient->confirmOrder($order->getId());
96
        array_push($confirmedOrders, $orderConfirmed);
97
    }
98
    return $confirmedOrders;
99
}