src/Controller/HealthController.php line 45

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: lancekesson
  5.  * Date: 04/07/2018
  6.  * Time: 09:06.
  7.  */
  8. namespace App\Controller;
  9. use App\Atd\Domain\Response\ResponseWrapper;
  10. use App\Atd\Domain\Response\Success;
  11. use App\Atd\Supplier\Atd\Classes\Response\Error;
  12. use App\Atd\Utilities\AtdResponseWrapper;
  13. // use App\Service\Health\AtdHealthService;
  14. use App\Service\LogPublisher\LogPublisherServiceInterface;
  15. use Psr\Container\ContainerInterface;
  16. use Psr\Log\LoggerInterface;
  17. use Symfony\Component\HttpFoundation\JsonResponse;
  18. use Symfony\Component\HttpFoundation\Response;
  19. final class StatusVocabulary
  20. {
  21.     public const STATUS_NOT_CHECKED 'NOT_CHECKED';
  22.     public const STATUS_AVAILABLE 'AVAILABLE';
  23.     public const STATUS_UNEXPECTED_RESPONSE 'UNEXPECTED_RESPONSE';
  24.     public const STATUS_ERROR 'ERROR';
  25. }
  26. class HealthController
  27. {
  28.     use AtdResponseWrapper;
  29.     private $healthService;
  30.     public function __construct(
  31.         private ContainerInterface $container,
  32.         private LoggerInterface $logger,
  33.         // private AtdHealthService $atdHealthService,
  34.         private LogPublisherServiceInterface $logPublisher,
  35.     ) {
  36.     }
  37.     public function index()
  38.     {
  39.         // $atdHealthService = $this->atdHealthService;
  40.         $healthContents = [];
  41.         $healthContents['services'] = [
  42.             'quicksilver' => [
  43.                 'status' => StatusVocabulary::STATUS_AVAILABLE,
  44.                 'env' => $this->getEnvironmentVariable('APP_ENV'),
  45.             ],
  46.             'release_version' => 3,
  47.             // "atd api" => [
  48.             //     "status" => $atdHealthService->atdHealthCheck(),
  49.             //     "url" => $atdHealthService->getTestUrl(),
  50.             // ]
  51.         ];
  52.         $healthContents['machine'] = $this->addMachineInformation();
  53.         //        $properties['success'] = [];
  54.         //        $properties['success'][] = new Success(['data'=>$healthContents]);
  55.         //        $properties['status'] = 'Success';
  56.         //
  57.         //        $response = new ResponseWrapper($properties);
  58.         //        $response = $this->wrapResponse($response);
  59.         $response = [
  60.             'status' => 'Success',
  61.             'data' => $healthContents,
  62.             'meta' => '',
  63.         ];
  64.         return new JsonResponse($response);
  65.     }
  66.     public function ready()
  67.     {
  68.         $healthContents = [];
  69.         $healthContents['services'] = [
  70.             'quicksilver' => [
  71.                 'status' => StatusVocabulary::STATUS_AVAILABLE,
  72.                 'env' => $this->getEnvironmentVariable('APP_ENV'),
  73.             ],
  74.         ];
  75.         $healthContents['machine'] = $this->addMachineInformation();
  76.         $response = [
  77.             'status' => 'Success',
  78.             'data' => $healthContents,
  79.             'meta' => '',
  80.         ];
  81.         return new JsonResponse($response);
  82.     }
  83.     private function addMachineInformation()
  84.     {
  85.         return [
  86.             'hostname' => $this->getMachineHostname(),
  87.         ];
  88.     }
  89.     private function getEnvironmentVariable($envName)
  90.     {
  91.         try {
  92.             return $_ENV[$envName];
  93.         } catch (\Exception $e) {
  94.             $this->logger->error("Attempting to access the environment variable: '{$envName}', resulted in an error", ['exception' => $e]);
  95.             return StatusVocabulary::STATUS_ERROR;
  96.         }
  97.     }
  98.     private function getMachineHostname()
  99.     {
  100.         try {
  101.             return gethostname();
  102.         } catch (\Exception $e) {
  103.             $this->logger->error('Error occurred when getting machine hostname using PHP built-in gethostname() function', ['exception' => $e]);
  104.             return StatusVocabulary::STATUS_ERROR;
  105.         }
  106.     }
  107.     public function readyV1($supplierCode)
  108.     {
  109.         // session_destroy();
  110.         try {
  111.             $healthRes $this->healthService->doHealth($supplierCode);
  112.         } catch (\Exception $exception) {
  113.             $properties $this->wrapExceptionInError($exception);
  114.             $healthRes = new ResponseWrapper($properties);
  115.         }
  116.         $response $this->wrapResponse($healthRes);
  117.         $response json_encode($response);
  118.         //   return new JsonResponse($response, $availRes->getStatusCode(), [], true);
  119.         return new Response($response$healthRes->getStatusCode());
  120.     }
  121.     public function writeVoucher()
  122.     {
  123.         $vouch_url '../../gateway-2/public/testVoucher.pdf';
  124.         $orderId rand();
  125.         $data file_get_contents($vouch_url);
  126.         $file 'public://'.$orderId.'.'.date('d-m-Y.h:i:s').'.pdf';
  127.         file_put_contents($file$data);
  128.         $response 'write';
  129.         return new Response($response\Symfony\Component\HttpFoundation\Response::HTTP_OK);
  130.     }
  131.     private function wrapExceptionInError($exception)
  132.     {
  133.         $errorProperties['errorText'] = $exception->getMessage();
  134.         $errorProperties['errorCode'] = 'Exception-Class:'.get_class($exception);
  135.         $properties['errors'][] = new Error($errorProperties);
  136.         $properties['status'] = 'Errors';
  137.         $statusCode = ($exception->getCode() > 100 && $exception->getCode() <= 600 $exception->getCode() : 500);
  138.         $properties['statusCode'] = $statusCode;
  139.         return $properties;
  140.     }
  141. }