|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace M6Web\Bundle\ApiExceptionBundle\Manager; |
|
4
|
|
|
|
|
5
|
|
|
use M6Web\Bundle\ApiExceptionBundle\Exception\Interfaces\ExceptionInterface; |
|
6
|
|
|
use M6Web\Bundle\ApiExceptionBundle\Exception\Interfaces\HttpExceptionInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Manage exceptions to define public data returned |
|
10
|
|
|
*/ |
|
11
|
|
|
class ExceptionManager |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $defaultConfig; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $exceptions; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @param array $defaultConfig |
|
27
|
|
|
* @param array $exceptions |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(array $defaultConfig, array $exceptions) |
|
30
|
|
|
{ |
|
31
|
1 |
|
$this->defaultConfig = $defaultConfig; |
|
32
|
1 |
|
$this->exceptions = $exceptions; |
|
33
|
1 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Configure Exception |
|
37
|
|
|
* |
|
38
|
|
|
* @param ExceptionInterface $exception |
|
39
|
|
|
* |
|
40
|
|
|
* @return ExceptionInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
public function configure(ExceptionInterface $exception) |
|
43
|
|
|
{ |
|
44
|
1 |
|
$exceptionName = get_class($exception); |
|
45
|
|
|
|
|
46
|
1 |
|
$configException = $this->getConfigException($exceptionName); |
|
47
|
|
|
|
|
48
|
1 |
|
$exception->setCode($configException['code']); |
|
49
|
1 |
|
$exception->setMessage($configException['message']); |
|
50
|
|
|
|
|
51
|
1 |
|
if ($exception instanceof HttpExceptionInterface) { |
|
52
|
1 |
|
$exception->setStatusCode($configException['status']); |
|
53
|
1 |
|
$exception->setHeaders($configException['headers']); |
|
54
|
1 |
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
return $exception; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get config to exception |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $exceptionName |
|
63
|
|
|
* |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function getConfigException($exceptionName) |
|
67
|
|
|
{ |
|
68
|
1 |
|
$exceptionParentName = get_parent_class($exceptionName); |
|
69
|
|
|
|
|
70
|
1 |
|
if (in_array( |
|
71
|
1 |
|
'M6Web\Bundle\ApiExceptionBundle\Exception\Interfaces\ExceptionInterface', |
|
72
|
1 |
|
class_implements($exceptionParentName) |
|
73
|
1 |
|
)) { |
|
74
|
1 |
|
$parentConfig = $this->getConfigException($exceptionParentName); |
|
75
|
1 |
|
} else { |
|
76
|
1 |
|
$parentConfig = $this->defaultConfig; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
if (isset($this->exceptions[$exceptionName])) { |
|
80
|
1 |
|
return array_merge($parentConfig, $this->exceptions[$exceptionName]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
return $parentConfig; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|