MultipleError::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Happyr\JsonApiResponseFactory\Model;
6
7
use Happyr\JsonApiResponseFactory\ResponseModelInterface;
8
9
/**
10
 * @author Tobias Nyholm <[email protected]>
11
 */
12
final class MultipleError implements ResponseModelInterface
13
{
14
    private $httpStatusCode;
15
16
    /**
17
     * @var AbstractError[]
18
     */
19
    private $errors;
20
21 3
    public function __construct(int $httpStatusCode, array $errors)
22
    {
23 3
        $this->httpStatusCode = $httpStatusCode;
24 3
        foreach ($errors as $error) {
25 1
            $this->addError($error);
26
        }
27 3
    }
28
29 2
    public function addError(AbstractError $error): void
30
    {
31 2
        $this->errors[] = $error;
32 2
    }
33
34 1
    public function getHttpStatusCode(): int
35
    {
36 1
        return $this->httpStatusCode;
37
    }
38
39 1
    public function getPayload(): array
40
    {
41 1
        $data = [];
42 1
        foreach ($this->errors as $error) {
43 1
            $data[] = $error->getErrorData();
44
        }
45
46
        return [
47 1
            'errors' => $data,
48
        ];
49
    }
50
}
51