Passed
Push — master ( e08493...2442d1 )
by Radu
01:22
created

ShipmentsCommand::retrieve()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
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\Sgr;
7
use WebServCo\Framework\CliResponse;
8
use WebServCo\Framework\Http;
9
10
final class ShipmentsCommand extends \ParcelValue\ApiClient\AbstractController
11
{
12
    protected $jwt;
13
    protected $headers;
14
15
    use \ParcelValue\ApiClient\Traits\ControllerApiTrait;
16
17
    public function __construct()
18
    {
19
        parent::__construct();
20
21
        $this->repository = new ShipmentsRepository($this->outputLoader);
22
23
        $this->validateApiConfig();
24
25
        $this->jwt = \ParcelValue\Api\AuthenticationToken::generate(
26
            $this->clientId,
27
            $this->clientKey,
28
            $this->serverKey
29
        );
30
        $this->headers = ['Authorization' => sprintf('Bearer %s', $this->jwt)];
31
    }
32
33
    public function create()
34
    {
35
        $this->outputCli(Ansi::clear(), true);
36
        $this->outputCli(Ansi::sgr(__METHOD__, [Sgr::BOLD]), true);
37
38
        $this->initApiCall();
39
40
        $url = sprintf('%s%s/shipments', $this->apiUrl, $this->apiVersion);
41
42
        $shipment = $this->repository->getShipment();
43
        $document = new Document();
44
        $document->setData($shipment);
45
46
        $this->headers['Content-Type'] = Document::CONTENT_TYPE;
47
48
        $this->handleApiCall($url, Http::METHOD_POST, $this->headers, $document->toJson());
49
50
        return new CliResponse('', true);
51
    }
52
53
    public function retrieve($id)
54
    {
55
        $this->outputCli(Ansi::clear(), true);
56
        $this->outputCli(Ansi::sgr(__METHOD__, [Sgr::BOLD]), true);
57
58
        $this->initApiCall();
59
60
        $url = sprintf('%s%s/shipments/%s', $this->apiUrl, $this->apiVersion, $id);
61
62
        $this->handleApiCall($url, Http::METHOD_GET, $this->headers);
63
64
        return new CliResponse('', true);
65
    }
66
}
67