Completed
Push — master ( d52ee7...e7842c )
by Alexandr
03:54
created

ErrorContainerTrait::hasErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Date: 01.12.15
4
 *
5
 * @author Portey Vasil <[email protected]>
6
 */
7
8
namespace Youshido\GraphQL\Validator\ErrorContainer;
9
10
11
use Youshido\GraphQL\Validator\Exception\DatableResolveException;
12
13
trait ErrorContainerTrait
14
{
15
16
    /** @var \Exception[] */
17
    protected $errors = [];
18
19 29
    public function addError(\Exception $exception)
20
    {
21 29
        $this->errors[] = $exception;
22
23 29
        return $this;
24
    }
25
26 100
    public function hasErrors()
27
    {
28 100
        return !empty($this->errors);
29
    }
30
31 2
    public function getErrors()
32
    {
33 2
        return $this->errors;
34
    }
35
36 1
    public function mergeErrors(ErrorContainerInterface $errorContainer)
37
    {
38 1
        if ($errorContainer->hasErrors()) {
39 1
            foreach ($errorContainer->getErrors() as $error) {
40 1
                $this->addError($error);
41 1
            }
42 1
        }
43
44 1
        return $this;
45
    }
46
47 28
    public function getErrorsArray($inGraphQLStyle = true)
48
    {
49 28
        $errors = [];
50
51 28
        foreach ($this->errors as $error) {
52 28
            if ($inGraphQLStyle) {
53 8
                if ($error instanceof DatableResolveException) {
54 1
                    $errors[] = array_merge(
55 1
                        ['message' => $error->getMessage()],
56 1
                        $error->getData() ?: [],
57 1
                        $error->getCode() ? ['code' => $error->getCode()] : []
58 1
                    );
59 1
                } else {
60 8
                    $errors[] = array_merge(
61 8
                        ['message' => $error->getMessage()],
62 8
                        $error->getCode() ? ['code' => $error->getCode()] : []
63 8
                    );
64
                }
65 8
            } else {
66 20
                $errors[] = $error->getMessage();
67
            }
68 28
        }
69
70 28
        return $errors;
71
    }
72
73 6
    public function clearErrors()
74
    {
75 6
        $this->errors = [];
76
77 6
        return $this;
78
    }
79
80
}