Failed Conditions
Pull Request — master (#10)
by Maximo
03:08
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
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Api\Controllers;
6
7
use Exception;
8
use PDOException;
9
use Phalcon\Http\Response;
10
use Phalcon\Queue\Beanstalk\Exception as BeanstalkException;
11
use Gewaer\Exception\ServerErrorHttpException;
12
use RedisException;
13
14
/**
15
 * Class IndexController
16
 *
17
 * @package Gewaer\Api\Controllers
18
 *
19
 * @property Redis $redis
20
 * @property Beanstalk $queue
21
 * @property Mysql $db
22
 */
23
class IndexController extends BaseController
24
{
25
    /**
26
     * Index
27
     *
28
     * @method GET
29
     * @url /
30
     *
31
     * @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...
32
     */
33
    public function index($id = null) : Response
34
    {
35
        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...
36
    }
37
38
    /**
39
     * Show the status of the diferent services
40
     *
41
     * @method GET
42
     * @url /status
43
     *
44
     * @return Phalcon\Http\Response
45
     */
46 1
    public function status() : Response
47
    {
48 1
        $response = [];
49
50
        //Try to connect to Redis
51
        try {
52 1
            $this->redis->hSet('htest', 'a', 'x');
53 1
            $this->redis->hSet('htest', 'b', 'y');
54 1
            $this->redis->hSet('htest', 'c', 'z');
55 1
            $this->redis->hSet('htest', 'd', 't');
56 1
            $this->redis->hGetAll('htest');
57
58
            //$this->redis->ping();
59
        } catch (RedisException $e) {
60
            $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...
61
            $response['errors']['redis'] = $e->getMessage();
62
        } catch (Exception $e) {
63
            $this->log->error("Redis isn't working. {$e->getMessage()}", $e->getTrace());
64
            $response['errors']['redis'] = "Redis isn't working.";
65
        }
66
67
        //Try to connect to Beanstalk
68
        try {
69 1
            $this->queue->connect();
70
        } catch (BeanstalkException $e) {
71
            $this->log->error($e->getMessage(), $e->getTrace());
72
            $response['errors']['beanstalk'] = $e->getMessage();
73
        } catch (Exception $e) {
74
            $this->log->error("Beanstalk isn't working. {$e->getMessage()}", $e->getTrace());
75
            $response['errors']['beanstalk'] = "Beanstalk isn't working.";
76 1
        } finally {
77 1
            $this->queue->disconnect();
78
        }
79
80
        //Try to connect to db
81
        try {
82 1
            $this->db->connect();
83
        } catch (PDOException $e) {
84
            $this->log->error($e->getMessage(), $e->getTrace());
85
            $response['errors']['db'] = $e->getMessage();
86
        } catch (Exception $e) {
87
            $this->log->error("The database isn't working. {$e->getMessage()}", $e->getTrace());
88
            $response['errors']['db'] = "The database isn't working.";
89
        }
90
91 1
        if (!count($response)) {
92 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...
93
        }
94
95
        throw new ServerErrorHttpException(json_encode($response));
96
    }
97
}
98