Commit f2a88e57 authored by François Agneray's avatar François Agneray
Browse files

Fixes some sonarqube code smells

parent a5459e7b
Pipeline #5114 passed with stages
in 2 minutes and 23 seconds
......@@ -55,23 +55,24 @@ final class AdminFileExplorerAction
return $response->withHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
}
$filePath = $args['fpath'];
$path = $this->dataPath;
if (array_key_exists('fpath', $args)) {
$path .= DIRECTORY_SEPARATOR . $args['fpath'];
$path .= DIRECTORY_SEPARATOR . $filePath;
}
if (is_file($path)) {
throw new HttpBadRequestException(
$request,
'Path ' . $args['fpath'] . ' is a file'
'Path ' . $filePath . ' is a file'
);
}
if (!file_exists($path)) {
throw new HttpNotFoundException(
$request,
'Directory ' . $args['fpath'] . ' is not found'
'Directory ' . $filePath . ' is not found'
);
}
......
......@@ -82,11 +82,12 @@ final class DatasetAction extends AbstractAction
}
// Dataset family is mandatory to edit a dataset
$family = $this->em->find('App\Entity\DatasetFamily', $parsedBody['id_dataset_family']);
$idDatasetFamily = $parsedBody['id_dataset_family'];
$family = $this->em->find('App\Entity\DatasetFamily', $idDatasetFamily);
if (is_null($family)) {
throw new HttpBadRequestException(
$request,
'Dataset family with id ' . $parsedBody['id_dataset_family'] . ' is not found'
'Dataset family with id ' . $idDatasetFamily . ' is not found'
);
}
......
......@@ -68,13 +68,14 @@ final class DatasetFileExplorerAction extends AbstractAction
}
// Search the correct dataset with primary key
$dataset = $this->em->find('App\Entity\Dataset', $args['dname']);
$datasetName = $args['dname'];
$dataset = $this->em->find('App\Entity\Dataset', $datasetName);
// If dataset is not found 404
if (is_null($dataset)) {
throw new HttpNotFoundException(
$request,
'Dataset with name ' . $args['dname'] . ' is not found'
'Dataset with name ' . $datasetName . ' is not found'
);
}
......@@ -83,23 +84,24 @@ final class DatasetFileExplorerAction extends AbstractAction
$this->verifyDatasetAuthorization($request, $dataset->getName(), $this->settings['admin_role']);
}
$filePath = $args['fpath'];
$path = $this->dataPath . $dataset->getDataPath();
if (array_key_exists('fpath', $args)) {
$path .= DIRECTORY_SEPARATOR . $args['fpath'];
$path .= DIRECTORY_SEPARATOR . $filePath;
}
if (is_file($path)) {
throw new HttpBadRequestException(
$request,
'Path ' . $args['fpath'] . ' is a file'
'Path ' . $filePath . ' is a file'
);
}
if (!file_exists($path)) {
throw new HttpNotFoundException(
$request,
'Directory ' . $args['fpath'] . ' is not found'
'Directory ' . $filePath . ' is not found'
);
}
......
......@@ -106,11 +106,12 @@ final class DatasetListAction extends AbstractAction
}
// Project is mandatory to add a new dataset
$project = $this->em->find('App\Entity\Project', $parsedBody['project_name']);
$projectName = $parsedBody['project_name'];
$project = $this->em->find('App\Entity\Project', $projectName);
if (is_null($project)) {
throw new HttpBadRequestException(
$request,
'Project with name ' . $parsedBody['project_name'] . ' is not found'
'Project with name ' . $projectName . ' is not found'
);
}
......
......@@ -68,13 +68,14 @@ final class DownloadFileAction extends AbstractAction
}
// Search the correct dataset with primary key
$dataset = $this->em->find('App\Entity\Dataset', $args['dname']);
$datasetName = $args['dname'];
$dataset = $this->em->find('App\Entity\Dataset', $datasetName);
// If dataset is not found 404
if (is_null($dataset)) {
throw new HttpNotFoundException(
$request,
'Dataset with name ' . $args['dname'] . ' is not found'
'Dataset with name ' . $datasetName . ' is not found'
);
}
......@@ -90,7 +91,7 @@ final class DownloadFileAction extends AbstractAction
if (!file_exists($filePath)) {
throw new HttpNotFoundException(
$request,
'File with path ' . $args['fpath'] . ' is not found for the dataset ' . $args['dname']
'File with path ' . $args['fpath'] . ' is not found for the dataset ' . $datasetName
);
}
......
......@@ -60,13 +60,14 @@ final class OptionListAction extends AbstractAction
}
// Search the correct select with primary key (name)
$select = $this->em->find('App\Entity\Select', $parsedBody['select_name']);
$selectName = $parsedBody['select_name'];
$select = $this->em->find('App\Entity\Select', $selectName);
// If select is not found 404
if (is_null($select)) {
throw new HttpNotFoundException(
$request,
'Select with name ' . $parsedBody['select_name'] . ' is not found'
'Select with name ' . $selectName . ' is not found'
);
}
......
......@@ -71,13 +71,14 @@ final class OutputCategoryAction extends AbstractAction
}
// Search the correct output family with primary key
$outputFamily = $this->em->find('App\Entity\OutputFamily', $parsedBody['id_output_family']);
$idOutputFamily = $parsedBody['id_output_family'];
$outputFamily = $this->em->find('App\Entity\OutputFamily', $idOutputFamily);
// Output family is mandatory. If is null 400
if (is_null($outputFamily)) {
throw new HttpBadRequestException(
$request,
'Output family with id ' . $parsedBody['id_output_family'] . ' is not found'
'Output family with id ' . $idOutputFamily . ' is not found'
);
}
......
......@@ -71,11 +71,12 @@ final class ProjectAction extends AbstractAction
}
// Database exists ?
$database = $this->em->find('App\Entity\Database', $parsedBody['id_database']);
$idDatabase = $parsedBody['id_database'];
$database = $this->em->find('App\Entity\Database', $idDatabase);
if (is_null($database)) {
throw new HttpBadRequestException(
$request,
'Database with id ' . $parsedBody['id_database'] . ' is not found'
'Database with id ' . $idDatabase . ' is not found'
);
}
......
......@@ -59,11 +59,12 @@ final class ProjectListAction extends AbstractAction
}
// Database exists ?
$database = $this->em->find('App\Entity\Database', $parsedBody['id_database']);
$idDatabase = $parsedBody['id_database'];
$database = $this->em->find('App\Entity\Database', $idDatabase);
if (is_null($database)) {
throw new HttpBadRequestException(
$request,
'Database with id ' . $parsedBody['id_database'] . ' is not found'
'Database with id ' . $idDatabase . ' is not found'
);
}
......
......@@ -96,16 +96,16 @@ class Group implements \JsonSerializable
public function jsonSerialize()
{
$datasets = array();
$datasetNames = array();
foreach ($this->getDatasets() as $dataset) {
$datasets[] = $dataset->getName();
$datasetNames[] = $dataset->getName();
}
return [
'id' => $this->getId(),
'role' => $this->getRole(),
'instance_name' => $this->getInstance()->getName(),
'datasets' => $datasets
'datasets' => $datasetNames
];
}
}
......@@ -51,7 +51,6 @@ class Equal extends Operator
*/
public function getExpression(): string
{
$expr = $this->expr->eq($this->column, $this->getSqlValue($this->value));
return $expr;
return $this->expr->eq($this->column, $this->getSqlValue($this->value));
}
}
......@@ -44,21 +44,24 @@ class JsonPostgres extends Operator
{
switch ($this->operator) {
case 'eq':
return $this->expr->eq($this->getColumn(), $this->getValue());
$expr = $this->expr->eq($this->getColumn(), $this->getValue());
break;
case 'gt':
return $this->expr->gt($this->getColumn(), $this->getValue());
$expr = $this->expr->gt($this->getColumn(), $this->getValue());
break;
case 'gte':
return $this->expr->gte($this->getColumn(), $this->getValue());
$expr = $this->expr->gte($this->getColumn(), $this->getValue());
break;
case 'lt':
return $this->expr->lt($this->getColumn(), $this->getValue());
$expr = $this->expr->lt($this->getColumn(), $this->getValue());
break;
case 'lte':
return $this->expr->lte($this->getColumn(), $this->getValue());
$expr = $this->expr->lte($this->getColumn(), $this->getValue());
break;
default:
$expr = '';
}
return $$expr;
}
private function getColumn(): string
......
......@@ -46,94 +46,249 @@ class OperatorFactory implements IOperatorFactory
): IOperator {
switch ($type) {
case 'bw':
if (count($parameters) != 2) {
throw OperatorException::operatorBadNumberOfParameters($type, 2);
}
return new Between($expr, $column, $columnType, $parameters[0], $parameters[1]);
$operator = $this->createBetween($type, $expr, $column, $columnType, $parameters);
break;
case 'eq':
if (count($parameters) != 1) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new Equal($expr, $column, $columnType, $parameters[0]);
$operator = $this->createEqual($type, $expr, $column, $columnType, $parameters);
break;
case 'neq':
if (count($parameters) != 1) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new NotEqual($expr, $column, $columnType, $parameters[0]);
$operator = $this->createNotEqual($type, $expr, $column, $columnType, $parameters);
break;
case 'gt':
if (count($parameters) != 1) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new GreaterThan($expr, $column, $columnType, $parameters[0]);
$operator = $this->createGreaterThan($type, $expr, $column, $columnType, $parameters);
break;
case 'gte':
if (count($parameters) != 1) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new GreaterThanEqual($expr, $column, $columnType, $parameters[0]);
$operator = $this->createGreaterThanEqual($type, $expr, $column, $columnType, $parameters);
break;
case 'lt':
if (count($parameters) != 1) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new LessThan($expr, $column, $columnType, $parameters[0]);
$operator = $this->createLessThan($type, $expr, $column, $columnType, $parameters);
break;
case 'lte':
if (count($parameters) != 1) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new LessThanEqual($expr, $column, $columnType, $parameters[0]);
$operator = $this->createLessThanEqual($type, $expr, $column, $columnType, $parameters);
break;
case 'lk':
if (count($parameters) != 1) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new Like($expr, $column, $columnType, $parameters[0]);
$operator = $this->createLike($type, $expr, $column, $columnType, $parameters);
break;
case 'nlk':
if (count($parameters) != 1) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new NotLike($expr, $column, $columnType, $parameters[0]);
$operator = $this->createNotLike($type, $expr, $column, $columnType, $parameters);
break;
case 'in':
if (count($parameters) < 1) {
throw OperatorException::inBadNumberOfParameters($type);
}
return new In($expr, $column, $columnType, $parameters);
$operator = $this->createIn($type, $expr, $column, $columnType, $parameters);
break;
case 'nin':
if (count($parameters) < 1) {
throw OperatorException::inBadNumberOfParameters($type);
}
return new NotIn($expr, $column, $columnType, $parameters);
$operator = $this->createNotIn($type, $expr, $column, $columnType, $parameters);
break;
case 'nl':
if (count($parameters) != 0) {
throw OperatorException::operatorBadNumberOfParameters($type, 0);
}
return new OperatorNull($expr, $column, $columnType);
$operator = $this->createOperatorNull($type, $expr, $column, $columnType, $parameters);
break;
case 'nnl':
if (count($parameters) != 0) {
throw OperatorException::operatorBadNumberOfParameters($type, 0);
}
return new OperatorNotNull($expr, $column, $columnType);
$operator = $this->createOperatorNotNull($type, $expr, $column, $columnType, $parameters);
break;
case 'js':
if ($columnType !== 'json') {
throw OperatorException::operatorJsonNeedColumnTypeJson();
}
if (count($parameters) != 3) {
throw OperatorException::operatorBadNumberOfParameters($type, 3);
}
return new JsonPostgres($expr, $column, $columnType, $parameters[0], $parameters[1], $parameters[2]);
$operator = $this->createJsonPostgres($type, $expr, $column, $columnType, $parameters);
break;
default:
throw OperatorException::unknownOperator($type);
}
return $operator;
}
private function createBetween(
string $type,
ExpressionBuilder $expr,
string $column,
string $columnType,
array $parameters
): Between {
if (count($parameters) != 2) {
throw OperatorException::operatorBadNumberOfParameters($type, 2);
}
return new Between($expr, $column, $columnType, $parameters[0], $parameters[1]);
}
private function createEqual(
string $type,
ExpressionBuilder $expr,
string $column,
string $columnType,
array $parameters
): Equal {
if (count($parameters) != 1) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new Equal($expr, $column, $columnType, $parameters[0]);
}
private function createNotEqual(
string $type,
ExpressionBuilder $expr,
string $column,
string $columnType,
array $parameters
): NotEqual {
if (count($parameters) != 1) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new NotEqual($expr, $column, $columnType, $parameters[0]);
}
private function createGreaterThan(
string $type,
ExpressionBuilder $expr,
string $column,
string $columnType,
array $parameters
): GreaterThan {
if (count($parameters) != 1) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new GreaterThan($expr, $column, $columnType, $parameters[0]);
}
private function createGreaterThanEqual(
string $type,
ExpressionBuilder $expr,
string $column,
string $columnType,
array $parameters
): GreaterThanEqual {
if (count($parameters) != 1) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new GreaterThanEqual($expr, $column, $columnType, $parameters[0]);
}
private function createLessThan(
string $type,
ExpressionBuilder $expr,
string $column,
string $columnType,
array $parameters
): LessThan {
if (count($parameters) != 1) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new LessThan($expr, $column, $columnType, $parameters[0]);
}
private function createLessThanEqual(
string $type,
ExpressionBuilder $expr,
string $column,
string $columnType,
array $parameters
): LessThanEqual {
if (count($parameters) != 1) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new LessThanEqual($expr, $column, $columnType, $parameters[0]);
}
private function createLike(
string $type,
ExpressionBuilder $expr,
string $column,
string $columnType,
array $parameters
): Like {
if (count($parameters) != 1) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new Like($expr, $column, $columnType, $parameters[0]);
}
private function createNotLike(
string $type,
ExpressionBuilder $expr,
string $column,
string $columnType,
array $parameters
): NotLike {
if (count($parameters) != 1) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
}
return new NotLike($expr, $column, $columnType, $parameters[0]);
}
private function createIn(
string $type,
ExpressionBuilder $expr,
string $column,
string $columnType,
array $parameters
): In {
if (count($parameters) < 1) {
throw OperatorException::inBadNumberOfParameters($type);
}
return new In($expr, $column, $columnType, $parameters);
}
private function createNotIn(
string $type,
ExpressionBuilder $expr,
string $column,
string $columnType,
array $parameters
): NotIn {
if (count($parameters) < 1) {
throw OperatorException::inBadNumberOfParameters($type);
}
return new NotIn($expr, $column, $columnType, $parameters);
}
private function createOperatorNull(
string $type,
ExpressionBuilder $expr,
string $column,
string $columnType,
array $parameters
): OperatorNull {
if (count($parameters) != 0) {
throw OperatorException::operatorBadNumberOfParameters($type, 0);
}
return new OperatorNull($expr, $column, $columnType);
}
private function createOperatorNotNull(
string $type,
ExpressionBuilder $expr,
string $column,
string $columnType,
array $parameters
): OperatorNotNull {
if (count($parameters) != 0) {
throw OperatorException::operatorBadNumberOfParameters($type, 0);
}
return new OperatorNotNull($expr, $column, $columnType);
}
private function createJsonPostgres(
string $type,
ExpressionBuilder $expr,
string $column,
string $columnType,
array $parameters
): JsonPostgres {
if ($columnType !== 'json') {
throw OperatorException::operatorJsonNeedColumnTypeJson();
}
if (count($parameters) != 3) {
throw OperatorException::operatorBadNumberOfParameters($type, 3);
}
return new JsonPostgres($expr, $column, $columnType, $parameters[0], $parameters[1], $parameters[2]);
}
}
......@@ -22,18 +22,6 @@ use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
*/
class OperatorNotNull extends Operator
{
/**
* Create the class before call getExpression method to execute this operator
*
* @param ExpressionBuilder $expr
* @param string $column
* @param string $columnType
*/
public function __construct(ExpressionBuilder $expr, string $column, string $columnType)
{
parent::__construct($expr, $column, $columnType);
}
/**
* This method returns the not null expression for this criterion