Passed
Push — master ( 7c5bf0...a6c9f1 )
by Radu
06:08
created

Command::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ParcelValue\ApiClient\Domain\AuthenticationToken;
6
7
use ParcelValue\Api\AuthenticationToken;
8
use WebServCo\Framework\Cli\Ansi;
9
use WebServCo\Framework\Cli\Response;
10
use WebServCo\Framework\Cli\Sgr;
11
use WebServCo\Framework\Environment\Config;
12
use WebServCo\Framework\Interfaces\ResponseInterface;
13
14
final class Command extends \ParcelValue\ApiClient\AbstractController
15
{
16
    use \ParcelValue\ApiClient\Traits\ControllerApiTrait;
17
18
    protected string $jwt;
19
20
    protected \WebServCo\Framework\Interfaces\OutputLoggerInterface $outputLogger;
21
22
    public function __construct()
23
    {
24
        parent::__construct();
25
26
        $this->jwt = \ParcelValue\Api\AuthenticationToken::generate(
27
            Config::string('APP_API_CLIENT_ID'),
28
            Config::string('APP_API_CLIENT_KEY'),
29
            Config::string('APP_API_SERVER_KEY'),
30
        );
31
32
        $this->outputLogger = new \WebServCo\Framework\Log\CliOutputLogger();
33
    }
34
35
    public function generate(): ResponseInterface
36
    {
37
        $this->init();
38
39
        $this->outputLogger->output(Ansi::clear(), true);
40
        $this->outputLogger->output(Ansi::sgr(__METHOD__, [Sgr::BOLD]), true);
41
        $this->outputLogger->output('');
42
43
        $jwt = AuthenticationToken::generate(
44
            Config::string('APP_API_CLIENT_ID'),
45
            Config::string('APP_API_CLIENT_KEY'),
46
            Config::string('APP_API_SERVER_KEY'),
47
        );
48
        $this->outputLogger->output(Ansi::sgr('Success!', [Sgr::GREEN]), false);
49
        $this->outputLogger->output(' Your token is:', true);
50
        $this->outputLogger->output('');
51
        $this->outputLogger->output($jwt, true);
52
        $this->outputLogger->output('');
53
        return new Response();
54
    }
55
56
    public function validate(?string $token = null): ResponseInterface
57
    {
58
        $this->init();
59
60
        $this->outputLogger->output(Ansi::clear(), true);
61
        $this->outputLogger->output(Ansi::sgr(__METHOD__, [Sgr::BOLD]), true);
62
        $this->outputLogger->output('');
63
        $this->outputLogger->output(\sprintf('Input: %s', $token), true);
64
        $this->outputLogger->output('');
65
66
        try {
67
            if (!$token) {
68
                throw new \InvalidArgumentException('Token is missing.');
69
            }
70
            $result = AuthenticationToken::decode($token, Config::string('APP_API_SERVER_KEY'));
71
            $this->outputLogger->output(Ansi::sgr('Success!', [Sgr::GREEN]), true);
72
            $this->outputLogger->output(\var_export($result, true), true);
73
        } catch (\Throwable $e) {
74
            $this->outputLogger->output(Ansi::sgr(\sprintf('Error: %s', $e->getMessage()), [Sgr::RED]), true);
75
        }
76
77
        $this->outputLogger->output('');
78
        return new Response();
79
    }
80
}
81