ApiPing::execute()   C
last analyzed

Complexity

Conditions 20
Paths 2

Size

Total Lines 74
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 5.3202
c 0
b 0
f 0
nc 2
cc 20
eloc 52
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->getInitializer()->configureDi($test);
31
        $api = $test->get('Magium\Util\Api\ApiConfiguration');
32
        /* @var $api ApiConfiguration */
33
        if (!$api instanceof ApiConfiguration) {
0 ignored issues
show
Bug introduced by
The class Magium\Util\Api\ApiConfiguration does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
34
            throw new InvalidConfigurationException('Did not receive an instance of ApiConfiguration; received ' . get_class($api));
35
        }
36
        $api->setEnabled(true); // Gotta force this for this test
37
38
        $output->writeln('Sending un-authenticated payload...');
39
        $client = new Client('http://' . $api->getApiHostname(). '/api/ping');
40
        $request = $client->get();
41
        $response = $request->send();
42
        $output->writeln(
43
            'Checking for 200 status message... '
44
            . ($response->getStatusCode() == '200'?'OK':'Failed')
45
        );
46
        $output->writeln(
47
            'Checking for application/json content type... '
48
            . (stripos($response->getContentType(), 'application/json') !== false?'OK':'Failed')
49
        );
50
        $content = json_decode($response->getBody(), true);
51
        $output->writeln(
52
            'Checking for successful response... '
53
            . (is_array($content) && isset($content['status']) && $content['status'] === 'success'?'OK':'Failed')
54
        );
55
56
        $output->writeln('');
57
        $output->writeln('Attempting authenticated ping...');
58
59
        $request = $test->get('Magium\Util\Api\Request');
60
        /* @var $request \Magium\Util\Api\Request */
61
        $response = $request->fetch('/api/ping-authed');
62
63
        $output->writeln(
64
            'Checking for 200 status message... '
65
            . ($response->getStatusCode() == '200'?'OK':'Failed')
66
        );
67
        $output->writeln(
68
            'Checking for application/json content type... '
69
            . (stripos($response->getContentType(), 'application/json') !== false?'OK':'Failed')
70
        );
71
        $content = $request->getPayload($response);
72
        $output->writeln(
73
            'Checking for successful response... '
74
            . (is_array($content) && isset($content['status']) && $content['status'] === 'success'?'OK':'Failed')
75
        );
76
77
78
        $output->writeln('');
79
        $output->writeln('Attempting authenticated echo...');
80
        $request = $test->get('Magium\Util\Api\Request');
81
        /* @var $request \Magium\Util\Api\Request */
82
        $response = $request->push('/api/echo-authed', ['message' => 'hello world']);
83
        $output->writeln(
84
            'Checking for 200 status message... '
85
            . ($response->getStatusCode() == '200'?'OK':'Failed')
86
        );
87
        $output->writeln(
88
            'Checking for application/json content type... '
89
            . (stripos($response->getContentType(), 'application/json') !== false?'OK':'Failed')
90
        );
91
        $content = $request->getPayload($response);
92
        $output->writeln(
93
            'Checking for successful response... '
94
            . (is_array($content) && isset($content['status']) && $content['status'] === 'success'?'OK':'Failed')
95
        );
96
        $output->writeln(
97
            'Checking for matching echo message... '
98
            . (is_array($content) && isset($content['message']) && $content['message'] === 'hello world'?'OK':'Failed')
99
        );
100
    }
101
    
102
}