bakaphp /
phalcon-api
| 1 | <?php |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
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 |
||
| 24 | */ |
||
| 25 | public function index($id = null) : Response |
||
| 26 | {
|
||
| 27 | return $this->response(['Woot Baka']); |
||
| 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');
|
||
| 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()); |
||
| 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(); |
||
| 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']); |
||
| 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'); |
||
| 97 | } |
||
| 98 | } |
||
| 99 |