SplashRequestParameterFetcher::fetchValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace Mouf\Mvc\Splash\Services;
4
5
use ReflectionParameter;
6
7
/**
8
 * This class fetches the parameter from the request.
9
 *
10
 * @author David Negrier
11
 */
12
class SplashRequestParameterFetcher implements ParameterFetcher
13
{
14
    /**
15
     * Returns whether this fetcher factory can handle the parameter passed in parameter for the url $url.
16
     *
17
     * @param ReflectionParameter $reflectionParameter
18
     * @param string              $url
19
     *
20
     * @return bool
21
     */
22
    public function canHandle(ReflectionParameter $reflectionParameter, string $url = null) : bool
23
    {
24
        // Yes, we can! (always)
25
        return true;
26
    }
27
28
    /**
29
     * Returns some data needed by this fetcher to fetch data from the request.
30
     * This data MUST be serializable (and will be serialized). This function will be called only once
31
     * and data cached. You can perform expensive computation in this function.
32
     *
33
     * @param ReflectionParameter $reflectionParameter
34
     * @param string|null         $url
35
     *
36
     * @return mixed
37
     */
38
    public function getFetcherData(ReflectionParameter $reflectionParameter, string $url = null)
39
    {
40
        if ($reflectionParameter->isDefaultValueAvailable()) {
41
            return [
42
                'key' => $reflectionParameter->getName(),
0 ignored issues
show
Bug introduced by
Consider using $reflectionParameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
43
                'compulsory' => false,
44
                'default' => $reflectionParameter->getDefaultValue(),
45
            ];
46
        } else {
47
            return [
48
                'key' => $reflectionParameter->getName(),
0 ignored issues
show
Bug introduced by
Consider using $reflectionParameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
49
                'compulsory' => true,
50
            ];
51
        }
52
    }
53
54
    /**
55
     * Returns the value to be injected in this parameter.
56
     *
57
     * @param mixed                $data    The data generated by "getFetcherData"
58
     * @param SplashRequestContext $context
59
     *
60
     * @return mixed
61
     */
62
    public function fetchValue($data, SplashRequestContext $context)
63
    {
64
        $key = $data['key'];
65
        $compulsory = $data['compulsory'];
66
        $default = $data['default'] ?? null;
67
68
        return $context->getParameter($key, $compulsory, $default);
69
    }
70
}
71