RuntimeObject   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 85
ccs 0
cts 25
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getSpec() 0 4 1
A getObject() 0 4 1
A getWrapper() 0 4 1
A hasMethod() 0 4 1
A getMethod() 0 19 3
A storeMethod() 0 8 1
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\Runtime;
17
18
19
use Pinepain\JsSandbox\Specs\ObjectSpecInterface;
20
use Pinepain\JsSandbox\Wrappers\WrapperInterface;
21
use Ref\WeakReference;
22
use V8\FunctionObject;
23
use V8\ObjectValue;
24
25
26
class RuntimeObject
27
{
28
    /**
29
     * @var object
30
     */
31
    private $object;
32
    /**
33
     * @var ObjectSpecInterface
34
     */
35
    private $spec;
36
    /**
37
     * @var WrapperInterface
38
     */
39
    private $wrapper;
40
41
    /**
42
     * @var RuntimeMethod[]
43
     */
44
    private $wrapped_methods = [];
45
46
    /**
47
     * @param object              $object
48
     * @param ObjectSpecInterface $spec
49
     */
50
    public function __construct($object, ObjectSpecInterface $spec, WrapperInterface $wrapper)
51
    {
52
        $this->object  = $object;
53
        $this->spec    = $spec;
54
        $this->wrapper = $wrapper;
55
    }
56
57
    public function getSpec(): ObjectSpecInterface
58
    {
59
        return $this->spec;
60
    }
61
62
    public function getObject()
63
    {
64
        return $this->object;
65
    }
66
67
    public function getWrapper(): WrapperInterface
68
    {
69
        return $this->wrapper;
70
    }
71
72
    public function hasMethod(string $name): bool
73
    {
74
        return null !== $this->getMethod($name);
75
    }
76
77
    /**
78
     * @param string $name
79
     *
80
     * @return FunctionObject|null
81
     */
82
    public function getMethod(string $name)
83
    {
84
        if (!isset($this->wrapped_methods[$name])) {
85
            return null;
86
        }
87
88
        if (!$this->wrapped_methods[$name]->getReference()->valid()) {
89
            // UNEXPECTED
90
            unset($this->wrapped_methods[$name]);
91
92
            return null;
93
        }
94
95
        /** @var FunctionObject $js_function */
96
        $js_function = $this->wrapped_methods[$name]->getReference()->get();
97
        assert($js_function != null);
98
99
        return $js_function;
100
    }
101
102
    public function storeMethod(string $name, FunctionObject $function, ObjectValue $object)
103
    {
104
        $ref = new WeakReference($function, function () use ($name) {
105
            unset($this->wrapped_methods[$name]);
106
        });
107
108
        $this->wrapped_methods[$name] = new RuntimeMethod($ref, $object);
109
    }
110
}
111