Completed
Pull Request — develop (#337)
by Bastian
06:24
created

MalformedInputException   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 37
ccs 8
cts 8
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setErrorType() 0 6 2
1
<?php
2
/**
3
 * MalformedInput exception class
4
 */
5
6
namespace Graviton\ExceptionBundle\Exception;
7
8
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
9
use Symfony\Component\HttpFoundation\Response;
10
11
/**
12
 * MalformedInput exception class
13
 *
14
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
15
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
16
 * @link     http://swisscom.ch
17
 */
18
final class MalformedInputException extends BadRequestHttpException implements RestExceptionInterface
19
{
20
    use RestExceptionTrait;
21
22
    private $errorTypes = array(
23
        JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
24
        JSON_ERROR_STATE_MISMATCH => 'Underflow or modes mismatch',
25
        JSON_ERROR_CTRL_CHAR => 'Unexpected control character found',
26
        JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON',
27
        JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded'
28
    );
29
30
    /**
31
     * Constructor
32
     *
33
     * @param string     $message Error message
34
     * @param \Exception $prev    Previous Exception
35
     */
36 2
    public function __construct($message = "Malformed input", $prev = null)
37
    {
38 2
        parent::__construct($message, $prev, Response::HTTP_INTERNAL_SERVER_ERROR);
39 2
    }
40
41
    /**
42
     * Sets the specific json error type to make consistent error reporting (and thus testing) possible
43
     *
44
     * @param int $error Error constant from json_last_error
45
     *
46
     * @return void
47
     */
48 1
    public function setErrorType($error)
49
    {
50 1
        if (isset($this->errorTypes[$error])) {
51 1
            $this->message = trim($this->errorTypes[$error].': '.$this->message);
52 1
        }
53 1
    }
54
}
55