SplashRequestParameterFetcher   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
c 2
b 0
f 0
lcom 0
cbo 1
dl 0
loc 59
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A canHandle() 0 5 1
A getFetcherData() 0 15 2
A fetchValue() 0 8 1
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