GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ApiTestCommand   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 97.67%

Importance

Changes 6
Bugs 1 Features 1
Metric Value
wmc 11
c 6
b 1
f 1
lcom 2
cbo 4
dl 0
loc 92
ccs 42
cts 43
cp 0.9767
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 22 1
A createPayload() 0 21 4
B handleResponse() 0 28 6
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\ApiTestPayload;
15
use CL\Slack\Payload\ApiTestPayloadResponse;
16
use Symfony\Component\Console\Input\InputOption;
17
18
/**
19
 * @author Cas Leentfaar <[email protected]>
20
 */
21
class ApiTestCommand extends AbstractApiCommand
22
{
23
    /**
24
     * @var bool
25
     */
26
    private $expectError = false;
27
28
    /**
29
     * {@inheritDoc}
30
     */
31 1
    protected function configure()
32
    {
33 1
        parent::configure();
34
35 1
        $this->setName('api:test');
36 1
        $this->setDescription('Tests connecting with the Slack API using the token.');
37 1
        $this->addOption('arguments', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Arguments to be tested in key:value format, such as "foo:bar"');
38 1
        $this->addOption('error', null, InputOption::VALUE_REQUIRED, 'Optional error message to mock an error response from Slack with');
39 1
        $this->setHelp(<<<EOT
40
The <info>api:test</info> command lets you connect with the Slack API for testing purposes.
41
42
Testing arguments returned by Slack
43
<info>php bin/slack api.test --args="foo=bar&apple=pie"</info>
44
45
Testing an error response
46
<info>php bin/slack api.test --error="This is my error"</info>
47
48
For more information about the related API method, check out the official documentation:
49
<comment>https://api.slack.com/methods/api.test</comment>
50
EOT
51 1
        );
52 1
    }
53
54
    /**
55
     * @return ApiTestPayload
56
     */
57 1
    protected function createPayload()
58
    {
59 1
        $payload = new ApiTestPayload();
60
61 1
        if ($this->input->getOption('arguments')) {
62 1
            $args = [];
63 1
            foreach ($this->input->getOption('arguments') as $keyValue) {
64 1
                list($key, $value) = explode(':', $keyValue);
65 1
                $args[$key] = $value;
66 1
            }
67
68 1
            $payload->replaceArguments($args);
69 1
        }
70
71 1
        if ($this->input->getOption('error')) {
72 1
            $this->expectError = true;
73 1
            $payload->setError($this->input->getOption('error'));
74 1
        }
75
76 1
        return $payload;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     *
82
     * @param ApiTestPayloadResponse $payloadResponse
83
     */
84 1
    protected function handleResponse($payloadResponse)
85
    {
86 1
        if ($this->expectError === true || $payloadResponse->isOk()) {
87 1
            $this->writeOk('Slack API seems to have responded correctly (no error expected, no error returned)');
88 1
            $data = [];
89 1
            if (null !== $error = $payloadResponse->getError()) {
90 1
                $data['error'] = $error;
91 1
            }
92 1
            foreach ($payloadResponse->getArguments() as $key => $val) {
93 1
                if ($key == 'token') {
94
                    continue;
95
                }
96
97 1
                $data['args'][$key] = $val;
98 1
            }
99 1
            $this->renderKeyValueTable($data);
100
101
            // force 0 so any error tested here won't trigger a failure
102 1
            return 0;
103
        } else {
104 1
            $this->writeError(sprintf(
105 1
                'Slack API did not respond correctly (no error expected): %s',
106 1
                lcfirst($payloadResponse->getErrorExplanation())
107 1
            ));
108
109 1
            return 1;
110
        }
111
    }
112
}
113