Completed
Push — master ( 6744a4...ab6d26 )
by Guilh
7s
created

AbstractExceptionNormalizer::getExceptionMessage()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4.128

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 4
cts 5
cp 0.8
rs 9.2
cc 4
eloc 5
nc 3
nop 2
crap 4.128
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