<?php
/**
* Created by PhpStorm.
* User: lancekesson
* Date: 04/07/2018
* Time: 09:06.
*/
namespace App\Controller;
use App\Atd\Domain\Response\ResponseWrapper;
use App\Atd\Domain\Response\Success;
use App\Atd\Supplier\Atd\Classes\Response\Error;
use App\Atd\Utilities\AtdResponseWrapper;
// use App\Service\Health\AtdHealthService;
use App\Service\LogPublisher\LogPublisherServiceInterface;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
final class StatusVocabulary
{
public const STATUS_NOT_CHECKED = 'NOT_CHECKED';
public const STATUS_AVAILABLE = 'AVAILABLE';
public const STATUS_UNEXPECTED_RESPONSE = 'UNEXPECTED_RESPONSE';
public const STATUS_ERROR = 'ERROR';
}
class HealthController
{
use AtdResponseWrapper;
private $healthService;
public function __construct(
private ContainerInterface $container,
private LoggerInterface $logger,
// private AtdHealthService $atdHealthService,
private LogPublisherServiceInterface $logPublisher,
) {
}
public function index()
{
// $atdHealthService = $this->atdHealthService;
$healthContents = [];
$healthContents['services'] = [
'quicksilver' => [
'status' => StatusVocabulary::STATUS_AVAILABLE,
'env' => $this->getEnvironmentVariable('APP_ENV'),
],
'release_version' => 3,
// "atd api" => [
// "status" => $atdHealthService->atdHealthCheck(),
// "url" => $atdHealthService->getTestUrl(),
// ]
];
$healthContents['machine'] = $this->addMachineInformation();
// $properties['success'] = [];
// $properties['success'][] = new Success(['data'=>$healthContents]);
// $properties['status'] = 'Success';
//
// $response = new ResponseWrapper($properties);
// $response = $this->wrapResponse($response);
$response = [
'status' => 'Success',
'data' => $healthContents,
'meta' => '',
];
return new JsonResponse($response);
}
public function ready()
{
$healthContents = [];
$healthContents['services'] = [
'quicksilver' => [
'status' => StatusVocabulary::STATUS_AVAILABLE,
'env' => $this->getEnvironmentVariable('APP_ENV'),
],
];
$healthContents['machine'] = $this->addMachineInformation();
$response = [
'status' => 'Success',
'data' => $healthContents,
'meta' => '',
];
return new JsonResponse($response);
}
private function addMachineInformation()
{
return [
'hostname' => $this->getMachineHostname(),
];
}
private function getEnvironmentVariable($envName)
{
try {
return $_ENV[$envName];
} catch (\Exception $e) {
$this->logger->error("Attempting to access the environment variable: '{$envName}', resulted in an error", ['exception' => $e]);
return StatusVocabulary::STATUS_ERROR;
}
}
private function getMachineHostname()
{
try {
return gethostname();
} catch (\Exception $e) {
$this->logger->error('Error occurred when getting machine hostname using PHP built-in gethostname() function', ['exception' => $e]);
return StatusVocabulary::STATUS_ERROR;
}
}
public function readyV1($supplierCode)
{
// session_destroy();
try {
$healthRes = $this->healthService->doHealth($supplierCode);
} catch (\Exception $exception) {
$properties = $this->wrapExceptionInError($exception);
$healthRes = new ResponseWrapper($properties);
}
$response = $this->wrapResponse($healthRes);
$response = json_encode($response);
// return new JsonResponse($response, $availRes->getStatusCode(), [], true);
return new Response($response, $healthRes->getStatusCode());
}
public function writeVoucher()
{
$vouch_url = '../../gateway-2/public/testVoucher.pdf';
$orderId = rand();
$data = file_get_contents($vouch_url);
$file = 'public://'.$orderId.'.'.date('d-m-Y.h:i:s').'.pdf';
file_put_contents($file, $data);
$response = 'write';
return new Response($response, \Symfony\Component\HttpFoundation\Response::HTTP_OK);
}
private function wrapExceptionInError($exception)
{
$errorProperties['errorText'] = $exception->getMessage();
$errorProperties['errorCode'] = 'Exception-Class:'.get_class($exception);
$properties['errors'][] = new Error($errorProperties);
$properties['status'] = 'Errors';
$statusCode = ($exception->getCode() > 100 && $exception->getCode() <= 600 ? $exception->getCode() : 500);
$properties['statusCode'] = $statusCode;
return $properties;
}
}