1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the slack-cli package. |
5
|
|
|
* |
6
|
|
|
* (c) Cas Leentfaar <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CL\SlackCli\Command; |
13
|
|
|
|
14
|
|
|
use CL\Slack\Payload\AuthTestPayload; |
15
|
|
|
use CL\Slack\Payload\AuthTestPayloadResponse; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Cas Leentfaar <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class AuthTestCommand extends AbstractApiCommand |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* {@inheritDoc} |
25
|
|
|
*/ |
26
|
1 |
|
protected function configure() |
27
|
|
|
{ |
28
|
1 |
|
parent::configure(); |
29
|
|
|
|
30
|
1 |
|
$this->setName('auth:test'); |
31
|
1 |
|
$this->setDescription('Test authentication with the Slack API and, optionally, tells you who you are (use -v).'); |
32
|
1 |
|
$this->setHelp(<<<EOT |
33
|
|
|
The <info>auth:test</info> command lets you test authenticating with the Slack API. |
34
|
|
|
|
35
|
|
|
Use the verbose option `-v` to also return information about the token's user. |
36
|
|
|
|
37
|
|
|
For more information about the related API method, check out the official documentation: |
38
|
|
|
<comment>https://api.slack.com/methods/auth.test</comment> |
39
|
|
|
EOT |
40
|
1 |
|
); |
41
|
1 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return AuthTestPayload |
45
|
|
|
*/ |
46
|
1 |
|
protected function createPayload() |
47
|
|
|
{ |
48
|
1 |
|
return new AuthTestPayload(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
* |
54
|
|
|
* @param AuthTestPayloadResponse $payloadResponse |
55
|
|
|
*/ |
56
|
1 |
|
protected function handleResponse($payloadResponse) |
57
|
|
|
{ |
58
|
1 |
|
if ($payloadResponse->isOk()) { |
59
|
1 |
|
$this->writeOk('Successfully authenticated by the Slack API!'); |
60
|
1 |
|
if ($this->output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) { |
61
|
|
|
$data = [ |
62
|
1 |
|
'User ID' => $payloadResponse->getUserId(), |
63
|
1 |
|
'Username' => $payloadResponse->getUsername(), |
64
|
1 |
|
'Team ID' => $payloadResponse->getTeamId(), |
65
|
1 |
|
'Team' => $payloadResponse->getTeam(), |
66
|
1 |
|
]; |
67
|
1 |
|
$this->renderKeyValueTable($data); |
68
|
1 |
|
} |
69
|
1 |
|
} else { |
70
|
1 |
|
$this->writeError(sprintf('Failed to be authenticated by the Slack API: %s', lcfirst($payloadResponse->getErrorExplanation()))); |
71
|
|
|
|
72
|
1 |
|
return 1; |
73
|
|
|
} |
74
|
1 |
|
} |
75
|
|
|
} |
76
|
|
|
|