Skip to content

Commit 9b5d8a2

Browse files
committed
IBX-8180: Applied review remarks
1 parent d61d718 commit 9b5d8a2

File tree

7 files changed

+37
-20
lines changed

7 files changed

+37
-20
lines changed

src/bundle/Resources/config/routing.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,8 @@ ibexa.rest.languages.view:
405405

406406
ibexa.rest.location.swap:
407407
path: /content/locations/{locationPath}
408-
defaults:
409-
_controller: Ibexa\Rest\Server\Controller\Location:swap
410-
condition: 'is_content_type_compatible(request, "application/vnd.ibexa.api.SwapLocationInput")'
408+
controller: Ibexa\Rest\Server\Controller\Location:swap
409+
condition: 'ibexa_get_media_type(request) === "SwapLocationInput"'
411410
methods: [POST]
412411
requirements:
413412
locationPath: "[0-9/]+"

src/lib/Server/Input/Parser/AbstractDestinationLocationParser.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99

1010
use Ibexa\Contracts\Core\Repository\LocationService;
1111
use Ibexa\Contracts\Core\Repository\Values\Content\Location;
12-
use Ibexa\Contracts\Rest\Exceptions;
1312
use Ibexa\Contracts\Rest\Input\ParsingDispatcher;
1413
use Ibexa\Rest\Input\BaseParser;
14+
use Ibexa\Rest\Server\Exceptions\ValidationFailedException;
1515
use Ibexa\Rest\Server\Validation\Builder\Input\Parser\BaseInputParserValidatorBuilder;
1616
use Symfony\Component\Validator\Validator\ValidatorInterface;
1717

1818
abstract class AbstractDestinationLocationParser extends BaseParser
1919
{
20+
protected const string PARSER = '';
2021
public const string DESTINATION_KEY = 'destination';
2122

2223
public function __construct(
@@ -60,18 +61,23 @@ private function extractLocationIdFromPath(string $path): int
6061
}
6162

6263
/**
64+
* @phpstan-assert array{
65+
* 'destination': string,
66+
* } $data
67+
*
6368
* @param array<mixed> $data
6469
*
65-
* @throws \Ibexa\Contracts\Rest\Exceptions\Parser
70+
* @throws \Ibexa\Rest\Server\Exceptions\ValidationFailedException
6671
*/
6772
private function validateInputData(array $data): void
6873
{
6974
$builder = $this->getValidatorBuilder();
7075
$builder->validateInputArray($data);
71-
$errors = $builder->build()->getViolations();
72-
if ($errors->count() > 0) {
73-
throw new Exceptions\Parser(
74-
"The 'destination' element is malformed or missing."
76+
$violations = $builder->build()->getViolations();
77+
if ($violations->count() > 0) {
78+
throw new ValidationFailedException(
79+
static::PARSER,
80+
$violations,
7581
);
7682
}
7783
}

src/lib/Server/Input/Parser/MoveLocation.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
final class MoveLocation extends AbstractDestinationLocationParser
1515
{
16+
protected const string PARSER = 'MoveLocation';
17+
1618
protected function getValidatorBuilder(): BaseInputParserValidatorBuilder
1719
{
1820
return new MoveLocationInputValidatorBuilder($this->validator);

src/lib/Server/Input/Parser/SwapLocationInput.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
final class SwapLocationInput extends AbstractDestinationLocationParser
1515
{
16+
protected const string PARSER = 'SwapLocationInput';
17+
1618
protected function getValidatorBuilder(): BaseInputParserValidatorBuilder
1719
{
1820
return new SwapLocationInputValidatorBuilder($this->validator);

tests/lib/Server/Input/Parser/AbstractDestinationLocationInputTest.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
namespace Ibexa\Tests\Rest\Server\Input\Parser;
1010

1111
use Ibexa\Contracts\Core\Repository\LocationService;
12-
use Ibexa\Contracts\Rest\Exceptions\Parser;
1312
use Ibexa\Core\Repository\Values\Content\Location;
13+
use Ibexa\Rest\Server\Exceptions\ValidationFailedException;
1414
use Ibexa\Rest\Server\Input\Parser\AbstractDestinationLocationParser;
1515
use PHPUnit\Framework\MockObject\MockObject;
1616
use Symfony\Component\Validator\Validator\ValidatorInterface;
@@ -52,10 +52,12 @@ protected function parse(): void
5252
);
5353
}
5454

55-
protected function parseExceptionOnMissingDestinationElement(): void
55+
protected function parseExceptionOnMissingDestinationElement(string $parser): void
5656
{
57-
$this->expectException(Parser::class);
58-
$this->expectExceptionMessage("The 'destination' element is malformed or missing.");
57+
$this->expectException(ValidationFailedException::class);
58+
$this->expectExceptionMessage(
59+
sprintf('Input data validation failed for %s', $parser),
60+
);
5961

6062
$inputArray = [
6163
'new_destination' => '/1/2/3',
@@ -66,16 +68,18 @@ protected function parseExceptionOnMissingDestinationElement(): void
6668
$sessionInput->parse($inputArray, $this->getParsingDispatcherMock());
6769
}
6870

69-
protected function parseExceptionOnInvalidDestinationElement(): void
71+
protected function parseExceptionOnInvalidDestinationElement(string $parser): void
7072
{
7173
$inputArray = [
7274
'destination' => 'test_destination',
7375
];
7476

7577
$sessionInput = $this->getParser();
7678

77-
$this->expectException(Parser::class);
78-
$this->expectExceptionMessage("The 'destination' element is malformed or missing.");
79+
$this->expectException(ValidationFailedException::class);
80+
$this->expectExceptionMessage(
81+
sprintf('Input data validation failed for %s', $parser),
82+
);
7983

8084
$sessionInput->parse($inputArray, $this->getParsingDispatcherMock());
8185
}

tests/lib/Server/Input/Parser/MoveLocationTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,21 @@
1414

1515
final class MoveLocationTest extends AbstractDestinationLocationInputTest
1616
{
17+
private const string PARSER = 'MoveLocation';
18+
1719
public function testParse(): void
1820
{
1921
$this->parse();
2022
}
2123

2224
public function testParseExceptionOnMissingDestinationElement(): void
2325
{
24-
$this->parseExceptionOnMissingDestinationElement();
26+
$this->parseExceptionOnMissingDestinationElement(self::PARSER);
2527
}
2628

2729
public function testParseExceptionOnInvalidDestinationElement(): void
2830
{
29-
$this->parseExceptionOnInvalidDestinationElement();
31+
$this->parseExceptionOnInvalidDestinationElement(self::PARSER);
3032
}
3133

3234
protected function internalGetParser(): MoveLocation

tests/lib/Server/Input/Parser/SwapLocationInputTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,21 @@
1414

1515
final class SwapLocationInputTest extends AbstractDestinationLocationInputTest
1616
{
17+
private const string PARSER = 'SwapLocationInput';
18+
1719
public function testParse(): void
1820
{
1921
$this->parse();
2022
}
2123

2224
public function testParseExceptionOnMissingDestinationElement(): void
2325
{
24-
$this->parseExceptionOnMissingDestinationElement();
26+
$this->parseExceptionOnMissingDestinationElement(self::PARSER);
2527
}
2628

2729
public function testParseExceptionOnInvalidDestinationElement(): void
2830
{
29-
$this->parseExceptionOnInvalidDestinationElement();
31+
$this->parseExceptionOnInvalidDestinationElement(self::PARSER);
3032
}
3133

3234
protected function internalGetParser(): SwapLocationInput

0 commit comments

Comments
 (0)