Completed
Push — master ( fdf082...9ad6b1 )
by Kevin
08:56 queued 05:59
created

ApiPing::execute()   C

Complexity

Conditions 19
Paths 1

Size

Total Lines 71
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 71
rs 5.4997
cc 19
eloc 50
nc 1
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Magium\Cli\Command;
4
5
use Guzzle\Http\Client;
6
use Magium\Cli\Command\Test\TestSkeleton;
7
use Magium\InvalidConfigurationException;
8
use Magium\NotFoundException;
9
use Magium\Util\Api\ApiConfiguration;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\ArrayInput;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
class ApiPing extends Command
18
{
19
20
    protected function configure()
21
    {
22
        $this->setName('api:ping');
23
        $this->setDescription('Pings the API ping endpoint to ensure that the key works');
24
25
    }
26
27
    protected function execute(InputInterface $input, OutputInterface $output)
28
    {
29
        $test = new TestSkeleton();
30
        $test->configureDi();
31
        $api = $test->get('Magium\Util\Api\ApiConfiguration');
32
        /* @var $api ApiConfiguration */
33
        $api->setEnabled(true); // Gotta force this for this test
34
35
        $output->writeln('Sending un-authenticated payload...');
36
        $client = new Client('http://' . $api->getApiHostname(). '/api/ping');
37
        $request = $client->get();
38
        $response = $request->send();
39
        $output->writeln(
40
            'Checking for 200 status message... '
41
            . ($response->getStatusCode() == '200'?'OK':'Failed')
42
        );
43
        $output->writeln(
44
            'Checking for application/json content type... '
45
            . (stripos($response->getContentType(), 'application/json') !== false?'OK':'Failed')
46
        );
47
        $content = json_decode($response->getBody(), true);
48
        $output->writeln(
49
            'Checking for successful response... '
50
            . (is_array($content) && isset($content['status']) && $content['status'] === 'success'?'OK':'Failed')
51
        );
52
53
        $output->writeln('');
54
        $output->writeln('Attempting authenticated ping...');
55
56
        $request = $test->get('Magium\Util\Api\Request');
57
        /* @var $request \Magium\Util\Api\Request */
58
        $response = $request->fetch('/api/ping-authed');
59
60
        $output->writeln(
61
            'Checking for 200 status message... '
62
            . ($response->getStatusCode() == '200'?'OK':'Failed')
63
        );
64
        $output->writeln(
65
            'Checking for application/json content type... '
66
            . (stripos($response->getContentType(), 'application/json') !== false?'OK':'Failed')
67
        );
68
        $content = $request->getPayload($response);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $request->fetch('/api/ping-authed') on line 58 can also be of type array or null; however, Magium\Util\Api\Request::getPayload() does only seem to accept object<Guzzle\Http\Message\Response>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
69
        $output->writeln(
70
            'Checking for successful response... '
71
            . (is_array($content) && isset($content['status']) && $content['status'] === 'success'?'OK':'Failed')
72
        );
73
74
75
        $output->writeln('');
76
        $output->writeln('Attempting authenticated echo...');
77
        $request = $test->get('Magium\Util\Api\Request');
78
        /* @var $request \Magium\Util\Api\Request */
79
        $response = $request->push('/api/echo-authed', ['message' => 'hello world']);
80
        $output->writeln(
81
            'Checking for 200 status message... '
82
            . ($response->getStatusCode() == '200'?'OK':'Failed')
83
        );
84
        $output->writeln(
85
            'Checking for application/json content type... '
86
            . (stripos($response->getContentType(), 'application/json') !== false?'OK':'Failed')
87
        );
88
        $content = $request->getPayload($response);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $request->push('/api/ech...age' => 'hello world')) on line 79 can also be of type array or null; however, Magium\Util\Api\Request::getPayload() does only seem to accept object<Guzzle\Http\Message\Response>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
89
        $output->writeln(
90
            'Checking for successful response... '
91
            . (is_array($content) && isset($content['status']) && $content['status'] === 'success'?'OK':'Failed')
92
        );
93
        $output->writeln(
94
            'Checking for matching echo message... '
95
            . (is_array($content) && isset($content['message']) && $content['message'] === 'hello world'?'OK':'Failed')
96
        );
97
    }
98
    
99
}