Skip to content

Commit 042794b

Browse files
munkieGuilhemN
authored andcommitted
Use ExceptionValueMap to store exception values (codes or message) and resolve by exception, instead of trait
1 parent 389562f commit 042794b

File tree

8 files changed

+165
-62
lines changed

8 files changed

+165
-62
lines changed

Controller/ExceptionController.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace FOS\RestBundle\Controller;
1313

14-
use FOS\RestBundle\Util\ClassMapHandlerTrait;
14+
use FOS\RestBundle\Util\ExceptionValueMap;
1515
use FOS\RestBundle\View\View;
1616
use FOS\RestBundle\View\ViewHandlerInterface;
1717
use Symfony\Component\HttpFoundation\Request;
@@ -25,15 +25,24 @@
2525
*/
2626
class ExceptionController
2727
{
28-
use ClassMapHandlerTrait;
29-
28+
/**
29+
* @var ViewHandlerInterface
30+
*/
3031
private $viewHandler;
32+
33+
/**
34+
* @var ExceptionValueMap
35+
*/
3136
private $exceptionCodes;
37+
38+
/**
39+
* @var bool
40+
*/
3241
private $showException;
3342

3443
public function __construct(
3544
ViewHandlerInterface $viewHandler,
36-
array $exceptionCodes,
45+
ExceptionValueMap $exceptionCodes,
3746
$showException
3847
) {
3948
$this->viewHandler = $viewHandler;
@@ -92,7 +101,7 @@ protected function createView(\Exception $exception, $code, array $templateData,
92101
protected function getStatusCode(\Exception $exception)
93102
{
94103
// If matched
95-
if ($statusCode = $this->resolveValue(get_class($exception), $this->exceptionCodes)) {
104+
if ($statusCode = $this->exceptionCodes->resolveException($exception)) {
96105
return $statusCode;
97106
}
98107

Controller/TemplatingExceptionController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace FOS\RestBundle\Controller;
1313

14+
use FOS\RestBundle\Util\ExceptionValueMap;
1415
use FOS\RestBundle\View\ViewHandlerInterface;
1516
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
1617
use Symfony\Component\HttpFoundation\Request;
@@ -22,7 +23,7 @@ abstract class TemplatingExceptionController extends ExceptionController
2223

2324
public function __construct(
2425
ViewHandlerInterface $viewHandler,
25-
array $exceptionCodes,
26+
ExceptionValueMap $exceptionCodes,
2627
$showException,
2728
EngineInterface $templating
2829
) {

DependencyInjection/FOSRestExtension.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,9 @@ private function loadException(array $config, XmlFileLoader $loader, ContainerBu
322322
$container->getDefinition('fos_rest.exception_listener')->replaceArgument(0, 'fos_rest.exception.twig_controller:showAction');
323323
}
324324

325-
$container->getDefinition('fos_rest.exception.controller')
326-
->replaceArgument(1, $config['exception']['codes']);
327-
$container->getDefinition('fos_rest.serializer.exception_normalizer.jms')
328-
->replaceArgument(0, $config['exception']['messages']);
329-
$container->getDefinition('fos_rest.serializer.exception_normalizer.symfony')
325+
$container->getDefinition('fos_rest.exception.codes_map')
326+
->replaceArgument(0, $config['exception']['codes']);
327+
$container->getDefinition('fos_rest.exception.messages_map')
330328
->replaceArgument(0, $config['exception']['messages']);
331329
}
332330

Resources/config/exception_listener.xml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,30 @@
1414

1515
<service id="fos_rest.exception.controller" class="FOS\RestBundle\Controller\ExceptionController">
1616
<argument type="service" id="fos_rest.view_handler" />
17-
<argument type="collection" /> <!-- exception codes -->
17+
<argument type="service" id="fos_rest.exception.codes_map" /> <!-- exception codes -->
1818
<argument>%kernel.debug%</argument>
1919
</service>
2020

2121
<service id="fos_rest.exception.twig_controller" class="FOS\RestBundle\Controller\TwigExceptionController" parent="fos_rest.exception.controller">
2222
<argument type="service" id="templating.engine.twig" />
2323
</service>
2424

25+
<service id="fos_rest.exception.codes_map" class="FOS\RestBundle\Util\ExceptionValueMap" public="false">
26+
<argument type="collection"/> <!-- exception codes -->
27+
</service>
28+
29+
<service id="fos_rest.exception.messages_map" class="FOS\RestBundle\Util\ExceptionValueMap" public="false">
30+
<argument type="collection"/> <!-- exception messages -->
31+
</service>
32+
2533
<service id="fos_rest.serializer.exception_normalizer.jms" class="FOS\RestBundle\Serializer\Normalizer\ExceptionHandler">
26-
<argument type="collection" /> <!-- exception messages -->
34+
<argument type="service" id="fos_rest.exception.messages_map" /> <!-- exception messages -->
2735
<argument>%kernel.debug%</argument>
2836
<tag name="jms_serializer.subscribing_handler" />
2937
</service>
3038

3139
<service id="fos_rest.serializer.exception_normalizer.symfony" class="FOS\RestBundle\Serializer\Normalizer\ExceptionNormalizer" public="false">
32-
<argument type="collection" /> <!-- exception messages -->
40+
<argument type="service" id="fos_rest.exception.messages_map" /> <!-- exception messages -->
3341
<argument>%kernel.debug%</argument>
3442
<tag name="serializer.normalizer" />
3543
</service>

Serializer/Normalizer/AbstractExceptionNormalizer.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace FOS\RestBundle\Serializer\Normalizer;
1313

14-
use FOS\RestBundle\Util\ClassMapHandlerTrait;
14+
use FOS\RestBundle\Util\ExceptionValueMap;
1515
use Symfony\Component\HttpFoundation\Response;
1616

1717
/**
@@ -21,16 +21,21 @@
2121
*/
2222
class AbstractExceptionNormalizer
2323
{
24-
use ClassMapHandlerTrait;
25-
24+
/**
25+
* @var ExceptionValueMap
26+
*/
2627
private $messagesMap;
28+
29+
/**
30+
* @var bool
31+
*/
2732
private $debug;
2833

2934
/**
3035
* @param array $messagesMap
3136
* @param bool $debug
3237
*/
33-
public function __construct(array $messagesMap, $debug)
38+
public function __construct(ExceptionValueMap $messagesMap, $debug)
3439
{
3540
$this->messagesMap = $messagesMap;
3641
$this->debug = $debug;
@@ -46,7 +51,7 @@ public function __construct(array $messagesMap, $debug)
4651
*/
4752
protected function getExceptionMessage(\Exception $exception, $statusCode = null)
4853
{
49-
$showMessage = $this->resolveValue(get_class($exception), $this->messagesMap);
54+
$showMessage = $this->messagesMap->resolveException($exception);
5055

5156
if ($showMessage || $this->debug) {
5257
return $exception->getMessage();
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRestBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\RestBundle\Tests;
13+
14+
use FOS\RestBundle\Util\ExceptionValueMap;
15+
16+
/**
17+
* ExceptionValueMap test.
18+
*
19+
* @author Mikhail Shamin <[email protected]>
20+
*/
21+
class ExceptionValueMapTest extends \PHPUnit_Framework_TestCase
22+
{
23+
/**
24+
* @var ExceptionValueMap
25+
*/
26+
private $valueMap;
27+
28+
protected function setUp()
29+
{
30+
$map = [
31+
\LogicException::class => 'logic',
32+
\DomainException::class => 'domain',
33+
\OutOfBoundsException::class => null,
34+
];
35+
36+
$this->valueMap = new ExceptionValueMap($map);
37+
}
38+
39+
public function testResolveExceptionValueIsFound()
40+
{
41+
$this->assertSame('logic', $this->valueMap->resolveException(new \LogicException()));
42+
}
43+
44+
public function testResolveExceptionValueIsFoundBySubclass()
45+
{
46+
$this->assertSame('logic', $this->valueMap->resolveException(new \BadFunctionCallException()));
47+
}
48+
49+
public function testResolveExceptionValueNotFound()
50+
{
51+
$this->assertFalse($this->valueMap->resolveException(new \RuntimeException()));
52+
}
53+
}

Util/ClassMapHandlerTrait.php

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

Util/ExceptionValueMap.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRestBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\RestBundle\Util;
13+
14+
/**
15+
* Stores map of values mapped to exception class
16+
* Resolves value by exception.
17+
*
18+
* @author Mikhail Shamin <[email protected]>
19+
*/
20+
class ExceptionValueMap
21+
{
22+
/**
23+
* Map of values mapped to exception class
24+
* key => exception class
25+
* value => value associated with exception.
26+
*
27+
* @var array
28+
*/
29+
private $map;
30+
31+
/**
32+
* @param array $map
33+
*/
34+
public function __construct(array $map)
35+
{
36+
$this->map = $map;
37+
}
38+
39+
/**
40+
* Resolves the value corresponding to an exception object.
41+
*
42+
* @param \Exception $exception
43+
*
44+
* @return mixed|false Value found or false is not found
45+
*/
46+
public function resolveException(\Exception $exception)
47+
{
48+
return $this->doResolveClass(get_class($exception));
49+
}
50+
51+
/**
52+
* Resolves the value corresponding to an exception class.
53+
*
54+
* @param string $class
55+
*
56+
* @return mixed|false if not found
57+
*/
58+
private function doResolveClass($class)
59+
{
60+
foreach ($this->map as $mapClass => $value) {
61+
if (!$value) {
62+
continue;
63+
}
64+
65+
if ($class === $mapClass || is_subclass_of($class, $mapClass)) {
66+
return $value;
67+
}
68+
}
69+
70+
return false;
71+
}
72+
}

0 commit comments

Comments
 (0)