AuraInputInterceptor   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 97.22%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 0
loc 106
ccs 35
cts 36
cp 0.9722
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A invoke() 0 16 3
A isValid() 0 6 1
A getNamedArguments() 0 16 3
A getFormProperty() 0 14 3
1
<?php
2
/**
3
 * This file is part of the Ray.WebFormModule package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Ray\WebFormModule;
8
9
use Doctrine\Common\Annotations\Reader;
10
use Ray\Aop\MethodInterceptor;
11
use Ray\Aop\MethodInvocation;
12
use Ray\WebFormModule\Annotation\AbstractValidation;
13
use Ray\WebFormModule\Annotation\FormValidation;
14
use Ray\WebFormModule\Exception\InvalidArgumentException;
15
use Ray\WebFormModule\Exception\InvalidFormPropertyException;
16
17
class AuraInputInterceptor implements MethodInterceptor
18
{
19
    /**
20
     * @var Reader
21
     */
22
    protected $reader;
23
24
    /**
25
     * @var FailureHandlerInterface
26
     */
27
    protected $failureHandler;
28
29
    /**
30
     * @param Reader                  $reader
31
     * @param FailureHandlerInterface $handler
32
     */
33 13
    public function __construct(Reader $reader, FailureHandlerInterface $handler)
34
    {
35 13
        $this->reader = $reader;
36 13
        $this->failureHandler = $handler;
37 13
    }
38
39
    /**
40
     * {@inheritdoc}
41
     *
42
     * @throws InvalidArgumentException
43
     */
44 12
    public function invoke(MethodInvocation $invocation)
45
    {
46 12
        $object = $invocation->getThis();
47
        /* @var $formValidation FormValidation */
48 12
        $method = $invocation->getMethod();
49 11
        $formValidation = $this->reader->getMethodAnnotation($method, AbstractValidation::class);
50 11
        $form = $this->getFormProperty($formValidation, $object);
0 ignored issues
show
Documentation introduced by
$formValidation is of type object|null, but the function expects a object<Ray\WebFormModule...ion\AbstractValidation>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
51 7
        $data = $form instanceof SubmitInterface ? $form->submit() : $this->getNamedArguments($invocation);
52 7
        $isValid = $this->isValid($data, $form);
53 7
        if ($isValid === true) {
54
            // validation   success
55 1
            return $invocation->proceed();
56
        }
57
58 6
        return $this->failureHandler->handle($formValidation, $invocation, $form);
0 ignored issues
show
Documentation introduced by
$formValidation is of type object|null, but the function expects a object<Ray\WebFormModule...ion\AbstractValidation>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
    }
60
61
    /**
62
     * @param array        $submit
63
     * @param AbstractForm $form
64
     *
65
     * @throws Exception\CsrfViolationException
66
     *
67
     * @return bool
68
     */
69 7
    public function isValid(array $submit, AbstractForm $form)
70
    {
71 7
        $isValid = $form->apply($submit);
72
73 7
        return $isValid;
74
    }
75
76
    /**
77
     * Return arguments as named arguments.
78
     *
79
     * @param MethodInvocation $invocation
80
     *
81
     * @return array
82
     */
83 7
    private function getNamedArguments(MethodInvocation $invocation)
0 ignored issues
show
Coding Style introduced by
getNamedArguments uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
84
    {
85 7
        $submit = [];
86 7
        $params = $invocation->getMethod()->getParameters();
87 7
        $args = $invocation->getArguments()->getArrayCopy();
88 7
        foreach ($params as $param) {
89 7
            $arg = array_shift($args);
90 7
            $submit[$param->getName()] = $arg;
91
        }
92
        // has token ?
93 7
        if (isset($_POST[AntiCsrf::TOKEN_KEY])) {
94
            $submit[AntiCsrf::TOKEN_KEY] = $_POST[AntiCsrf::TOKEN_KEY];
95
        }
96
97 7
        return $submit;
98
    }
99
100
    /**
101
     * Return form property
102
     *
103
     * @param AbstractValidation $formValidation
104
     * @param object             $object
105
     *
106
     * @return mixed
107
     */
108 11
    private function getFormProperty(AbstractValidation $formValidation, $object)
109
    {
110 11
        if (! property_exists($object, $formValidation->form)) {
111 3
            throw new InvalidFormPropertyException($formValidation->form);
112
        }
113 8
        $prop = (new \ReflectionClass($object))->getProperty($formValidation->form);
114 8
        $prop->setAccessible(true);
115 8
        $form = $prop->getValue($object);
116 8
        if (! $form instanceof AbstractForm) {
117 1
            throw new InvalidFormPropertyException($formValidation->form);
118
        }
119
120 7
        return $form;
121
    }
122
}
123