Completed
Push — master ( f979a7...d9e3ed )
by Portey
06:50
created

ErrorContainerTrait   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 71.43%

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 13
c 4
b 2
f 0
lcom 1
cbo 1
dl 0
loc 63
ccs 25
cts 35
cp 0.7143
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addError() 0 4 1
A hasErrors() 0 4 1
A getErrors() 0 4 1
D getErrorsArray() 0 33 9
A clearErrors() 0 6 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 2
    public function addError(\Exception $exception)
20
    {
21 2
        $this->errors[] = $exception;
22 2
    }
23
24 27
    public function hasErrors()
25
    {
26 27
        return !empty($this->errors);
27
    }
28
29
    public function getErrors()
30
    {
31
        return $this->errors;
32
    }
33
34 2
    public function getErrorsArray($asObject = true)
35
    {
36 2
        $errors = [];
37
38 2
        foreach ($this->errors as $error) {
39 2
            if ($asObject) {
40 1
                if (is_object($error)) {
41 1
                    if ($error instanceof DatableResolveException) {
42
                        $errors[] = array_merge(
43
                            ['message' => $error->getMessage()],
44
                            $error->getData() ?: [],
45
                            $error->getCode() ? ['code' => $error->getCode()] : []
46
                        );
47
                    } else {
48 1
                        $errors[] = array_merge(
49 1
                            ['message' => $error->getMessage()],
50 1
                            $error->getCode() ? ['code' => $error->getCode()] : []
51 1
                        );
52
                    }
53 1
                } else {
54
                    $errors[] = ['message' => $error];
55
                }
56 1
            } else {
57 1
                if (is_object($error)) {
58 1
                    $errors[] = $error->getMessage();
59 1
                } else {
60
                    $errors[] = $error;
61
                }
62
            }
63 2
        }
64
65 2
        return $errors;
66
    }
67
68 18
    public function clearErrors()
69
    {
70 18
        $this->errors = [];
71
72 18
        return $this;
73
    }
74
75
}