Skip to content

Commit

Permalink
Recover from revert on validationService
Browse files Browse the repository at this point in the history
  • Loading branch information
rjzondervan committed Nov 8, 2024
1 parent 3f4626c commit 07918db
Showing 1 changed file with 28 additions and 85 deletions.
113 changes: 28 additions & 85 deletions lib/Service/ValidationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@
namespace OCA\OpenCatalogi\Service;

use OCA\OpenCatalogi\Db\CatalogMapper;
use OCA\OpenCatalogi\Db\Publication;
use OCA\OpenCatalogi\Db\PublicationType;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\OCS\OCSBadRequestException;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\IAppConfig;
use OCP\IURLGenerator;
use Opis\JsonSchema\Errors\ErrorFormatter;
use Opis\JsonSchema\Validator;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
* Class ValidationService
Expand All @@ -14,112 +23,46 @@
*/
class ValidationService
{
/**
* @var string The name of the application.
*/
private string $appName;

/**
* @var array The current MongoDB Config.
*/
private array $mongodbConfig;

/**
* ValidationService constructor.
*
* @param IAppConfig $config The application config
* @param CatalogMapper $catalogMapper The catalog mapper.
* @param ObjectService $objectService The object service.
*/
public function __construct(
private readonly IAppConfig $config,
private readonly CatalogMapper $catalogMapper,
private readonly ObjectService $objectService,
) {
$this->appName = 'opencatalogi';

// Initialize MongoDB configuration
$this->mongodbConfig = [
'base_uri' => $this->config->getValueString(app: $this->appName, key: 'mongodbLocation'),
'headers' => ['api-key' => $this->config->getValueString(app: $this->appName, key: 'mongodbKey')],
'mongodbCluster' => $this->config->getValueString(app: $this->appName, key:'mongodbCluster')
];
}

/**
* Get the MongoDB configuration.
*
* @return array The mongodb config.
*/
public function getMongodbConfig(): array
private readonly IURLGenerator $urlGenerator,
)
{
return $this->mongodbConfig;
}

/**
* Fetches a catalog from either the local database or mongodb
* Validate a publication to the definition defined in the PublicationType.
*
* @param string $id The id of the catalog to be fetched.
* @return array The JSON Serialised catalog.
* @param Publication $publication The publication to validate.
* @return Publication The validated publication.
*
* @throws OCSNotFoundException If the catalog is not found.
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function getCatalog (string $id): array
public function validatePublication(array $publication): array
{
// Check if MongoDB storage is enabled
if ($this->config->hasKey(app: $this->appName, key: 'mongoStorage') !== false
&& $this->config->getValueString(app: $this->appName, key: 'mongoStorage') === '1'
) {
$filter = ['id' => $id, '_schema' => 'catalog'];
$publicationTypeId = $publication['publicationType'];
$publicationType = $this->objectService->getObject(objectType: 'publicationType', id: $publicationTypeId);

try {
return $this->objectService->findObject(filters: $filter, config: $this->getMongodbConfig());
} catch (OCSNotFoundException $exception) {
throw new OCSNotFoundException(message: 'Catalog not found for id: ' . $id);
}
}
$publicationType = (new PublicationType())->hydrate($publicationType);

// If MongoDB storage is not enabled, fetch from local database
return $this->catalogMapper->find(id: $id)->jsonSerialize();
}
$validator = new Validator();
$validator->setMaxErrors(100);

if(empty($publicationType->getProperties()) === true) {
return $publication;
}

$result = $validator->validate(data: (object) json_decode(json_encode($publication['data'])), schema: $publicationType->getSchema($this->urlGenerator));
/**
* Validates a publication against the rules set for the publication.
*
* @param array $publication The publication to be validated.
* @return array The publication after it has been validated.
*
* @throws OCSBadRequestException Thrown if the object does not validate
* @throws OCSNotFoundException Thrown if the catalog is not found
*/
public function validatePublication(array $publication): array
{
// Check for required fields
$requiredFields = ['catalogi', 'publicationType'];
foreach ($requiredFields as $field) {
if (isset($publication[$field]) === false) {
throw new OCSBadRequestException(message: $field . ' is required but not given.');
}
}

$catalog = $publication['catalogi'];
$publicationType = $publication['publicationType'];

try {
$catalog = $this->getCatalog($catalog);
} catch (OCSNotFoundException $exception) {
throw new OCSNotFoundException(message: $exception->getMessage());
}
$publication['validation'] = [];

// Check if the given publicationType is present in the catalog
if (in_array(needle: $publicationType, haystack: $catalog['publicationType']) === false) {
throw new OCSBadRequestException(message: 'Given publicationType object not present in catalog');
if ($result->hasError()) {
$errors = (new ErrorFormatter())->format($result->error());
$publication['validation'] = $errors;
}

return $publication;
Expand Down

0 comments on commit 07918db

Please sign in to comment.