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

ApiErrorException::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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
    public function __construct($object, $message = "", $code = 0, Exception $previous = null)
13
    {
14
        parent::__construct($message, $code, $previous);
15
16
        $this->setError($object);
17
    }
18
19
    // custom string representation of object
20
    public function __toString()
21
    {
22
        return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
23
    }
24
25
    public function setError($object)
26
    {
27
        if (is_array($object->message)) {
28
            $output = '';
29
            foreach ($object->message as $message) {
30
                $output .= $message . PHP_EOL;
31
            }
32
            $this->message = $output;
33
        } else {
34
            $this->message = $object->message;
35
        }
36
    }
37
}
38