Skip to content

Commit 6b94193

Browse files
committed
Prepared the export functionality.
1 parent 9ea0b3c commit 6b94193

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1795
-1602
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
vendor/
22
composer.lock
33
.idea/
4+
.phpcs*

config/module.config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1574,7 +1574,7 @@
15741574
/* Export services. */
15751575
Export\Service\AnrExportService::class => ReflectionBasedAbstractFactory::class,
15761576
Export\Service\AssetExportService::class => AutowireFactory::class,
1577-
Export\Service\AnrObjectExportService::class => AutowireFactory::class,
1577+
Export\Service\ObjectExportService::class => AutowireFactory::class,
15781578
Export\Service\SoaScaleCommentExportService::class => AutowireFactory::class,
15791579
Export\Service\OperationalRiskScalesExportService::class => AutowireFactory::class,
15801580
/* Import services. */

src/Entity/Question.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,16 @@
88
namespace Monarc\FrontOffice\Entity;
99

1010
use Doctrine\ORM\Mapping as ORM;
11-
use Monarc\Core\Entity\AnrSuperClass;
1211
use Monarc\Core\Entity\QuestionSuperClass;
1312

1413
/**
15-
* Question
16-
*
1714
* @ORM\Table(name="questions")
1815
* @ORM\Entity
1916
*/
2017
class Question extends QuestionSuperClass
2118
{
2219
/**
23-
* @var AnrSuperClass
20+
* @var Anr
2421
*
2522
* @ORM\ManyToOne(targetEntity="Anr", cascade={"persist"})
2623
* @ORM\JoinColumns({
@@ -43,17 +40,13 @@ class Question extends QuestionSuperClass
4340
*/
4441
protected $mode = 0;
4542

46-
47-
/**
48-
* @return AnrSuperClass
49-
*/
50-
public function getAnr()
43+
public function getAnr(): Anr
5144
{
5245
return $this->anr;
5346
}
5447

5548
/**
56-
* @param AnrSuperClass $anr
49+
* @param Anr $anr
5750
*/
5851
public function setAnr($anr): self
5952
{
@@ -70,13 +63,22 @@ public function getResponse()
7063
return $this->response;
7164
}
7265

73-
/**
74-
* @param string $response
75-
* @return Question
76-
*/
77-
public function setResponse($response)
66+
public function setResponse(string $response): self
7867
{
7968
$this->response = $response;
69+
70+
return $this;
71+
}
72+
73+
public function getMode(): int
74+
{
75+
return $this->mode;
76+
}
77+
78+
public function setMode(int $mode): self
79+
{
80+
$this->mode = $mode;
81+
8082
return $this;
8183
}
8284

src/Entity/QuestionChoice.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@
88
namespace Monarc\FrontOffice\Entity;
99

1010
use Doctrine\ORM\Mapping as ORM;
11-
use Monarc\Core\Entity\AnrSuperClass;
1211
use Monarc\Core\Entity\QuestionChoiceSuperClass;
1312

1413
/**
15-
* Question Choice
16-
*
1714
* @ORM\Table(name="questions_choices", indexes={
1815
* @ORM\Index(name="question_id", columns={"question_id"}),
1916
* @ORM\Index(name="anr_id", columns={"anr_id"}),
@@ -23,7 +20,7 @@
2320
class QuestionChoice extends QuestionChoiceSuperClass
2421
{
2522
/**
26-
* @var AnrSuperClass
23+
* @var Anr
2724
*
2825
* @ORM\ManyToOne(targetEntity="Anr", cascade={"persist"})
2926
* @ORM\JoinColumns({
@@ -32,16 +29,13 @@ class QuestionChoice extends QuestionChoiceSuperClass
3229
*/
3330
protected $anr;
3431

35-
/**
36-
* @return AnrSuperClass
37-
*/
38-
public function getAnr()
32+
public function getAnr(): Anr
3933
{
4034
return $this->anr;
4135
}
4236

4337
/**
44-
* @param AnrSuperClass $anr
38+
* @param Anr $anr
4539
*/
4640
public function setAnr($anr): self
4741
{

src/Entity/Recommendation.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Doctrine\Common\Collections\ArrayCollection;
1212
use Doctrine\ORM\Mapping as ORM;
1313
use Monarc\Core\Entity\Interfaces\PositionedEntityInterface;
14+
use Monarc\Core\Entity\Interfaces\PropertyStateEntityInterface;
1415
use Monarc\Core\Entity\Traits\CreateEntityTrait;
1516
use Monarc\Core\Entity\Traits\PropertyStateEntityTrait;
1617
use Monarc\Core\Entity\Traits\UpdateEntityTrait;
@@ -27,7 +28,7 @@
2728
* @ORM\Entity
2829
* @ORM\HasLifecycleCallbacks()
2930
*/
30-
class Recommendation implements PositionedEntityInterface
31+
class Recommendation implements PositionedEntityInterface, PropertyStateEntityInterface
3132
{
3233
use PropertyStateEntityTrait;
3334

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
<?php declare(strict_types=1);
22
/**
33
* @link https://github.com/monarc-project for the canonical source repository
4-
* @copyright Copyright (c) 2016-2023 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
4+
* @copyright Copyright (c) 2016-2024 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
55
* @license MONARC is licensed under GNU Affero General Public License version 3
66
*/
77

88
namespace Monarc\FrontOffice\Export\Controller;
99

1010
use Monarc\Core\Controller\Handler\AbstractRestfulControllerRequestHandler;
11+
use Monarc\FrontOffice\Export\Controller\Traits\ExportResponseControllerTrait;
1112
use Monarc\FrontOffice\Export\Service\AnrExportService;
1213
use Monarc\FrontOffice\Entity\Anr;
13-
use function strlen;
1414

1515
class ApiAnrExportController extends AbstractRestfulControllerRequestHandler
1616
{
17-
private AnrExportService $anrExportService;
17+
use ExportResponseControllerTrait;
1818

19-
public function __construct(AnrExportService $anrExportService)
19+
public function __construct(private AnrExportService $anrExportService)
2020
{
21-
$this->anrExportService = $anrExportService;
2221
}
2322

2423
/**
@@ -28,26 +27,8 @@ public function create($data)
2827
{
2928
/** @var Anr $anr */
3029
$anr = $this->getRequest()->getAttribute('anr');
30+
$result = $this->anrExportService->export($anr, $data);
3131

32-
[$fileName, $output] = $this->anrExportService->export($anr, $data);
33-
34-
$contentType = 'application/json; charset=utf-8';
35-
$extension = '.json';
36-
if (!empty($data['password'])) {
37-
$contentType = 'text/plain; charset=utf-8';
38-
$extension = '.bin';
39-
}
40-
41-
$response = $this->getResponse();
42-
$response->setContent($output);
43-
44-
$headers = $response->getHeaders();
45-
$filename = empty($fileName) ? $anr->getId() : $fileName;
46-
$headers->clearHeaders()
47-
->addHeaderLine('Content-Type', $contentType)
48-
->addHeaderLine('Content-Length', strlen($output))
49-
->addHeaderLine('Content-Disposition', 'attachment; filename="' . $filename . $extension);
50-
51-
return $response;
32+
return $this->prepareExportResponse($result['filename'], $result['content'], !empty($data['password']));
5233
}
5334
}
Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,31 @@
11
<?php declare(strict_types=1);
22
/**
33
* @link https://github.com/monarc-project for the canonical source repository
4-
* @copyright Copyright (c) 2016-2023 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
4+
* @copyright Copyright (c) 2016-2024 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
55
* @license MONARC is licensed under GNU Affero General Public License version 3
66
*/
77

88
namespace Monarc\FrontOffice\Export\Controller;
99

1010
use Monarc\Core\Controller\Handler\AbstractRestfulControllerRequestHandler;
11-
use Monarc\FrontOffice\Export\Service\AnrInstanceExportService;
11+
use Monarc\FrontOffice\Export\Controller\Traits\ExportResponseControllerTrait;
12+
use Monarc\FrontOffice\Export\Service\InstanceExportService;
1213
use Monarc\FrontOffice\Entity\Anr;
1314

1415
class ApiAnrInstancesExportController extends AbstractRestfulControllerRequestHandler
1516
{
16-
private AnrInstanceExportService $anrInstanceExportService;
17+
use ExportResponseControllerTrait;
1718

18-
public function __construct(AnrInstanceExportService $anrInstanceExportService)
19+
public function __construct(private InstanceExportService $anrInstanceExportService)
1920
{
20-
$this->anrInstanceExportService = $anrInstanceExportService;
2121
}
2222

2323
public function create($data)
2424
{
25-
// TODO: add the $data validator $data['id'] => instanceId is required.
26-
2725
/** @var Anr $anr */
2826
$anr = $this->getRequest()->getAttribute('anr');
27+
$result = $this->anrInstanceExportService->export($anr, $data);
2928

30-
$output = $this->anrInstanceExportService->export($data);
31-
32-
if (empty($data['password'])) {
33-
$contentType = 'application/json; charset=utf-8';
34-
$extension = '.json';
35-
} else {
36-
$contentType = 'text/plain; charset=utf-8';
37-
$extension = '.bin';
38-
}
39-
40-
$this->getResponse()
41-
->getHeaders()
42-
->clearHeaders()
43-
->addHeaderLine('Content-Type', $contentType)
44-
->addHeaderLine('Content-Disposition', 'attachment; filename="' .
45-
(empty($data['filename']) ? $data['id'] : $data['filename']) . $extension . '"');
46-
47-
$this->getResponse()
48-
->setContent($output);
49-
50-
return $this->getResponse();
29+
return $this->prepareExportResponse($result['filename'], $result['content'], !empty($data['password']));
5130
}
5231
}
Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,34 @@
11
<?php declare(strict_types=1);
22
/**
33
* @link https://github.com/monarc-project for the canonical source repository
4-
* @copyright Copyright (c) 2016-2023 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
4+
* @copyright Copyright (c) 2016-2024 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
55
* @license MONARC is licensed under GNU Affero General Public License version 3
66
*/
77

88
namespace Monarc\FrontOffice\Export\Controller;
99

1010
use Monarc\Core\Controller\Handler\AbstractRestfulControllerRequestHandler;
11-
use Monarc\Core\Controller\Handler\ControllerRequestResponseHandlerTrait;
1211
use Monarc\FrontOffice\Entity\Anr;
13-
use Monarc\FrontOffice\Export\Service\AnrObjectExportService;
12+
use Monarc\FrontOffice\Export\Controller\Traits\ExportResponseControllerTrait;
13+
use Monarc\FrontOffice\Export\Service\ObjectExportService;
1414

1515
class ApiAnrObjectsExportController extends AbstractRestfulControllerRequestHandler
1616
{
17-
use ControllerRequestResponseHandlerTrait;
17+
use ExportResponseControllerTrait;
1818

19-
private AnrObjectExportService $anrObjectExportService;
20-
21-
public function __construct(AnrObjectExportService $anrObjectExportService)
19+
public function __construct(private ObjectExportService $anrObjectExportService)
2220
{
23-
$this->anrObjectExportService = $anrObjectExportService;
2421
}
2522

2623
/**
2724
* @param array $data
2825
*/
2926
public function create($data)
3027
{
31-
// TODO: add a validator.
32-
if (empty($data['id'])) {
33-
throw new \Monarc\Core\Exception\Exception('Object to export is required', 412);
34-
}
35-
3628
/** @var Anr $anr */
3729
$anr = $this->getRequest()->getAttribute('anr');
30+
$result = $this->anrObjectExportService->export($anr, $data);
3831

39-
// TODO: ...
40-
$data['anr'] = $anr->getId();
41-
$output = $this->anrObjectExportService->export($anr, $data);
42-
43-
$response = $this->getResponse();
44-
$response->setContent($output);
45-
46-
$headers = $response->getHeaders();
47-
$filename = empty($data['filename']) ? $data['id'] : $data['filename'];
48-
$headers->clearHeaders()
49-
->addHeaderLine('Content-Type', 'application/json; charset=utf-8')
50-
->addHeaderLine('Content-Disposition', 'attachment; filename="' . $filename . '.json"');
51-
52-
return $response;
32+
return $this->prepareExportResponse($result['filename'], $result['content'], !empty($data['password']));
5333
}
5434
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* @link https://github.com/monarc-project for the canonical source repository
4+
* @copyright Copyright (c) 2016-2024 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
5+
* @license MONARC is licensed under GNU Affero General Public License version 3
6+
*/
7+
8+
namespace Monarc\FrontOffice\Export\Controller\Traits;
9+
10+
use Laminas\Diactoros\Response;
11+
use Psr\Http\Message\ResponseInterface;
12+
13+
use function strlen;
14+
15+
trait ExportResponseControllerTrait
16+
{
17+
private function prepareExportResponse(string $filename, string $output, bool $isEncrypted): ResponseInterface
18+
{
19+
$dataType = 'application/json';
20+
$contentType = 'application/json; charset=utf-8';
21+
$extension = '.json';
22+
if ($isEncrypted) {
23+
$dataType = 'text/plain';
24+
$contentType = 'text/plain; charset=utf-8';
25+
$extension = '.bin';
26+
}
27+
$stream = fopen('data://' . $dataType . ',' . $output, 'rb+');
28+
29+
return new Response($stream, 200, [
30+
'Content-Type' => $contentType,
31+
'Content-Length' => strlen($output),
32+
'Content-Disposition' => 'attachment; filename="' . $filename . $extension . '"',
33+
]);
34+
}
35+
}

src/Export/Service/AmvExportService.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)