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

NativeFunctionWrapper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 4
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\NativeWrappers;
17
18
19
use Pinepain\JsSandbox\Wrappers\WrapperInterface;
20
use V8\Context;
21
use V8\FunctionObject;
22
use V8\ObjectValue;
23
24
25
class NativeFunctionWrapper implements NativeFunctionWrapperInterface
26
{
27
    /**
28
     * @var Context
29
     */
30
    private $context;
31
    /**
32
     * @var ObjectValue
33
     */
34
    private $recv;
35
    /**
36
     * @var FunctionObject
37
     */
38
    private $function_object;
39
    /**
40
     * @var WrapperInterface
41
     */
42
    private $wrapper;
43
44
    public function __construct(Context $context, ObjectValue $recv, FunctionObject $function_object, WrapperInterface $wrapper)
45
    {
46
        $this->context         = $context;
47
        $this->recv            = $recv;
48
        $this->function_object = $function_object;
49
        $this->wrapper         = $wrapper;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function call(...$args)
56
    {
57
        $args_for_call = [];
58
        foreach ($args as $arg) {
59
            $args_for_call[] = $this->wrapper->wrap($this->context->getIsolate(), $this->context, $arg);
60
        }
61
62
        return $this->function_object->call($this->context, $this->recv, $args_for_call);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function __invoke(...$args)
69
    {
70
        return $this->call(... $args);
71
    }
72
}
73