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

AbstractExceptionNormalizer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 41
ccs 9
cts 9
cp 1
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\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