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

ErrorHandler::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
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