Shipments   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A types() 0 13 1
A locations() 0 16 1
A create() 0 24 1
A list() 0 20 1
A labelUrl() 0 13 1
A downloadLabelPdf() 0 22 2
1
<?php
2
3
namespace JeroenBoesten\ShopsUnitedLaravel\Modules;
4
5
use Illuminate\Support\Arr;
6
use JeroenBoesten\ShopsUnitedLaravel\ShopsUnitedLaravel;
7
8
class Shipments extends ShopsUnitedLaravel
9
{
10
    /**
11
     * Returns a Array with shipment type objects.
12
     * @return mixed
13
     */
14
    public function types()
15
    {
16
        $data = $this
17
            ->addQuery([
18
                'GebruikerId' => config('shops-united-laravel.account-id'),
19
                'Datum' => date('Y-m-d H:i:s'),
20
                'HmacSha256' => hash_hmac('sha256', config('shops-united-laravel.account-id').date('Y-m-d H:i:s'), config('shops-united-laravel.api-key')),
21
            ])
22
            ->setGetUrl('type.php')
23
            ->call();
24
25
        return json_decode(json_encode($data));
26
    }
27
28
    /**
29
     * Get the 20 nearest locations for the user to use the 'Pakje Gemak' service.
30
     * @param $zipCode
31
     * @param $houseNumber
32
     * @param string $carrier default 'PostNL' or 'DHL'
33
     * @return mixed
34
     */
35
    public function locations($zipCode, $houseNumber, $carrier = 'PostNL')
36
    {
37
        $data = $this
38
            ->addQuery([
39
                'GebruikerId' => config('shops-united-laravel.account-id'),
40
                'Datum' => date('Y-m-d H:i:s'),
41
                'Carrier' => $carrier,
42
                'Postcode' => $zipCode,
43
                'Nummer' => $houseNumber,
44
                'HmacSha256' => hash_hmac('sha256', config('shops-united-laravel.account-id').date('Y-m-d H:i:s').$zipCode.$houseNumber, config('shops-united-laravel.api-key')),
45
            ])
46
            ->setGetUrl('uitreiklocatie.php')
47
            ->call();
48
49
        return json_decode(json_encode($data));
50
    }
51
52
    /**
53
     * Create a new shipment to see the additional optional params visit https://login.shops-united.nl/api/docs.php#zending.
54
     *
55
     * @param string $carrier
56
     * @param string $type
57
     * @param string $reference
58
     * @param string $addresseeName
59
     * @param string $addresseeStreet
60
     * @param string $addresseeHouseNumber
61
     * @param string $addresseeZipCode
62
     * @param string $addresseeCity
63
     * @param int $packagesAmount
64
     * @param int $weight
65
     * @param array $optionalParams
66
     * @return mixed
67
     */
68
    public function create(string $carrier, string $type, string $reference, string $addresseeName, string $addresseeStreet, string $addresseeHouseNumber,
69
                           string $addresseeZipCode, string $addresseeCity, int $packagesAmount, int $weight, array $optionalParams = [])
70
    {
71
        $data = $this
72
            ->setParams(array_merge([
73
                'GebruikerId' => config('shops-united-laravel.account-id'),
74
                'Datum' => date('Y-m-d H:i:s'),
75
                'Carrier' => $carrier,
76
                'Type' => $type,
77
                'Referentie' => $reference,
78
                'Naam' => $addresseeName,
79
                'Straat' => $addresseeStreet,
80
                'Nummer' => $addresseeHouseNumber,
81
                'Postcode' => $addresseeZipCode,
82
                'Plaats' => $addresseeCity,
83
                'AantalPakketten' => $packagesAmount,
84
                'Gewicht' => $weight,
85
                'HmacSha256' => hash_hmac('sha256', config('shops-united-laravel.account-id').date('Y-m-d H:i:s').Arr::get($optionalParams, 'PostcodeAfzender', '').$addresseeZipCode, config('shops-united-laravel.api-key')),
86
            ], $optionalParams))
87
            ->setPostUrl('zending.php')
88
            ->call();
89
90
        return json_decode(json_encode($data));
91
    }
92
93
    public function list(int $shipmentId = null, string $orderBy = 'desc', string $minDateTime = null, string $maxDateTime = null, string $status = null, int $limit = 50)
94
    {
95
        $data = $this
96
            ->addQuery([
97
                'GebruikerId' => config('shops-united-laravel.account-id'),
98
                'Datum' => date('Y-m-d H:i:s'),
99
                'HmacSha256' => hash_hmac('sha256', config('shops-united-laravel.account-id').date('Y-m-d H:i:s'), config('shops-united-laravel.api-key')),
100
101
                'ZendingId' => $shipmentId,
102
                'MinDatetime' => $minDateTime,
103
                'MaxDatetime' => $maxDateTime,
104
                'Status' => $status,
105
                'Ordering' => $orderBy,
106
                'Limit' => $limit,
107
            ])
108
            ->setGetUrl('zendingen.php')
109
            ->call();
110
111
        return json_decode(json_encode($data));
112
    }
113
114
    public function labelUrl(int $shipmentId, bool $printPdf = false)
115
    {
116
        $data = $this
117
            ->addQuery([
118
                'GebruikerId' => config('shops-united-laravel.account-id'),
119
                'ZendingId' => $shipmentId,
120
                'PrintPdf' => $printPdf,
121
                'HmacSha256' => hash_hmac('sha256', config('shops-united-laravel.account-id').$shipmentId, config('shops-united-laravel.api-key')),
122
            ])
123
            ->setGetUrl('label.php');
124
125
        return $data->callableUrl.$data->query;
126
    }
127
128
    public function downloadLabelPdf(int $shipmentId, string $downloadPath = 'storage_dir', string $prefix = 'label_')
129
    {
130
        $labelUrl = $this->labelUrl($shipmentId, true);
131
132
        $jar = new \GuzzleHttp\Cookie\CookieJar();
133
        $client = new \GuzzleHttp\Client();
134
        $response = $client->request(
135
            'POST',
136
            $labelUrl,
137
            ['cookies' => $jar]
138
        );
139
140
        if ($downloadPath === 'storage_dir') {
141
            $downloadPath = storage_path();
142
        }
143
144
        $filePath = "{$downloadPath}/{$prefix}{$shipmentId}.pdf";
145
146
        file_put_contents($filePath, $response->getBody()->getContents());
147
148
        return $filePath;
149
    }
150
}
151