Completed
Pull Request — master (#133)
by Adrien
04:00
created

ErrorContainerTrait   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 17
c 1
b 1
f 0
lcom 1
cbo 4
dl 0
loc 74
ccs 42
cts 42
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addError() 0 6 1
A hasErrors() 0 4 1
A getErrors() 0 4 1
A mergeErrors() 0 10 3
A clearErrors() 0 6 1
D getErrorsArray() 0 31 10
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 90
    public function hasErrors()
28
    {
29 90
        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
                    $errors[] = array_merge(
68 4
                        ['message' => $error->getMessage()],
69 4
                        $error->getCode() ? ['code' => $error->getCode()] : []
70 4
                    );
71
                }
72 21
            } else {
73 21
                $errors[] = $error->getMessage();
74
            }
75 42
        }
76
77 42
        return $errors;
78
    }
79
80 115
    public function clearErrors()
81
    {
82 115
        $this->errors = [];
83
84 115
        return $this;
85
    }
86
87
}