Test Setup Failed
Pull Request — master (#94)
by Alex
06:41 queued 02:48
created

ResourceFunctionType::getResourceType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace POData\Providers\Metadata;
4
5
use AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\FunctionImportAnonymousType;
6
7
class ResourceFunctionType
8
{
9
    private $blacklist = ['exec', 'system', 'eval'];
10
11
    /**
12
     * @property string
13
     */
14
    private $functionName = null;
15
16
    /**
17
     * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\FunctionImportAnonymousType $baseType
18
     */
19
    private $baseType = null;
20
21
    private $resourceType = null;
22
23
    /**
24
     * ResourceFunctionType constructor.
25
     * @param string|array $functionName
26
     * @param FunctionImportAnonymousType $type
27
     * @param ResourceType $resource
28
     */
29
    public function __construct($functionName, FunctionImportAnonymousType $type, ResourceType $resource)
30
    {
31
        if (null === $functionName) {
32
            $msg = "FunctionName must not be null";
33
            throw new \InvalidArgumentException($msg);
34
        }
35
36
        if (!is_string($functionName) && !is_array($functionName)) {
37
            $msg = "Function name must be string or array";
38
            throw new \InvalidArgumentException($msg);
39
        }
40
41
        $isArray = is_array($functionName);
42
        if ($isArray && 1 == count($functionName)) {
43
            $functionName = $functionName[0];
44
            $isArray = false;
45
        }
46
47
        if ($isArray) {
48
            if (2 < count($functionName)) {
49
                $msg = "FunctionName must have no more than 2 elements";
50
                throw new \InvalidArgumentException($msg);
51
            }
52
            if (0 == count($functionName)) {
53
                $msg = "FunctionName must have 1 or 2 elements";
54
                throw new \InvalidArgumentException($msg);
55
            }
56
57
            if (!is_object($functionName[0]) && !is_string($functionName[0])) {
58
                $msg = "First element of FunctionName must be either object or string";
59
                throw new \InvalidArgumentException($msg);
60
            }
61
            if (!is_string($functionName[1])) {
62
                $msg = "Second element of FunctionName must be string";
63
                throw new \InvalidArgumentException($msg);
64
            }
65
            if (is_string($functionName[0])) {
66
                $functionName[0] = trim($functionName[0]);
67
                $func = $functionName[0];
68
                if ('' == $func) {
69
                    $msg = "First element of FunctionName must not be empty";
70
                    throw new \InvalidArgumentException($msg);
71
                }
72
                $this->checkBlacklist($func, true);
73
            }
74
        } else {
75
            if (!is_string($functionName) || empty(trim($functionName))) {
76
                $msg = "FunctionName must be a non-empty string";
77
                throw new \InvalidArgumentException($msg);
78
            }
79
            $functionName = trim($functionName);
80
81
            $this->checkBlacklist($functionName, false);
82
        }
83
84
        if (!$type->isOK($msg)) {
85
            throw new \InvalidArgumentException($msg);
86
        }
87
88
        $this->functionName = $functionName;
89
        $this->baseType = $type;
90
        $this->resourceType = $resource;
91
    }
92
93
    /**
94
     * Get endpoint name
95
     *
96
     * @return string
97
     */
98
    public function getName()
99
    {
100
        return $this->baseType->getName();
101
    }
102
103
    /**
104
     * Get underlying function name
105
     *
106
     * @return string
107
     */
108
    public function getFunctionName()
109
    {
110
        return $this->functionName;
111
    }
112
113
    /**
114
     * Required parameter list
115
     *
116
     * @return array
117
     */
118
    public function getParms()
119
    {
120
        return $this->baseType->getParameter();
121
    }
122
123
    /**
124
     * @return ResourceType
125
     */
126
    public function getResourceType()
127
    {
128
        return $this->resourceType;
129
    }
130
131
    public function get(array $parms = [])
132
    {
133
        // check inputs
134
        $baseParms = $this->getParms();
135
        $expectedParms = count($baseParms);
136
        $actualParms = count($parms);
137
        if ($expectedParms != $actualParms) {
138
            $msg = "Was expecting ". $expectedParms. " arguments, received ".$actualParms." instead";
139
            throw new \InvalidArgumentException($msg);
140
        }
141
142
        // commence primary ignition
143
        return call_user_func_array($this->functionName, $parms);
144
    }
145
146
    /**
147
     * @param $func
148
     */
149
    private function checkBlacklist($func, $fromArray = false)
150
    {
151
        if (in_array($func, $this->blacklist) || in_array(strtolower($func), $this->blacklist)) {
152
            $msg = (true === $fromArray ? "First element of " : "")."FunctionName blacklisted";
153
            throw new \InvalidArgumentException($msg);
154
        }
155
    }
156
}
157