Test Failed
Branch add-core-tests (a060d8)
by Rafael
05:48
created

IndexController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
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;
10
use Canvas\Exception\ServerErrorHttpException;
11
use RedisException;
12
use PhpAmqpLib\Exception\AMQPIOException ;
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 ServerErrorHttpException(json_encode($response));
97
    }
98
}
99