Test Failed
Push — master ( cb9afd...2b9e35 )
by Maximo
02:09
created

IndexController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 3 1
C status() 0 59 8
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
11
/**
12
 * Base controller
13
 *
14
 */
15
class IndexController extends BaseController
16
{
17
    /**
18
     * Index
19
     *
20
     * @method GET
21
     * @url /
22
     *
23
     * @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...
24
     */
25
    public function index($id = null) : Response
26
    {
27
        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...
28
    }
29
30
    /**
31
     * Show the status of the diferent services
32
     *
33
     * @method GET
34
     * @url /status
35
     *
36
     * @return Phalcon\Http\Response
37
     */
38
    public function status() : Response
39
    {
40
        $response = [];
41
42
        //Try to connect to Redis
43
        try {
44
            $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...
45
            $this->redis->hSet('htest', 'b', 'y');
46
            $this->redis->hSet('htest', 'c', 'z');
47
            $this->redis->hSet('htest', 'd', 't');
48
            $this->redis->hGetAll('htest');
49
50
            //$this->redis->ping();
51
        } catch (\RedisException $e) {
52
            $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...
53
            $response['errors']['redis'] = $e->getMessage();
54
        } catch (Exception $e) {
55
            $this->log->error("Redis isn't working. {$e->getMessage()}", $e->getTrace());
56
            $response['errors']['redis'] = "Redis isn't working.";
57
        }
58
59
        //Try to connect to Beanstalk
60
        try {
61
            $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...
62
        } catch (BeanstalkException $e) {
63
            $this->log->error($e->getMessage(), $e->getTrace());
64
            $response['errors']['beanstalk'] = $e->getMessage();
65
        } catch (Exception $e) {
66
            $this->log->error("Beanstalk isn't working. {$e->getMessage()}", $e->getTrace());
67
            $response['errors']['beanstalk'] = "Beanstalk isn't working.";
68
        } finally {
69
            $this->queue->disconnect();
70
        }
71
72
        //Try to connect to db
73
        try {
74
            $this->db->connect();
75
        } catch (PDOException $e) {
76
            $this->log->error($e->getMessage(), $e->getTrace());
77
            $response['errors']['db'] = $e->getMessage();
78
        } catch (Exception $e) {
79
            $this->log->error("The database isn't working. {$e->getMessage()}", $e->getTrace());
80
            $response['errors']['db'] = "The database isn't working.";
81
        }
82
83
        if (!count($response)) {
84
            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...
85
        }
86
87
        $request = new \Phalcon\Http\Request();
88
        $response = [
89
            'status' => [
90
                'type' => 'FAILED',
91
                'identifier' => $request->getServerAddress(),
92
                'errors' => $response['errors'],
93
            ],
94
        ];
95
96
        return $this->response($response, 400, 'Error');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response($response, 400, 'Error') returns the type Phalcon\Http\Response which is incompatible with the documented return type Gewaer\Api\Controllers\Phalcon\Http\Response.
Loading history...
97
    }
98
}
99