Passed
Push — wip-public-release ( a47743...14cdbd )
by Bogdan
02:52
created

FunctionCallHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 2
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\Specs\FunctionSpecInterface;
20
use Pinepain\JsSandbox\Wrappers\FunctionComponents\Runtime\ColdExecutionContextInterface;
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 FunctionExceptionHandlerInterface
33
     */
34
    private $exception_handler;
35
    /**
36
     * @var ReturnValueSetterInterface
37
     */
38
    private $return_setter;
39
40
    /**
41
     * @param ArgumentsExtractorInterface       $arguments_extractor
42
     * @param FunctionExceptionHandlerInterface $exception_handler
43
     * @param ReturnValueSetterInterface        $return_setter
44
     */
45
    public function __construct(ArgumentsExtractorInterface $arguments_extractor, FunctionExceptionHandlerInterface $exception_handler, ReturnValueSetterInterface $return_setter)
46
    {
47
        $this->arguments_extractor = $arguments_extractor;
48
        $this->exception_handler   = $exception_handler;
49
        $this->return_setter       = $return_setter;
50
    }
51
52
    public function wrap(callable $callback, FunctionSpecInterface $spec, ColdExecutionContextInterface $cold_execution_context)
53
    {
54
        return function (FunctionCallbackInfo $args) use ($callback, $spec, $cold_execution_context) {
55
            $arguments = $this->arguments_extractor->extract($args, $spec);
56
57
            if ($spec->needsExecutionContext()) {
58
                // Execution context is simple and abstract way to write advanced functions which relies on existent
59
                // abstraction level but at the same time allow manipulate on a lower level, e.g. examine current
60
                // context, building rich v8 native objects, but not limited to.
61
                $execution_context = $cold_execution_context->warm($args, $spec);
62
                array_unshift($arguments, $execution_context);
63
            }
64
65
            try {
66
                $ret = $callback(...$arguments);
67
            } catch (Throwable $e) {
68
                $this->exception_handler->handle($args->getIsolate(), $args->getContext(), $e, $spec->getExceptions());
69
70
                // Handling exception means process thrown exception in some way, e.g. cleanup message, hide or add details
71
                // and then throw new exception that inherits SandboxException. If it doesn't happened, we re-throw original
72
                // exception
73
                throw $e;
74
            }
75
76
            if (null !== $ret || !$spec->getReturn()->prefersUndefined()) {
77
                $this->return_setter->set($cold_execution_context->getWrapper(), $args, $spec, $ret);
78
            }
79
        };
80
    }
81
}
82