Test Failed
Push — master ( 90c56e...d0ec41 )
by Maximo
02:16
created

IndexController::status()   C

Complexity

Conditions 8
Paths 528

Size

Total Lines 50
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 17.5929

Importance

Changes 0
Metric Value
cc 8
eloc 34
nc 528
nop 0
dl 0
loc 50
ccs 15
cts 32
cp 0.4688
crap 17.5929
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
12
/**
13
 * Base controller
14
 *
15
 */
16
class IndexController extends BaseController
17
{
18
    /**
19
     * Index
20
     *
21
     * @method GET
22
     * @url /
23
     *
24
     * @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...
25
     */
26
    public function index($id = null) : Response
27
    {
28
        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...
29
    }
30
31
    /**
32
     * Show the status of the diferent services
33
     *
34
     * @method GET
35
     * @url /status
36
     *
37
     * @return Phalcon\Http\Response
38
     */
39 1
    public function status() : Response
40
    {
41 1
        $response = [];
42
43
        //Try to connect to Redis
44
        try {
45 1
            $this->redis->hSet('htest', 'a', 'x');
0 ignored issues
show
Bug Best Practice introduced by
The property redis does not exist on Gewaer\Api\Controllers\IndexController. Since you implemented __get, consider adding a @property annotation.
Loading history...
46
            $this->redis->hSet('htest', 'b', 'y');
47
            $this->redis->hSet('htest', 'c', 'z');
48
            $this->redis->hSet('htest', 'd', 't');
49
            $this->redis->hGetAll('htest');
50
51
            //$this->redis->ping();
52 1
        } catch (\RedisException $e) {
53 1
            $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...
54 1
            $response['errors']['redis'] = $e->getMessage();
55
        } catch (Exception $e) {
56
            $this->log->error("Redis isn't working. {$e->getMessage()}", $e->getTrace());
57
            $response['errors']['redis'] = "Redis isn't working.";
58
        }
59
60
        //Try to connect to Beanstalk
61
        try {
62 1
            $this->queue->connect();
0 ignored issues
show
Bug Best Practice introduced by
The property queue does not exist on Gewaer\Api\Controllers\IndexController. Since you implemented __get, consider adding a @property annotation.
Loading history...
63 1
        } catch (BeanstalkException $e) {
64 1
            $this->log->error($e->getMessage(), $e->getTrace());
65 1
            $response['errors']['beanstalk'] = $e->getMessage();
66
        } catch (Exception $e) {
67
            $this->log->error("Beanstalk isn't working. {$e->getMessage()}", $e->getTrace());
68
            $response['errors']['beanstalk'] = "Beanstalk isn't working.";
69 1
        } finally {
70 1
            $this->queue->disconnect();
71
        }
72
73
        //Try to connect to db
74
        try {
75 1
            $this->db->connect();
76
        } catch (PDOException $e) {
77
            $this->log->error($e->getMessage(), $e->getTrace());
78
            $response['errors']['db'] = $e->getMessage();
79
        } catch (Exception $e) {
80
            $this->log->error("The database isn't working. {$e->getMessage()}", $e->getTrace());
81
            $response['errors']['db'] = "The database isn't working.";
82
        }
83
84 1
        if (!count($response)) {
85
            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...
86
        }
87
88 1
        throw new ServerErrorHttpException(json_encode($response));
89
    }
90
}
91