Test Failed
Pull Request — master (#80)
by Maximo
05:41
created

IndexController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 73
ccs 0
cts 44
cp 0
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 50 8
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
 * @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 Beanstalk
69
        try {
70
            $this->queue->connect();
71
        } catch (BeanstalkException $e) {
72
            $this->log->error($e->getMessage(), $e->getTrace());
73
            $response['errors']['beanstalk'] = $e->getMessage();
74
        } catch (Exception $e) {
75
            $this->log->error("Beanstalk isn't working. {$e->getMessage()}", $e->getTrace());
76
            $response['errors']['beanstalk'] = "Beanstalk isn't working.";
77
        } finally {
78
            $this->queue->disconnect();
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