Completed
Pull Request — master (#137)
by
unknown
03:10
created

ErrorContainerTrait::clearErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
eloc 3
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\Exception\Interfaces\DatableExceptionInterface;
12
use Youshido\GraphQL\Exception\Interfaces\LocationableExceptionInterface;
13
14
trait ErrorContainerTrait
15
{
16
17
    /** @var \Exception[] */
18
    protected $errors = [];
19
20 42
    public function addError(\Exception $exception)
21
    {
22 42
        $this->errors[] = $exception;
23
24 42
        return $this;
25
    }
26
27 91
    public function hasErrors()
28
    {
29 91
        return !empty($this->errors);
30
    }
31
32 1
    public function getErrors()
33
    {
34 1
        return $this->errors;
35
    }
36
37 1
    public function mergeErrors(ErrorContainerInterface $errorContainer)
38
    {
39 1
        if ($errorContainer->hasErrors()) {
40 1
            foreach ($errorContainer->getErrors() as $error) {
41 1
                $this->addError($error);
42 1
            }
43 1
        }
44
45 1
        return $this;
46
    }
47
48 42
    public function getErrorsArray($inGraphQLStyle = true)
49
    {
50 42
        $errors = [];
51
52 42
        foreach ($this->errors as $error) {
53 42
            if ($inGraphQLStyle) {
54 21
                if ($error instanceof DatableExceptionInterface) {
55 1
                    $errors[] = array_merge(
56 1
                        ['message' => $error->getMessage()],
57 1
                        $error->getData() ?: [],
58 1
                        $error->getCode() ? ['code' => $error->getCode()] : []
59 1
                    );
60 21
                } elseif ($error instanceof LocationableExceptionInterface) {
61 18
                    $errors[] = array_merge(
62 18
                        ['message' => $error->getMessage()],
63 18
                        $error->getLocation() ? ['locations' => [$error->getLocation()->toArray()]] : [],
64 18
                        $error->getCode() ? ['code' => $error->getCode()] : []
65 18
                    );
66 18
                } else {
67 4
                    if ($this->container) {
68 3
                        $this->container->get('logger')->emerg($error);
0 ignored issues
show
Bug introduced by
The property container does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
69
                    }
70
71
                    $errors[] = array_merge(
72
                        ['message' => $error->getMessage()],
73
                        $error->getCode() ? ['code' => $error->getCode()] : []
74
                    );
75
                }
76 19
            } else {
77 21
                $errors[] = $error->getMessage();
78
            }
79 40
        }
80
81 40
        return $errors;
82
    }
83
84 116
    public function clearErrors()
85
    {
86 116
        $this->errors = [];
87
88 116
        return $this;
89
    }
90
91
}