1
|
|
|
<?php |
2
|
|
|
namespace ParcelValue\ApiClient\Domain\Clients; |
3
|
|
|
|
4
|
|
|
use ParcelValue\Api\AuthenticationToken; |
5
|
|
|
|
6
|
|
|
use WebServCo\Framework\Cli\Ansi; |
7
|
|
|
use WebServCo\Framework\Cli\Response; |
8
|
|
|
use WebServCo\Framework\Cli\Sgr; |
9
|
|
|
use WebServCo\Framework\Http\Method; |
10
|
|
|
|
11
|
|
|
final class Command extends \ParcelValue\ApiClient\AbstractController |
12
|
|
|
{ |
13
|
|
|
protected $jwt; |
14
|
|
|
|
15
|
|
|
use \ParcelValue\ApiClient\Traits\ControllerApiTrait; |
16
|
|
|
|
17
|
|
|
public function __construct() |
18
|
|
|
{ |
19
|
|
|
parent::__construct(); |
20
|
|
|
|
21
|
|
|
$this->repository = new Repository($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
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function current() |
33
|
|
|
{ |
34
|
|
|
$this->outputCli(Ansi::clear(), true); |
35
|
|
|
$this->outputCli(Ansi::sgr(__METHOD__, [Sgr::BOLD]), true); |
36
|
|
|
|
37
|
|
|
$url = sprintf('%s%s/clients/current', $this->apiUrl, $this->apiVersion); |
38
|
|
|
|
39
|
|
|
$this->handleApiCall($this->jwt, $url, Method::GET); |
40
|
|
|
|
41
|
|
|
return new Response('', true); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function generateAuthenticationToken() |
45
|
|
|
{ |
46
|
|
|
$this->outputCli(Ansi::clear(), true); |
47
|
|
|
$this->outputCli(Ansi::sgr(__METHOD__, [Sgr::BOLD]), true); |
48
|
|
|
$this->outputCli(); |
|
|
|
|
49
|
|
|
|
50
|
|
|
$jwt = AuthenticationToken::generate($this->clientId, $this->clientKey, $this->serverKey); |
51
|
|
|
$this->outputCli(Ansi::sgr('Success!', [Sgr::GREEN]), false); |
52
|
|
|
$this->outputCli(' Your token is:', true); |
53
|
|
|
$this->outputCli(); |
54
|
|
|
$this->outputCli($jwt, true); |
55
|
|
|
$this->outputCli(); |
56
|
|
|
return new \WebServCo\Framework\Cli\Response('', true); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function validateAuthenticationToken($token) |
60
|
|
|
{ |
61
|
|
|
$this->outputCli(Ansi::clear(), true); |
62
|
|
|
$this->outputCli(Ansi::sgr(__METHOD__, [Sgr::BOLD]), true); |
63
|
|
|
$this->outputCli(); |
|
|
|
|
64
|
|
|
$this->outputCli(sprintf('Input: %s', $token), true); |
65
|
|
|
$this->outputCli(); |
66
|
|
|
|
67
|
|
|
try { |
68
|
|
|
$result = AuthenticationToken::decode($token, $this->serverKey); |
69
|
|
|
$this->outputCli(Ansi::sgr('Success!', [Sgr::GREEN]), true); |
70
|
|
|
$this->outputCli(var_export($result, true), true); |
71
|
|
|
} catch (\Exception $e) { |
72
|
|
|
$this->outputCli( |
73
|
|
|
Ansi::sgr( |
74
|
|
|
sprintf('Error: %s', $e->getMessage()), |
75
|
|
|
[Sgr::RED] |
76
|
|
|
), |
77
|
|
|
true |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->outputCli(); |
82
|
|
|
return new \WebServCo\Framework\Cli\Response('', true); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.