1 | <?php |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
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 |
||
25 | */ |
||
26 | public function index($id = null) : Response |
||
27 | { |
||
28 | return $this->response(['Woot Baka']); |
||
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'); |
|
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()); |
|
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(); |
|
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']); |
||
86 | } |
||
87 | |||
88 | 1 | throw new ServerErrorHttpException(json_encode($response)); |
|
89 | } |
||
90 | } |
||
91 |