Completed
Pull Request — master (#146)
by
unknown
03:57
created

ErrorHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 34
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 5 1
A createPluginChain() 0 13 3
1
<?php
2
3
namespace Youshido\GraphQL\Execution\ErrorHandler;
4
5
use Youshido\GraphQL\Exception\InvalidErrorHandlerPluginException;
6
use Youshido\GraphQL\Execution\Context\ExecutionContext;
7
use Youshido\GraphQL\Execution\ErrorHandler\Plugin\ErrorHandlerPluginInterface;
8
9
class ErrorHandler
10
{
11
    /**
12
     * @var callable
13
     */
14
    private $pluginChain;
15
    /**
16
     * @param ErrorHandlerPluginInterface[] $plugins
17
     */
18 70
    public function __construct(array $plugins)
19
    {
20 70
        $this->pluginChain = $this->createPluginChain($plugins);
21 70
    }
22
23 19
    public function handle($error, ExecutionContext $executionContext)
24
    {
25 19
        $pluginChain = $this->pluginChain;
26 19
        return $pluginChain($error, $executionContext);
27
    }
28
29 70
    private function createPluginChain($pluginList)
30
    {
31
        $lastCallable = function () {}; // Last callable is a NO-OP
32 70
        while ($plugin = array_pop($pluginList)) {
33 70
            if (! $plugin instanceof ErrorHandlerPluginInterface) {
34
                throw InvalidErrorHandlerPluginException::forPlugin($plugin);
35
            }
36 70
            $lastCallable = function ($error, ExecutionContext $executionContext) use ($plugin, $lastCallable) {
37 19
                return $plugin->execute($error, $executionContext, $lastCallable);
38 70
            };
39
        }
40 70
        return $lastCallable;
41
    }
42
}
43