GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( c85033...38e2ef )
by Kevin
01:10
created

BolComRetailerClient::getOpenOrders()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace HomeDesignShops\LaravelBolComRetailer;
4
5
use HomeDesignShops\LaravelBolComRetailer\Models\Transport;
6
use Illuminate\Support\Collection;
7
use Picqer\BolRetailer\Exception\HttpException;
8
use Picqer\BolRetailer\Model\OrderItem;
9
use Picqer\BolRetailer\Model\ReducedOrder;
10
use Picqer\BolRetailer\Order;
11
use Picqer\BolRetailer\ProcessStatus;
12
use Picqer\BolRetailer\Shipment;
13
14
class BolComRetailerClient
15
{
16
    /**
17
     * Max retry counts for the API requests.
18
     *
19
     * @var int
20
     */
21
    protected $maxRetries;
22
23
    /**
24
     * Holds the retry counts.
25
     * @var int
26
     */
27
    protected $retriesCount = 0;
28
29
    /**
30
     * BolComRetailerClient constructor.
31
     */
32
    public function __construct()
33
    {
34
        $this->maxRetries = config('bol-com-retailer.max_retries');
35
    }
36
37
    /**
38
     * Returns a collection of the open orders.
39
     *
40
     * @return Collection
41
     */
42
    public function getOpenOrders(): Collection
43
    {
44
        try {
45
            return collect(Order::all())
46
                ->transform(static function (ReducedOrder $reducedOrder) {
47
                    return Order::get($reducedOrder->orderId);
48
                });
49
50
        } catch (HttpException $e) {
51
            $this->retriesCount++;
52
            $retryInSeconds = str_replace(['Too many requests, retry in ', ' seconds.'], '', $e->getDetail());
53
54
            sleep( (int) $retryInSeconds );
55
56
            if($this->retriesCount <= $this->maxRetries) {
57
                return $this->getOpenOrders();
58
            }
59
60
            throw $e;
61
        }
62
    }
63
64
    /**
65
     * Returns a Bol.com order.
66
     * Null if order not found.
67
     *
68
     * @param string $orderId
69
     * @return Order|null
70
     */
71
    public function getOrder(string $orderId)
72
    {
73
        try {
74
            return Order::get($orderId);
75
        } catch (\Exception $e) {
76
            return null;
77
        }
78
    }
79
80
    /**
81
     * Ships a order item
82
     *
83
     * @param OrderItem $orderItem
84
     * @param Transport $transport
85
     * @return ProcessStatus
86
     */
87
    public function shipOrderItem(OrderItem $orderItem, Transport $transport): ProcessStatus
88
    {
89
        try {
90
            return Shipment::create($orderItem, [
91
                'shipmentReference' => $transport->shipmentReference,
92
                'transport' => [
93
                    'transporterCode' => $transport->transporterCode,
94
                    'trackAndTrace' => $transport->trackAndTraceCode
95
                ]
96
            ]);
97
98
        } catch (HttpException $e) {
99
            $this->retriesCount++;
100
            $retryInSeconds = str_replace(['Too many requests, retry in ', ' seconds.'], '', $e->getDetail());
101
102
            sleep( (int) $retryInSeconds );
103
104
            if($this->retriesCount <= $this->maxRetries) {
105
                return $this->shipOrderItem($orderItem, $transport);
106
            }
107
108
            throw $e;
109
        }
110
    }
111
}
112