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