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

api/controllers/IndexController.php (1 issue)

1
<?php
0 ignored issues
show
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
31
     */
32
    public function index($id = null) : Response
33
    {
34
        return $this->response(['Woot Baka']);
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());
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']);
92
        }
93
94
        throw new ServerErrorHttpException(json_encode($response));
95
    }
96
}
97