Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gebruiksrecht delete logic #83

Merged
merged 11 commits into from
Sep 20, 2023
16 changes: 16 additions & 0 deletions Installation/installation.json
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,22 @@
"zgw.drc.download"
]
},
{
"name": "DrcGebruiksrechtDeleteAction",
"reference": "https://vng.opencatalogi.nl/action/drc.DrcGebruiksrechtAction.action.json",
"actionHandler": "CommonGateway\\ZGWBundle\\ActionHandler\\DrcGebruiksrechtDeleteHandler",
"listens": [
"commongateway.object.pre.delete"
],
"conditions": {
"==": [
{
"var": "entity.reference"
},
"https://vng.opencatalogi.nl/schemas/drc.gebruiksrecht.schema.json"
]
}
},
{
"name": "DrcInhoudAction",
"reference": "https://vng.opencatalogi.nl/action/drc.DrcInhoudAction.action.json",
Expand Down
67 changes: 67 additions & 0 deletions src/ActionHandler/DrcGebruiksrechtDeleteHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace CommonGateway\ZGWBundle\ActionHandler;

use CommonGateway\ZGWBundle\Service\DRCService;
use CommonGateway\CoreBundle\ActionHandler\ActionHandlerInterface;

class DrcGebruiksrechtDeleteHandler implements ActionHandlerInterface
{

private DRCService $drcService;


public function __construct(DRCService $drcService)
{
$this->drcService = $drcService;

}//end __construct()


/**
* This function returns the requered configuration as a [json-schema](https://json-schema.org/) array.
*
* @throws array a [json-schema](https://json-schema.org/) that this action should comply to
*/
public function getConfiguration(): array
{
return [
'$id' => 'https://vng.opencatalogi.nl/ActionHandler/drc.InhoudHandler.ActionHandler.json',
'$schema' => 'https://docs.commongateway.nl/schemas/ActionHandler.schema.json',
'title' => '',
'description' => 'This handler returns a welcoming string',
'required' => [],
'properties' => [
'enkelvoudigInformatieObjectEntityId' => [
'type' => 'uuid',
'description' => 'The id of the huwelijks entity',
'nullable' => true,
'$ref' => 'https://vng.opencatalogi.nl/schemas/drc.enkelvoudigInformatieObject.schema.json',
],
],
];

}//end getConfiguration()


/**
* This function runs the service.
*
* @param array $data The data from the call
* @param array $configuration The configuration of the action
*
* @throws GatewayException
* @throws CacheException
* @throws InvalidArgumentException
* @throws ComponentException
*
* @return array
*/
public function run(array $data, array $configuration): array
{
return $this->drcService->gebruiksrechtDeleteHandler($data, $configuration);

}//end run()


}//end class
42 changes: 42 additions & 0 deletions src/Service/DRCService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Symfony\Component\HttpFoundation\Response;
use CommonGateway\CoreBundle\Service\CacheService;

class DRCService

Check warning on line 16 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

The class DRCService has an overall complexity of 87 which is very high. The configured complexity threshold is 50.

Check warning on line 16 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

The class DRCService has an overall complexity of 87 which is very high. The configured complexity threshold is 50.
{

private array $configuration;
Expand Down Expand Up @@ -63,7 +63,7 @@
*
* @return string|null Error message.
*/
private function checkForLockErrors(bool $willBeLocked, ObjectEntity $objectEntity, array $data): ?string

Check warning on line 66 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

The method checkForLockErrors() has a Cyclomatic Complexity of 10. The configured cyclomatic complexity threshold is 10.

Check warning on line 66 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

The method checkForLockErrors() has a Cyclomatic Complexity of 10. The configured cyclomatic complexity threshold is 10.
{

// Cant unlock a non locked object.
Expand Down Expand Up @@ -99,7 +99,7 @@
*
* @return array Http response.
*/
public function drcLockHandler(array $data, array $configuration): array

Check warning on line 102 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

Avoid unused parameters such as '$configuration'.

Check warning on line 102 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

Avoid unused parameters such as '$configuration'.
{
$this->data = $data;
$path = $this->data['path'];
Expand All @@ -124,7 +124,7 @@
}

$lockId = Uuid::uuid4()->toString();
$objectEntity->hydrate(['lock' => $willBeLocked ? $lockId : null, 'locked' => $willBeLocked ?? null, 'bestandsdelen' => []]);

Check warning on line 127 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

Line exceeds 125 characters; contains 133 characters

Check warning on line 127 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

Line exceeds 125 characters; contains 133 characters
if ($willBeLocked === true) {
$objectEntity->setLock($lockId);
}
Expand All @@ -135,7 +135,7 @@
if ($willBeLocked === true) {
$responseBody = \Safe\json_encode(['lock' => $lockId]);
$statusCode = 200;
} else {

Check warning on line 138 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

The method drcLockHandler uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.

Check warning on line 138 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

The method drcLockHandler uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
$responseBody = null;
$statusCode = 204;
}
Expand Down Expand Up @@ -163,8 +163,8 @@
{
if (isset($gebruiksrecht['informatieobject']) == false
|| is_string($gebruiksrecht['informatieobject']) === false
|| (Uuid::isValid($gebruiksrecht['informatieobject']) === false

Check warning on line 166 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

Avoid using static access to class '\Ramsey\Uuid\Uuid' in method 'checkGebruiksrechtInfoObject'.

Check warning on line 166 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

Avoid using static access to class '\Ramsey\Uuid\Uuid' in method 'checkGebruiksrechtInfoObject'.
&& Uuid::isValid(end($explodedInfoObject)) === false)

Check warning on line 167 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

Avoid using static access to class '\Ramsey\Uuid\Uuid' in method 'checkGebruiksrechtInfoObject'.

Check warning on line 167 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

Avoid using static access to class '\Ramsey\Uuid\Uuid' in method 'checkGebruiksrechtInfoObject'.
) {
$this->data['response'] = new Response(\Safe\json_encode(['message' => 'No id or url given for informatieobject, it is required.']), 400, ['content-type' => 'application/json']);

Expand All @@ -181,7 +181,7 @@
*
* @return array|void
*/
private function checkInformatieObject(?ObjectEntity $informatieObject)

Check warning on line 184 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

Avoid unused private methods such as 'checkInformatieObject'.

Check warning on line 184 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

Avoid unused private methods such as 'checkInformatieObject'.
{
if ($informatieObject instanceof ObjectEntity === false) {
$this->data['response'] = new Response(\Safe\json_encode(['message' => 'No existing informatieobject found with given body, a existing informatieobject is required.']), 403, ['content-type' => 'application/json']);
Expand All @@ -200,7 +200,7 @@
*
* @return array Http response.
*/
public function gebruiksrechtHandler(array $data, array $configuration): array

Check warning on line 203 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

Avoid unused parameters such as '$configuration'.

Check warning on line 203 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

Avoid unused parameters such as '$configuration'.
{
$this->data = $data;

Expand All @@ -216,7 +216,7 @@
$this->checkGebruiksrechtInfoObject($gebruiksrecht, $explodedInfoObject);

// If POST make sure the enkelvoudiginformatieobject has indicatieGebruiksrecht set to true.
if (Uuid::isValid($gebruiksrecht['informatieobject']) === true) {

Check warning on line 219 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

Avoid using static access to class '\Ramsey\Uuid\Uuid' in method 'gebruiksrechtHandler'.

Check warning on line 219 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

Avoid using static access to class '\Ramsey\Uuid\Uuid' in method 'gebruiksrechtHandler'.
$informatieObjectId = $gebruiksrecht['informatieobject'];
} else {
$informatieObjectId = end($explodedInfoObject);
Expand All @@ -239,6 +239,48 @@
}//end gebruiksrechtHandler()


/**
* Handles gebruiksrecht DELETE logic.
*
* @param array $data The data passed by the action.
* @param array $configuration The configuration of the action.
*
* @return array Http response.
*/
public function gebruiksrechtDeleteHandler(array $data, array $configuration): array
{
$this->data = $data;

// If last gebruiksrecht of enkelvoudiginformatieobject set indicatieGebruiksrecht to null.
$gebruiksrechtObject = $data['object'];
if ($gebruiksrechtObject instanceof ObjectEntity === false) {
return $this->data;
}

$informatieObject = $gebruiksrechtObject->getValue('informatieobject');
if ($informatieObject instanceof ObjectEntity === false) {
return $this->data;
}

$gebruiksrechtSchema = $this->entityManager->getRepository('App:Entity')->findOneBy(['reference' => 'https://vng.opencatalogi.nl/schemas/drc.gebruiksrecht.schema.json']);
$gebruiksrechtInfoObjectProperty = $this->entityManager->getRepository('App:Attribute')->findOneBy(['name' => 'informatieobject', 'entity' => $gebruiksrechtSchema]);
$gebruiksrechtValues = $this->entityManager->getRepository('App:Value')->findBy(['stringValue' => $informatieObject->getUri(), 'attribute' => $gebruiksrechtInfoObjectProperty]);

// If we have 1 or less gebruiksrechten for this enkelvoudiginformatieobject, we set enkelvoudiginformatieobject.indicatieGebruiksrecht to null.
if (count($gebruiksrechtValues) <= 1) {
$informatieObject->hydrate(['indicatieGebruiksrecht' => null]);

$this->entityManager->persist($informatieObject);
$this->entityManager->flush();
$this->cacheService->cacheObject($informatieObject);
$this->entityManager->clear();
}

return $this->data;

}//end gebruiksrechtDeleteHandler()


/**
* Returns the data from an document as a response.
*
Expand All @@ -261,7 +303,7 @@
$file = $this->entityManager->getRepository('App:File')->find($path['id']);
if ($file instanceof File
) {
$this->data['response'] = new Response(\Safe\base64_decode($file->getBase64()), 200, ['content-type' => $file->getMimeType()]);

Check warning on line 306 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

Line exceeds 125 characters; contains 139 characters

Check warning on line 306 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

Line exceeds 125 characters; contains 139 characters
} else if ($objectEntity instanceof ObjectEntity === true
&& $objectEntity->getEntity()->getId()->toString() == $configuration['enkelvoudigInformatieObjectEntityId']
) {
Expand All @@ -278,7 +320,7 @@
}

$this->data['response'] = new Response(
\Safe\base64_decode($objectEntity->getValueObject('inhoud')->getFiles()->matching($criteria)->last()->getBase64()),

Check warning on line 323 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

Line exceeds 125 characters; contains 131 characters

Check warning on line 323 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

Line exceeds 125 characters; contains 131 characters
200,
['content-type' => $objectEntity->getValueObject('inhoud')->getFiles()->first()->getMimeType()]
);
Expand Down Expand Up @@ -385,7 +427,7 @@
*
* @return void
*/
public function createOrUpdateFile(ObjectEntity $objectEntity, array $data, Endpoint $downloadEndpoint, bool $setResponse=true): void

Check warning on line 430 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

Line exceeds 125 characters; contains 137 characters

Check warning on line 430 in src/Service/DRCService.php

View workflow job for this annotation

GitHub Actions / build

Line exceeds 125 characters; contains 137 characters
{
if ($objectEntity->getValueObject('inhoud')->getFiles()->count() > 0) {
$file = $objectEntity->getValueObject('inhoud')->getFiles()->first();
Expand Down
Loading