FunctionWrapper   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 9
dl 0
loc 68
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B wrap() 0 30 4
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;
17
18
19
use Pinepain\JsSandbox\Wrappers\CallbackGuards\CallbackGuardInterface;
20
use Pinepain\JsSandbox\Wrappers\FunctionComponents\FunctionCallHandlerInterface;
21
use Pinepain\JsSandbox\Wrappers\FunctionComponents\FunctionWrappersCacheInterface;
22
use Pinepain\JsSandbox\Wrappers\FunctionComponents\Runtime\ColdExecutionContext;
23
use Pinepain\JsSandbox\Wrappers\ObjectComponents\WrappedObject;
24
use Pinepain\JsSandbox\Wrappers\Runtime\RuntimeFunction;
25
use V8\Context;
26
use V8\FunctionObject;
27
use V8\Isolate;
28
use V8\StringValue;
29
30
31
class FunctionWrapper implements WrapperInterface, WrapperAwareInterface
32
{
33
    use WrapperAwareTrait;
34
35
    /**
36
     * @var FunctionWrappersCacheInterface
37
     */
38
    private $cache;
39
    /**
40
     * @var FunctionCallHandlerInterface
41
     */
42
    private $handler;
43
    /**
44
     * @var CallbackGuardInterface
45
     */
46
    private $guard;
47
48
    /**
49
     * @param FunctionWrappersCacheInterface $cache
50
     * @param FunctionCallHandlerInterface   $handler
51
     * @param CallbackGuardInterface         $guard
52
     */
53
    public function __construct(FunctionWrappersCacheInterface $cache, FunctionCallHandlerInterface $handler, CallbackGuardInterface $guard)
54
    {
55
        $this->cache   = $cache;
56
        $this->handler = $handler;
57
        $this->guard   = $guard;
58
    }
59
60
    /**
61
     * @param Isolate         $isolate
62
     * @param Context         $context
63
     * @param RuntimeFunction $value
64
     *
65
     * @return FunctionObject
66
     * @throws WrapperException
67
     */
68
    public function wrap(Isolate $isolate, Context $context, $value): FunctionObject
69
    {
70
        if (!($value instanceof RuntimeFunction)) {
71
            throw new WrapperException('Invalid function value to wrap');
72
        }
73
74
        if ($this->cache->has($value)) {
75
            return $this->cache->get($value);
76
        }
77
78
        $cold_execution_context = new ColdExecutionContext($this->wrapper, $value);
79
80
        $callback = $this->handler->wrap($value, $cold_execution_context);
81
        $callback = $this->guard->guard($callback);
82
83
        $f = new FunctionObject($context, $callback);
84
85
        if ($value->getObject()) {
86
87
            // we have function which may act as object, pass it to wrapper as we need it to be properly registered
88
            $wrapped = new WrappedObject($f, $value->getObject(), $value->getObjectSpec());
89
            $this->wrapper->wrap($isolate, $context, $wrapped);
90
        }
91
92
        $f->setName(new StringValue($isolate, $value->getName()));
93
94
        $this->cache->put($value, $f);
95
96
        return $f;
97
    }
98
}
99