ApiPing   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 21
lcom 0
cbo 4
dl 0
loc 86
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 6 1
C execute() 0 74 20
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
}