Failed Conditions
Pull Request — master (#10)
by Maximo
03:34
created

IndexController::status()   C

Complexity

Conditions 8
Paths 528

Size

Total Lines 50
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 21.3931

Importance

Changes 0
Metric Value
cc 8
eloc 34
nc 528
nop 0
dl 0
loc 50
ccs 13
cts 32
cp 0.4063
crap 21.3931
rs 5.3226
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style introduced by
End of line character is invalid; expected "\n" but found "\r\n"
Loading history...
2
declare(strict_types=1);
3
4
namespace Gewaer\Api\Controllers;
5
6
use Exception;
7
use PDOException;
8
use Phalcon\Http\Response;
9
use Phalcon\Queue\Beanstalk\Exception as BeanstalkException;
10
use Gewaer\Exception\ServerErrorHttpException;
11
use RedisException;
12
13
/**
14
 * Class IndexController
15
 *
16
 * @package Gewaer\Api\Controllers
17
 *
18
 * @property Redis $redis
19
 * @property Beanstalk $queue
20
 * @property Mysql $db
21
 */
22
class IndexController extends BaseController
23
{
24
    /**
25
     * Index
26
     *
27
     * @method GET
28
     * @url /
29
     *
30
     * @return Phalcon\Http\Response
0 ignored issues
show
Bug introduced by
The type Gewaer\Api\Controllers\Phalcon\Http\Response was not found. Did you mean Phalcon\Http\Response? If so, make sure to prefix the type with \.
Loading history...
31
     */
32
    public function index($id = null) : Response
33
    {
34
        return $this->response(['Woot Baka']);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response(array('Woot Baka')) returns the type Phalcon\Http\Response which is incompatible with the documented return type Gewaer\Api\Controllers\Phalcon\Http\Response.
Loading history...
35
    }
36
37
    /**
38
     * Show the status of the diferent services
39
     *
40
     * @method GET
41
     * @url /status
42
     *
43
     * @return Phalcon\Http\Response
44
     */
45 1
    public function status() : Response
46
    {
47 1
        $response = [];
48
49
        //Try to connect to Redis
50
        try {
51 1
            $this->redis->hSet('htest', 'a', 'x');
52 1
            $this->redis->hSet('htest', 'b', 'y');
53 1
            $this->redis->hSet('htest', 'c', 'z');
54 1
            $this->redis->hSet('htest', 'd', 't');
55 1
            $this->redis->hGetAll('htest');
56
57
            //$this->redis->ping();
58
        } catch (RedisException $e) {
59
            $this->log->error($e->getMessage(), $e->getTrace());
0 ignored issues
show
Bug Best Practice introduced by
The property log does not exist on Gewaer\Api\Controllers\IndexController. Since you implemented __get, consider adding a @property annotation.
Loading history...
60
            $response['errors']['redis'] = $e->getMessage();
61
        } catch (Exception $e) {
62
            $this->log->error("Redis isn't working. {$e->getMessage()}", $e->getTrace());
63
            $response['errors']['redis'] = "Redis isn't working.";
64
        }
65
66
        //Try to connect to Beanstalk
67
        try {
68 1
            $this->queue->connect();
69
        } catch (BeanstalkException $e) {
70
            $this->log->error($e->getMessage(), $e->getTrace());
71
            $response['errors']['beanstalk'] = $e->getMessage();
72
        } catch (Exception $e) {
73
            $this->log->error("Beanstalk isn't working. {$e->getMessage()}", $e->getTrace());
74
            $response['errors']['beanstalk'] = "Beanstalk isn't working.";
75 1
        } finally {
76 1
            $this->queue->disconnect();
77
        }
78
79
        //Try to connect to db
80
        try {
81 1
            $this->db->connect();
82
        } catch (PDOException $e) {
83
            $this->log->error($e->getMessage(), $e->getTrace());
84
            $response['errors']['db'] = $e->getMessage();
85
        } catch (Exception $e) {
86
            $this->log->error("The database isn't working. {$e->getMessage()}", $e->getTrace());
87
            $response['errors']['db'] = "The database isn't working.";
88
        }
89
90 1
        if (!count($response)) {
91 1
            return $this->response(['OK']);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response(array('OK')) returns the type Phalcon\Http\Response which is incompatible with the documented return type Gewaer\Api\Controllers\Phalcon\Http\Response.
Loading history...
92
        }
93
94
        throw new ServerErrorHttpException(json_encode($response));
95
    }
96
}
97