Completed
Pull Request — master (#34)
by Adam
03:15
created

ApiErrorException::setError()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 11
rs 10
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->code = $object->error;
35
            $this->message = $object->message;
36
        }
37
    }
38
}
39