Completed
Push — master ( b785dd...3df4eb )
by Vladimir
03:46
created

JailedDocument   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 82.14%

Importance

Changes 0
Metric Value
dl 0
loc 100
ccs 23
cts 28
cp 0.8214
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A coreInstanceOf() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A __construct() 0 11 3
B __call() 0 22 5
1
<?php
2
3
/**
4
 * @copyright 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Document;
9
10
/**
11
 * Class JailObject.
12
 *
13
 * A wrapper object to only allow certain functions on the white list to be called. This is used in order to limit which
14
 * functions a user can call from Twig templates to prevent unexpected behavior.
15
 */
16
class JailedDocument implements \ArrayAccess
17
{
18
    /**
19
     * @var string[]
20
     */
21
    private $whiteListFunctions;
22
23
    /**
24
     * @var string[]
25
     */
26
    private $jailedFunctions;
27
28
    /**
29
     * @var JailedDocumentInterface
30
     */
31
    private $object;
32
33
    /**
34
     * JailObject constructor.
35
     *
36
     * @param JailedDocumentInterface $object             The object that will be jailed
37
     * @param array                   $whiteListFunctions A list of function names that can be called
38
     * @param array                   $jailedFunctions
39
     */
40 51
    public function __construct(&$object, array $whiteListFunctions, array $jailedFunctions = array())
41
    {
42 51
        if (!($object instanceof JailedDocumentInterface) && !($object instanceof \ArrayAccess))
43 51
        {
44
            throw new \InvalidArgumentException('Must implement the ArrayAccess and Jailable interfaces');
45
        }
46
47 51
        $this->object = &$object;
48 51
        $this->whiteListFunctions = $whiteListFunctions;
49 51
        $this->jailedFunctions = $jailedFunctions;
50 51
    }
51
52 10
    public function __call($name, $arguments)
53
    {
54
        // White listed functions will always be getter functions, so somehow get the name of a possible getter function
55
        // name.
56 10
        $lcName = lcfirst($name);
57 10
        $getFxnCall = ($lcName[0] === 'g' && strpos($lcName, 'get') === 0) ? $lcName : sprintf('get%s', ucfirst($name));
58
59
        // Check if our function call is a jailed call, meaning the function should be mapped to special "jailed"
60
        // jailed version of the function call.
61 10
        if (array_key_exists($getFxnCall, $this->jailedFunctions))
62 10
        {
63 3
            return call_user_func_array(array($this->object, $this->jailedFunctions[$getFxnCall]), $arguments);
64
        }
65
66
        // Otherwise, test to see if the function call is in our white list and call it
67 7
        if (in_array($getFxnCall, $this->whiteListFunctions))
68 7
        {
69 6
            return call_user_func_array(array($this->object, $getFxnCall), $arguments);
70
        }
71
72 1
        throw new \BadMethodCallException();
73
    }
74
75 4
    public function coreInstanceOf($class)
0 ignored issues
show
Coding Style introduced by
function coreInstanceOf() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
76
    {
77 4
        return is_subclass_of($this->object, $class);
78
    }
79
80
    //
81
    // ArrayAccess Implementation
82
    //
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 22
    public function offsetExists($offset)
88
    {
89 22
        return $this->object->offsetExists($offset);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 30
    public function offsetGet($offset)
96
    {
97 30
        return $this->object->offsetGet($offset);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function offsetSet($offset, $value)
104
    {
105
        return $this->object->offsetSet($offset, $value);
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function offsetUnset($offset)
112
    {
113
        return $this->object->offsetUnset($offset);
114
    }
115
}
116