Skip to content

Commit

Permalink
Prepared the export functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslanbaidan committed May 2, 2024
1 parent 9ea0b3c commit 6b94193
Show file tree
Hide file tree
Showing 43 changed files with 1,795 additions and 1,602 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor/
composer.lock
.idea/
.phpcs*
2 changes: 1 addition & 1 deletion config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -1574,7 +1574,7 @@
/* Export services. */
Export\Service\AnrExportService::class => ReflectionBasedAbstractFactory::class,
Export\Service\AssetExportService::class => AutowireFactory::class,
Export\Service\AnrObjectExportService::class => AutowireFactory::class,
Export\Service\ObjectExportService::class => AutowireFactory::class,
Export\Service\SoaScaleCommentExportService::class => AutowireFactory::class,
Export\Service\OperationalRiskScalesExportService::class => AutowireFactory::class,
/* Import services. */
Expand Down
32 changes: 17 additions & 15 deletions src/Entity/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@
namespace Monarc\FrontOffice\Entity;

use Doctrine\ORM\Mapping as ORM;
use Monarc\Core\Entity\AnrSuperClass;
use Monarc\Core\Entity\QuestionSuperClass;

/**
* Question
*
* @ORM\Table(name="questions")
* @ORM\Entity
*/
class Question extends QuestionSuperClass
{
/**
* @var AnrSuperClass
* @var Anr
*
* @ORM\ManyToOne(targetEntity="Anr", cascade={"persist"})
* @ORM\JoinColumns({
Expand All @@ -43,17 +40,13 @@ class Question extends QuestionSuperClass
*/
protected $mode = 0;


/**
* @return AnrSuperClass
*/
public function getAnr()
public function getAnr(): Anr
{
return $this->anr;
}

/**
* @param AnrSuperClass $anr
* @param Anr $anr
*/
public function setAnr($anr): self
{
Expand All @@ -70,13 +63,22 @@ public function getResponse()
return $this->response;
}

/**
* @param string $response
* @return Question
*/
public function setResponse($response)
public function setResponse(string $response): self
{
$this->response = $response;

return $this;
}

public function getMode(): int
{
return $this->mode;
}

public function setMode(int $mode): self
{
$this->mode = $mode;

return $this;
}

Expand Down
12 changes: 3 additions & 9 deletions src/Entity/QuestionChoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
namespace Monarc\FrontOffice\Entity;

use Doctrine\ORM\Mapping as ORM;
use Monarc\Core\Entity\AnrSuperClass;
use Monarc\Core\Entity\QuestionChoiceSuperClass;

/**
* Question Choice
*
* @ORM\Table(name="questions_choices", indexes={
* @ORM\Index(name="question_id", columns={"question_id"}),
* @ORM\Index(name="anr_id", columns={"anr_id"}),
Expand All @@ -23,7 +20,7 @@
class QuestionChoice extends QuestionChoiceSuperClass
{
/**
* @var AnrSuperClass
* @var Anr
*
* @ORM\ManyToOne(targetEntity="Anr", cascade={"persist"})
* @ORM\JoinColumns({
Expand All @@ -32,16 +29,13 @@ class QuestionChoice extends QuestionChoiceSuperClass
*/
protected $anr;

/**
* @return AnrSuperClass
*/
public function getAnr()
public function getAnr(): Anr
{
return $this->anr;
}

/**
* @param AnrSuperClass $anr
* @param Anr $anr
*/
public function setAnr($anr): self
{
Expand Down
3 changes: 2 additions & 1 deletion src/Entity/Recommendation.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Monarc\Core\Entity\Interfaces\PositionedEntityInterface;
use Monarc\Core\Entity\Interfaces\PropertyStateEntityInterface;
use Monarc\Core\Entity\Traits\CreateEntityTrait;
use Monarc\Core\Entity\Traits\PropertyStateEntityTrait;
use Monarc\Core\Entity\Traits\UpdateEntityTrait;
Expand All @@ -27,7 +28,7 @@
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
*/
class Recommendation implements PositionedEntityInterface
class Recommendation implements PositionedEntityInterface, PropertyStateEntityInterface
{
use PropertyStateEntityTrait;

Expand Down
31 changes: 6 additions & 25 deletions src/Export/Controller/ApiAnrExportController.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
<?php declare(strict_types=1);
/**
* @link https://github.com/monarc-project for the canonical source repository
* @copyright Copyright (c) 2016-2023 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
* @copyright Copyright (c) 2016-2024 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
* @license MONARC is licensed under GNU Affero General Public License version 3
*/

namespace Monarc\FrontOffice\Export\Controller;

use Monarc\Core\Controller\Handler\AbstractRestfulControllerRequestHandler;
use Monarc\FrontOffice\Export\Controller\Traits\ExportResponseControllerTrait;
use Monarc\FrontOffice\Export\Service\AnrExportService;
use Monarc\FrontOffice\Entity\Anr;
use function strlen;

class ApiAnrExportController extends AbstractRestfulControllerRequestHandler
{
private AnrExportService $anrExportService;
use ExportResponseControllerTrait;

public function __construct(AnrExportService $anrExportService)
public function __construct(private AnrExportService $anrExportService)
{
$this->anrExportService = $anrExportService;
}

/**
Expand All @@ -28,26 +27,8 @@ public function create($data)
{
/** @var Anr $anr */
$anr = $this->getRequest()->getAttribute('anr');
$result = $this->anrExportService->export($anr, $data);

[$fileName, $output] = $this->anrExportService->export($anr, $data);

$contentType = 'application/json; charset=utf-8';
$extension = '.json';
if (!empty($data['password'])) {
$contentType = 'text/plain; charset=utf-8';
$extension = '.bin';
}

$response = $this->getResponse();
$response->setContent($output);

$headers = $response->getHeaders();
$filename = empty($fileName) ? $anr->getId() : $fileName;
$headers->clearHeaders()
->addHeaderLine('Content-Type', $contentType)
->addHeaderLine('Content-Length', strlen($output))
->addHeaderLine('Content-Disposition', 'attachment; filename="' . $filename . $extension);

return $response;
return $this->prepareExportResponse($result['filename'], $result['content'], !empty($data['password']));
}
}
35 changes: 7 additions & 28 deletions src/Export/Controller/ApiAnrInstancesExportController.php
Original file line number Diff line number Diff line change
@@ -1,52 +1,31 @@
<?php declare(strict_types=1);
/**
* @link https://github.com/monarc-project for the canonical source repository
* @copyright Copyright (c) 2016-2023 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
* @copyright Copyright (c) 2016-2024 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
* @license MONARC is licensed under GNU Affero General Public License version 3
*/

namespace Monarc\FrontOffice\Export\Controller;

use Monarc\Core\Controller\Handler\AbstractRestfulControllerRequestHandler;
use Monarc\FrontOffice\Export\Service\AnrInstanceExportService;
use Monarc\FrontOffice\Export\Controller\Traits\ExportResponseControllerTrait;
use Monarc\FrontOffice\Export\Service\InstanceExportService;
use Monarc\FrontOffice\Entity\Anr;

class ApiAnrInstancesExportController extends AbstractRestfulControllerRequestHandler
{
private AnrInstanceExportService $anrInstanceExportService;
use ExportResponseControllerTrait;

public function __construct(AnrInstanceExportService $anrInstanceExportService)
public function __construct(private InstanceExportService $anrInstanceExportService)
{
$this->anrInstanceExportService = $anrInstanceExportService;
}

public function create($data)
{
// TODO: add the $data validator $data['id'] => instanceId is required.

/** @var Anr $anr */
$anr = $this->getRequest()->getAttribute('anr');
$result = $this->anrInstanceExportService->export($anr, $data);

$output = $this->anrInstanceExportService->export($data);

if (empty($data['password'])) {
$contentType = 'application/json; charset=utf-8';
$extension = '.json';
} else {
$contentType = 'text/plain; charset=utf-8';
$extension = '.bin';
}

$this->getResponse()
->getHeaders()
->clearHeaders()
->addHeaderLine('Content-Type', $contentType)
->addHeaderLine('Content-Disposition', 'attachment; filename="' .
(empty($data['filename']) ? $data['id'] : $data['filename']) . $extension . '"');

$this->getResponse()
->setContent($output);

return $this->getResponse();
return $this->prepareExportResponse($result['filename'], $result['content'], !empty($data['password']));
}
}
34 changes: 7 additions & 27 deletions src/Export/Controller/ApiAnrObjectsExportController.php
Original file line number Diff line number Diff line change
@@ -1,54 +1,34 @@
<?php declare(strict_types=1);
/**
* @link https://github.com/monarc-project for the canonical source repository
* @copyright Copyright (c) 2016-2023 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
* @copyright Copyright (c) 2016-2024 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
* @license MONARC is licensed under GNU Affero General Public License version 3
*/

namespace Monarc\FrontOffice\Export\Controller;

use Monarc\Core\Controller\Handler\AbstractRestfulControllerRequestHandler;
use Monarc\Core\Controller\Handler\ControllerRequestResponseHandlerTrait;
use Monarc\FrontOffice\Entity\Anr;
use Monarc\FrontOffice\Export\Service\AnrObjectExportService;
use Monarc\FrontOffice\Export\Controller\Traits\ExportResponseControllerTrait;
use Monarc\FrontOffice\Export\Service\ObjectExportService;

class ApiAnrObjectsExportController extends AbstractRestfulControllerRequestHandler
{
use ControllerRequestResponseHandlerTrait;
use ExportResponseControllerTrait;

private AnrObjectExportService $anrObjectExportService;

public function __construct(AnrObjectExportService $anrObjectExportService)
public function __construct(private ObjectExportService $anrObjectExportService)
{
$this->anrObjectExportService = $anrObjectExportService;
}

/**
* @param array $data
*/
public function create($data)
{
// TODO: add a validator.
if (empty($data['id'])) {
throw new \Monarc\Core\Exception\Exception('Object to export is required', 412);
}

/** @var Anr $anr */
$anr = $this->getRequest()->getAttribute('anr');
$result = $this->anrObjectExportService->export($anr, $data);

// TODO: ...
$data['anr'] = $anr->getId();
$output = $this->anrObjectExportService->export($anr, $data);

$response = $this->getResponse();
$response->setContent($output);

$headers = $response->getHeaders();
$filename = empty($data['filename']) ? $data['id'] : $data['filename'];
$headers->clearHeaders()
->addHeaderLine('Content-Type', 'application/json; charset=utf-8')
->addHeaderLine('Content-Disposition', 'attachment; filename="' . $filename . '.json"');

return $response;
return $this->prepareExportResponse($result['filename'], $result['content'], !empty($data['password']));
}
}
35 changes: 35 additions & 0 deletions src/Export/Controller/Traits/ExportResponseControllerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);
/**
* @link https://github.com/monarc-project for the canonical source repository
* @copyright Copyright (c) 2016-2024 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
* @license MONARC is licensed under GNU Affero General Public License version 3
*/

namespace Monarc\FrontOffice\Export\Controller\Traits;

use Laminas\Diactoros\Response;
use Psr\Http\Message\ResponseInterface;

use function strlen;

trait ExportResponseControllerTrait
{
private function prepareExportResponse(string $filename, string $output, bool $isEncrypted): ResponseInterface
{
$dataType = 'application/json';
$contentType = 'application/json; charset=utf-8';
$extension = '.json';
if ($isEncrypted) {
$dataType = 'text/plain';
$contentType = 'text/plain; charset=utf-8';
$extension = '.bin';
}
$stream = fopen('data://' . $dataType . ',' . $output, 'rb+');

return new Response($stream, 200, [
'Content-Type' => $contentType,
'Content-Length' => strlen($output),
'Content-Disposition' => 'attachment; filename="' . $filename . $extension . '"',
]);
}
}
24 changes: 0 additions & 24 deletions src/Export/Service/AmvExportService.php

This file was deleted.

Loading

0 comments on commit 6b94193

Please sign in to comment.