FunctionCallHandler::wrap()   B
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 34
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.439
c 0
b 0
f 0
ccs 0
cts 8
cp 0
cc 5
eloc 15
nc 1
nop 2
crap 30
1
<?php declare(strict_types=1);
2
3
/*
4
 * This file is part of the pinepain/js-sandbox PHP library.
5
 *
6
 * Copyright (c) 2016-2017 Bogdan Padalko <[email protected]>
7
 *
8
 * Licensed under the MIT license: http://opensource.org/licenses/MIT
9
 *
10
 * For the full copyright and license information, please view the
11
 * LICENSE file that was distributed with this source or visit
12
 * http://opensource.org/licenses/MIT
13
 */
14
15
16
namespace Pinepain\JsSandbox\Wrappers\FunctionComponents;
17
18
19
use Pinepain\JsSandbox\Wrappers\FunctionComponents\Runtime\ColdExecutionContextInterface;
20
use Pinepain\JsSandbox\Wrappers\Runtime\RuntimeFunctionInterface;
21
use Throwable;
22
use V8\FunctionCallbackInfo;
23
24
25
class FunctionCallHandler implements FunctionCallHandlerInterface
26
{
27
    /**
28
     * @var ArgumentsExtractorInterface
29
     */
30
    private $arguments_extractor;
31
    /**
32
     * @var FunctionDecoratorInterface
33
     */
34
    private $decorator;
35
    /**
36
     * @var FunctionExceptionHandlerInterface
37
     */
38
    private $exception_handler;
39
    /**
40
     * @var ReturnValueSetterInterface
41
     */
42
    private $return_setter;
43
44
    /**
45
     * @param ArgumentsExtractorInterface $arguments_extractor
46
     * @param FunctionDecoratorInterface $decorator
47
     * @param FunctionExceptionHandlerInterface $exception_handler
48
     * @param ReturnValueSetterInterface $return_setter
49
     */
50
    public function __construct(
51
        ArgumentsExtractorInterface $arguments_extractor,
52
        FunctionDecoratorInterface $decorator,
53
        FunctionExceptionHandlerInterface $exception_handler,
54
        ReturnValueSetterInterface $return_setter
55
    ) {
56
        $this->arguments_extractor = $arguments_extractor;
57
        $this->decorator           = $decorator;
58
        $this->exception_handler   = $exception_handler;
59
        $this->return_setter       = $return_setter;
60
    }
61
62
    public function wrap(RuntimeFunctionInterface $function, ColdExecutionContextInterface $cold_execution_context)
63
    {
64
        return function (FunctionCallbackInfo $args) use ($function, $cold_execution_context) {
65
            $spec     = $function->getSpec();
66
            $callback = $function->getCallback();
67
68
            if ($spec->getDecorators()) {
69
                // When we have decorators, we need executions context.
70
                // Execution context is simple and abstract way to write advanced functions which relies on existent
71
                // abstraction level but at the same time allow manipulate on a lower level, e.g. examine current
72
                // context, building rich v8 native objects, but not limited to.
73
                $exec = $cold_execution_context->warm($args, $spec);
74
75
                $callback = $this->decorator->decorate($callback, $spec, $exec);
76
            }
77
78
            $arguments = $this->arguments_extractor->extract($args, $spec);
79
            
80
            try {
81
                $ret = $callback(...$arguments);
82
            } catch (Throwable $e) {
83
                $this->exception_handler->handle($args->getIsolate(), $args->getContext(), $e, $spec->getExceptions());
84
85
                // Handling exception means process thrown exception in some way, e.g. cleanup message, hide or add details
86
                // and then throw new exception that inherits SandboxException. If it doesn't happened, we re-throw original
87
                // exception
88
                throw $e;
89
            }
90
91
            if (null !== $ret || !$spec->getReturn()->prefersUndefined()) {
92
                $this->return_setter->set($cold_execution_context->getWrapper(), $args, $spec, $ret);
93
            }
94
        };
95
    }
96
}
97