SplashRequestFetcher::canHandle()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
rs 9.2
cc 4
eloc 9
nc 3
nop 2
1
<?php
2
3
namespace Mouf\Mvc\Splash\Services;
4
5
use ReflectionParameter;
6
7
/**
8
 * This class is used to inject parameters into an object witch respect the RequestInterface of the PSR-7.
9
 *
10
 * @author David Negrier and Benoit Ngo
11
 */
12
class SplashRequestFetcher 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
        $class = $reflectionParameter->getClass();
25
        if ($class === null) {
26
            return false;
27
        }
28
        $name = $class->getName();
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
29
        // Check type of requested parameter; Only interfaces are allowed in an action of a controller.
30
        if ($name === 'Psr\\Http\\Message\\RequestInterface' || $name === 'Psr\\Http\\Message\\ServerRequestInterface') {
31
            return true;
32
        } else {
33
            return false;
34
        }
35
    }
36
37
    /**
38
     * Returns some data needed by this fetcher to fetch data from the request.
39
     * This data MUST be serializable (and will be serialized). This function will be called only once
40
     * and data cached. You can perform expensive computation in this function.
41
     *
42
     * @param ReflectionParameter $reflectionParameter
43
     * @param string|null         $url
44
     *
45
     * @return mixed
46
     */
47
    public function getFetcherData(ReflectionParameter $reflectionParameter, string $url = null)
48
    {
49
        return;
50
    }
51
52
    /**
53
     * Returns the value to be injected in this parameter.
54
     *
55
     * @param mixed                $data    The data generated by "getFetcherData"
56
     * @param SplashRequestContext $context
57
     *
58
     * @return mixed
59
     */
60
    public function fetchValue($data, SplashRequestContext $context)
61
    {
62
        return $context->getRequest();
63
    }
64
}
65