Passed
Push — master ( 9da6fd...0354d3 )
by Rafael
05:30
created

ErrorQueue::throw()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 1
nop 1
crap 2
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Error;
12
13
/**
14
 * Throw errors in a queue in order to display later
15
 * alongside the data in the response. Works similar to Symfony FlashBag message container.
16
 *
17
 * For example, a client make a mutation and the operation
18
 * is made partially, the server must need return the modified data to the client
19
 * but is needed too inform of any error happened to display why the operation has been made partially.
20
 *
21
 * It’s like having a talk with a real human:
22
 * "Hey Johny, here are all you asked me to save. I did everything except that task; I went to save it up, but I don't permission to save it".
23
 */
24
class ErrorQueue
25
{
26
    /**
27
     * @var array|\Throwable[]
28
     */
29
    private static $errors = [];
30
31
    /**
32
     * @param \Throwable $error
33
     */
34 1
    public static function throw(\Throwable $error): void
35
    {
36
        try {
37 1
            throw $error;
38 1
        } catch (\Throwable $e) {
39 1
            self::$errors[$error->getCode()] = $e;
40
        }
41 1
    }
42
43
    /**
44
     * Gets error for a given type.
45
     *
46
     * @param string|int $code
47
     *
48
     * @return null|\Throwable
49
     */
50
    public static function peek($code): ?\Throwable
51
    {
52
        return self::$errors[$code] ?? null;
53
    }
54
55
    /**
56
     * Gets all errors in queue.
57
     *
58
     * @return array
59
     */
60
    public static function peekAll(): array
61
    {
62
        return self::$errors;
63
    }
64
65
    /**
66
     * Remove given error code from the queue
67
     *
68
     * @param string|int $code
69
     */
70
    public static function remove($code): void
71
    {
72
        unset(self::$errors[$code]);
73
    }
74
75
    /**
76
     * Has error for a given code?
77
     *
78
     * @param string|int $code
79
     *
80
     * @return bool
81
     */
82
    public static function has($code): bool
83
    {
84
        return isset(self::$errors[$code]);
85
    }
86
87
    /**
88
     * Gets and clears errors from the queue.
89
     *
90
     * @return array|\Throwable[]
91
     */
92 22
    public static function all(): array
93
    {
94 22
        $errors = self::$errors;
95 22
        self::$errors = [];
96
97 22
        return $errors;
98
    }
99
}
100