IndexController::status()   B
last analyzed

Complexity

Conditions 8
Paths 66

Size

Total Lines 50
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 34
nc 66
nop 0
dl 0
loc 50
ccs 0
cts 40
cp 0
crap 72
rs 8.1315
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Canvas\Api\Controllers;
6
7
use Exception;
8
use PDOException;
9
use Phalcon\Http\Response;
0 ignored issues
show
Bug introduced by
The type Phalcon\Http\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Canvas\Http\Exception\InternalServerErrorException;
11
use RedisException;
12
use PhpAmqpLib\Exception\AMQPIOException ;
0 ignored issues
show
Bug introduced by
The type PhpAmqpLib\Exception\AMQPIOException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
/**
15
 * Class IndexController.
16
 *
17
 * @package Canvas\Api\Controllers
18
 *
19
 * @property Redis $redis
20
 * @property Beanstalk $queue
21
 * @property Mysql $db
22
 * @property \Monolog\Logger $log
23
 */
24
class IndexController extends BaseController
25
{
26
    /**
27
     * Index.
28
     *
29
     * @method GET
30
     * @url /
31
     *
32
     * @return Response
33
     */
34
    public function index($id = null) : Response
35
    {
36
        return $this->response(['Woot Baka']);
37
    }
38
39
    /**
40
     * Show the status of the diferent services.
41
     *
42
     * @method GET
43
     * @url /status
44
     *
45
     * @return Response
46
     */
47
    public function status() : Response
48
    {
49
        $response = [];
50
51
        //Try to connect to Redis
52
        try {
53
            $this->redis->hSet('htest', 'a', 'x');
54
            $this->redis->hSet('htest', 'b', 'y');
55
            $this->redis->hSet('htest', 'c', 'z');
56
            $this->redis->hSet('htest', 'd', 't');
57
            $this->redis->hGetAll('htest');
58
59
            //$this->redis->ping();
60
        } catch (RedisException $e) {
61
            $this->log->error($e->getMessage(), $e->getTrace());
62
            $response['errors']['redis'] = $e->getMessage();
63
        } catch (Exception $e) {
64
            $this->log->error("Redis isn't working. {$e->getMessage()}", $e->getTrace());
65
            $response['errors']['redis'] = "Redis isn't working.";
66
        }
67
68
        //Try to connect to RabbitMQ
69
        try {
70
            $this->queue;
71
        } catch (AMQPIOException $e) {
0 ignored issues
show
Unused Code introduced by
catch (\PhpAmqpLib\Exception\AMQPIOException $e) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
72
            $this->log->error($e->getMessage(), $e->getTrace());
73
            $response['errors']['RabbitMQ'] = $e->getMessage();
74
        } catch (Exception $e) {
75
            $this->log->error("RabbitMQ isn't working. {$e->getMessage()}", $e->getTrace());
76
            $response['errors']['RabbitMQ'] = "RabbitMQ isn't working.";
77
        } finally {
78
            $this->queue->close();
79
        }
80
81
        //Try to connect to db
82
        try {
83
            $this->db->connect();
84
        } catch (PDOException $e) {
85
            $this->log->error($e->getMessage(), $e->getTrace());
86
            $response['errors']['db'] = $e->getMessage();
87
        } catch (Exception $e) {
88
            $this->log->error("The database isn't working. {$e->getMessage()}", $e->getTrace());
89
            $response['errors']['db'] = "The database isn't working.";
90
        }
91
92
        if (!count($response)) {
93
            return $this->response(['OK']);
94
        }
95
96
        throw new InternalServerErrorException(json_encode($response));
97
    }
98
}
99