Completed
Push — rest_bdd_errors ( d93549...822103 )
by André
20:35
created

ErrorMessage::parse()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 12
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 19
rs 8.2222
1
<?php
2
3
/**
4
 * File containing the ErrorMessage parser class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 *
9
 * @version //autogentag//
10
 */
11
12
namespace eZ\Publish\Core\REST\Client\Input\Parser;
13
14
use eZ\Publish\Core\REST\Common\Input\BaseParser;
15
use eZ\Publish\Core\REST\Common\Input\ParsingDispatcher;
16
use eZ\Publish\Core\REST\Client\Exceptions\ServerException;
17
use eZ\Publish\Core\REST\Client\Values\ErrorMessage as ErrorMessageValue;
18
19
/**
20
 * Parser for ErrorMessage.
21
 */
22
class ErrorMessage extends BaseParser
23
{
24
    /**
25
     * Mapping of error codes to the respective exception classes.
26
     *
27
     * @var array
28
     */
29
    protected $errorCodeMapping = array(
30
        403 => '\\eZ\\Publish\\Core\\REST\\Common\\Exceptions\\ForbiddenException',
31
        404 => '\\eZ\\Publish\\Core\\REST\\Common\\Exceptions\\NotFoundException',
32
        406 => '\\eZ\\Publish\\Core\\REST\\Client\\Exceptions\\InvalidArgumentException',
33
        409 => '\\eZ\\Publish\\Core\\REST\\Client\\Exceptions\\BadStateException',
34
    );
35
36
    /**
37
     * Parse input structure.
38
     *
39
     * @param array $data
40
     * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher
41
     *
42
     * @return \Exception|ErrorMessageValue
43
     */
44
    public function parse(array $data, ParsingDispatcher $parsingDispatcher)
45
    {
46
        $error = new ErrorMessageValue([
47
            'code' => $data['errorCode'],
48
            'message' => isset($data['errorMessage']) ? $data['errorMessage'] : null,
49
            'description' => isset($data['errorDescription']) ? $data['errorDescription'] : null,
50
            'trace' => isset($data['trace']) ? $data['trace'] : null,
51
            'file' => isset($data['file']) ? $data['file'] : null,
52
            'line' => isset($data['line']) ? $data['line'] : null,
53
        ]);
54
55
        // So client behaves closer to api, return relevant exceptions on status codes that maps to them
56
        if (isset($this->errorCodeMapping[$data['errorCode']])) {
57
            $exceptionClass = $this->errorCodeMapping[$data['errorCode']];
58
            return new $exceptionClass($data['errorDescription'], $data['errorCode'], new ServerException($error));
59
        }
60
61
        return $error;
62
    }
63
}
64