Completed
Push — master ( 30ce68...607ba7 )
by Radu
05:54 queued 01:08
created

Command::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
namespace ParcelValue\ApiClient\Domain\Shipments;
3
4
use WebServCo\Api\JsonApi\Document;
5
use WebServCo\Framework\Cli\Ansi;
6
use WebServCo\Framework\Cli\Response;
7
use WebServCo\Framework\Cli\Sgr;
8
use WebServCo\Framework\Http\Method;
9
10
final class Command extends \ParcelValue\ApiClient\AbstractController
11
{
12
    protected $jwt;
13
14
    use \ParcelValue\ApiClient\Traits\ControllerApiTrait;
15
16
    public function __construct()
17
    {
18
        parent::__construct();
19
20
        $this->repository = new Repository($this->outputLoader);
21
22
        $this->validateApiConfig();
23
24
        $this->jwt = \ParcelValue\Api\AuthenticationToken::generate(
25
            $this->clientId,
26
            $this->clientKey,
27
            $this->serverKey
28
        );
29
    }
30
31
    public function create()
32
    {
33
        $this->outputCli(Ansi::clear(), true);
34
        $this->outputCli(Ansi::sgr(__METHOD__, [Sgr::BOLD]), true);
35
36
        $url = sprintf('%s%s/shipments', $this->apiUrl, $this->apiVersion);
37
38
        $shipment = $this->repository->getShipment();
39
        $document = new Document();
40
        $document->setData($shipment);
41
42
        $this->handleApiCall($this->jwt, $url, Method::POST, $document->toJson());
43
44
        return new Response('', true);
45
    }
46
47
    public function retrieve($shipmentId)
48
    {
49
        $this->outputCli(Ansi::clear(), true);
50
        $this->outputCli(Ansi::sgr(__METHOD__, [Sgr::BOLD]), true);
51
52
        $url = sprintf('%s%s/shipments/%s', $this->apiUrl, $this->apiVersion, $shipmentId);
53
54
        $this->handleApiCall($this->jwt, $url, Method::GET);
55
56
        return new Response('', true);
57
    }
58
59
    public function downloadDocuments($shipmentId)
60
    {
61
        $this->outputCli(Ansi::clear(), true);
62
        $this->outputCli(Ansi::sgr(__METHOD__, [Sgr::BOLD]), true);
63
64
        $url = sprintf('%s%s/shipments/%s/documents', $this->apiUrl, $this->apiVersion, $shipmentId);
65
66
        $this->handleApiCall($this->jwt, $url, Method::GET);
67
68
        $data = json_decode($this->responseContent, true);
69
        if (isset($data['data']['attributes']['fileData']) && isset($data['data']['attributes']['fileName'])) {
70
            $filePath = sprintf(
71
                '%svar/tmp/%s',
72
                $this->config()->get('app/path/project'),
73
                $data['data']['attributes']['fileName']
74
            );
75
            try {
76
                file_put_contents($filePath, base64_decode($data['data']['attributes']['fileData']));
77
                $this->outputCli(Ansi::sgr(sprintf('Shipment documents saved: %s', $filePath), [Sgr::GREEN]), true);
78
            } catch (\Exception $e) {
79
                $this->outputCli(
80
                    Ansi::sgr(sprintf('Error saving shipment documents: %s', $e->getMessage()), [Sgr::RED]),
81
                    true
82
                );
83
            }
84
        }
85
        return new Response('', true);
86
    }
87
}
88