64 lines
1.8 KiB
PHP
64 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Action\Pdf;
|
|
|
|
use App\Action\AbstractCrudAction;
|
|
use App\Service\MaintenanceManagerService;
|
|
use App\Service\PdfService;
|
|
use Interop\Http\ServerMiddleware\DelegateInterface;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use Zend\Diactoros\Response\EmptyResponse;
|
|
use Zend\Diactoros\Response\TextResponse;
|
|
|
|
class GenerateMaintenanceSheetAction extends AbstractCrudAction
|
|
{
|
|
const CORS_ALLOW_METHODS = [
|
|
'GET',
|
|
];
|
|
|
|
/**
|
|
* @var PdfService
|
|
*/
|
|
private $pdfService;
|
|
|
|
/**
|
|
* @var MaintenanceManagerService
|
|
*/
|
|
private $maintenanceManager;
|
|
|
|
public function __construct(PdfService $pdfService, MaintenanceManagerService $maintenanceManagerService)
|
|
{
|
|
$this->pdfService = $pdfService;
|
|
$this->maintenanceManager = $maintenanceManagerService;
|
|
}
|
|
|
|
/**
|
|
* @param ServerRequestInterface $request
|
|
* @param DelegateInterface $delegate
|
|
* @return \Zend\Diactoros\Response\JsonResponse|TextResponse|static
|
|
* @throws \PHPPdf\Core\PHPPdf\Exception\Exception
|
|
*/
|
|
public function get(ServerRequestInterface $request, DelegateInterface $delegate)
|
|
{
|
|
$id = $request->getAttribute('id', false);
|
|
try {
|
|
return (new TextResponse($this->pdfService->getMaintenanceSheet($id)))
|
|
->withHeader('Content-type', 'application/pdf');
|
|
} catch (\Exception $e) {
|
|
return new TextResponse($e->getMessage(), 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Configure CORS preflight
|
|
*
|
|
* @param ServerRequestInterface $request
|
|
* @param DelegateInterface $delegate
|
|
* @return static
|
|
*/
|
|
public function options(ServerRequestInterface $request, DelegateInterface $delegate)
|
|
{
|
|
return $this->withCorsHeaders(new EmptyResponse(), self::CORS_ALLOW_METHODS);
|
|
}
|
|
}
|