Passed
Pull Request — master (#34)
by Adam
03:39
created

ApiErrorException::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 4
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace AcquiaCloudApi\Exception;
4
5
use \Exception;
6
7
/**
8
 * Represents an error returned from the API.
9
 */
10
class ApiErrorException extends Exception
11
{
12
    /**
13
     * ApiErrorException Constructor.
14
     *
15
     * @param object    $object
16
     * @param string    $message
17
     * @param int       $code
18
     * @param Exception $previous
19
     */
20
    public function __construct($object, $message = "", $code = 0, Exception $previous = null)
21
    {
22
        parent::__construct($message, $code, $previous);
23
24
        $this->setError($object);
25
    }
26
27
    /**
28
     * __toString() magic method.
29
     */
30
    public function __toString()
31
    {
32
        return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
33
    }
34
35
    /**
36
     * Sets the error.
37
     *
38
     * @param object $object
39
     */
40
    public function setError($object)
41
    {
42
        if (is_array($object->message) || is_object($object->message)) {
43
            $output = '';
44
            foreach ($object->message as $message) {
45
                $output .= $message . PHP_EOL;
46
            }
47
            $this->message = $output;
48
        } else {
49
            $this->code = $object->error;
50
            $this->message = $object->message;
51
        }
52
    }
53
}
54