Passed
Push — master ( 4438f7...71dff0 )
by Radu
01:11
created

ShipmentsCommand::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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