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 ( 7b7e14...7726e9 )
by Kevin
01:35
created

BolComRetailerService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 0
loc 89
rs 10
c 0
b 0
f 0

3 Methods

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