Completed
Pull Request — master (#1358)
by Guilh
07:06
created

AbstractExceptionNormalizer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 88.89%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 36
ccs 8
cts 9
cp 0.8889
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getExceptionMessage() 0 10 4
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\Serializer\Normalizer;
13
14
use FOS\RestBundle\Util\ClassMapHandlerTrait;
15
use Symfony\Component\HttpFoundation\Response;
16
17
/**
18
 * @author Ener-Getick <[email protected]>
19
 *
20
 * @internal do not use this class in your code.
21
 */
22
class AbstractExceptionNormalizer
23
{
24
    use ClassMapHandlerTrait;
25
26
    private $messagesMap;
27
    private $debug;
28
29
    /**
30
     * @param array $messagesMap
31
     * @param bool  $debug
32
     */
33 7
    public function __construct(array $messagesMap, $debug)
34
    {
35 7
        $this->messagesMap = $messagesMap;
36 7
        $this->debug = $debug;
37 7
    }
38
39
    /**
40
     * Extracts the exception message.
41
     *
42
     * @param \Exception $exception
43
     * @param int|null   $statusCode
44
     *
45
     * @return string
46
     */
47 4
    protected function getExceptionMessage(\Exception $exception, $statusCode = null)
48
    {
49 4
        $showMessage = $this->resolveValue(get_class($exception), $this->messagesMap);
50
51 4
        if ($showMessage || $this->debug) {
52 4
            return $exception->getMessage();
53
        }
54
55
        return array_key_exists($statusCode, Response::$statusTexts) ? Response::$statusTexts[$statusCode] : 'error';
56
    }
57
}
58