MalformedInputException::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 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  https://opensource.org/licenses/MIT MIT 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
0 ignored issues
show
Documentation introduced by
Should the type for parameter $prev not be \Exception|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
35
     */
36
    public function __construct($message = "Malformed input", $prev = null)
37
    {
38
        parent::__construct($message, $prev, Response::HTTP_INTERNAL_SERVER_ERROR);
39
    }
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
    public function setErrorType($error)
49
    {
50
        if (isset($this->errorTypes[$error])) {
51
            $this->message = trim($this->errorTypes[$error].': '.$this->message);
52
        }
53
    }
54
}
55