Completed
Pull Request — master (#1370)
by Mikhail
05:28
created

AbstractExceptionNormalizer::getExceptionMessage()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.2
cc 4
eloc 5
nc 3
nop 2
crap 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\ExceptionValueMap;
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
    /**
25
     * @var ExceptionValueMap
26
     */
27
    private $messagesMap;
28
29
    /**
30
     * @var bool
31
     */
32
    private $debug;
33
34
    /**
35
     * @param array $messagesMap
36
     * @param bool  $debug
37
     */
38 11
    public function __construct(ExceptionValueMap $messagesMap, $debug)
39
    {
40 11
        $this->messagesMap = $messagesMap;
41 11
        $this->debug = $debug;
42 11
    }
43
44
    /**
45
     * Extracts the exception message.
46
     *
47
     * @param \Exception $exception
48
     * @param int|null   $statusCode
49
     *
50
     * @return string
51
     */
52 6
    protected function getExceptionMessage(\Exception $exception, $statusCode = null)
53
    {
54 6
        $showMessage = $this->messagesMap->resolveException($exception);
55
56 6
        if ($showMessage || $this->debug) {
57 5
            return $exception->getMessage();
58
        }
59
60 1
        return array_key_exists($statusCode, Response::$statusTexts) ? Response::$statusTexts[$statusCode] : 'error';
61
    }
62
}
63