Completed
Push — rest_bdd_errors ( a44e6e...d93549 )
by André
22:45
created

ServerException::getCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * File containing the ServerException 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
10
namespace eZ\Publish\Core\REST\Client\Exceptions;
11
12
use eZ\Publish\Core\REST\Client\Values\ErrorMessage;
13
use Throwable;
14
15
/**
16
 * Wraps a eZ\Publish\Core\REST\Client\Values\ErrorMessage to provide info from server.
17
 *
18
 * To be used as previous exception when throwing client exceptions for server error response codes.
19
 * Only works on PHP7 or higher as it needs to implement Throwable in order to be able to set all properties.
20
 */
21
class ServerException implements Throwable
22
{
23
    /**
24
     * @var ErrorMessage
25
     */
26
    private $errorValueObject;
27
28
    /**
29
     * @param ErrorMessage $error
30
     */
31
    public function __construct(ErrorMessage $error)
32
    {
33
        $this->errorValueObject = $error;
34
    }
35
36
    public function getMessage()
37
    {
38
        return $this->errorValueObject->message;
39
    }
40
41
    public function getCode()
42
    {
43
        return $this->errorValueObject->code;
44
    }
45
46
    public function getFile()
47
    {
48
        return $this->errorValueObject->file;
49
    }
50
51
    public function getLine()
52
    {
53
        return $this->errorValueObject->line;
54
    }
55
56
    public function getTrace()
57
    {
58
        return $this->errorValueObject->trace;
59
    }
60
61
    public function getTraceAsString()
62
    {
63
        return implode( "\n", $this->errorValueObject->trace);
64
    }
65
66
    public function getPrevious()
67
    {
68
        return;
69
    }
70
71
    public function __toString()
72
    {
73
        return <<< EOF
74
Error message ({$this->errorValueObject->code}): {$this->errorValueObject->message}
75
76
{$this->errorValueObject->description}
77
78
In {$this->errorValueObject->file}:{$this->errorValueObject->line}, trace:
79
80
{$this->errorValueObject->trace}
81
EOF;
82
    }
83
}
84