Completed
Push — 1.x ( 54a263...51200d )
by Akihito
04:04
created

AuraInputInterceptor::getFormProperty()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0987

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 9
cp 0.7778
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 2
crap 3.0987
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 5
    public function __construct(Reader $reader, FailureHandlerInterface $handler)
34
    {
35 5
        $this->reader = $reader;
36 5
        $this->failureHandler = $handler;
37 5
    }
38
39
    /**
40
     * {@inheritdoc}
41
     *
42
     * @throws InvalidArgumentException
43
     */
44 4
    public function invoke(MethodInvocation $invocation)
45
    {
46 4
        $object = $invocation->getThis();
47
        /* @var $formValidation FormValidation */
48 4
        $formValidation = $this->reader->getMethodAnnotation($invocation->getMethod(), AbstractValidation::class);
49 4
        $form = $this->getFormProperty($formValidation, $object);
50 4
        $data = $form instanceof SubmitInterface ? $form->submit() : $this->getNamedArguments($invocation);
51 4
        $isValid = $this->isValid($data, $form);
52 4
        if ($isValid === true) {
53
            // validation   success
54
            return $invocation->proceed();
55
        }
56
57 4
        return $this->failureHandler->handle($formValidation, $invocation, $form);
58
    }
59
60
    /**
61
     * @param array        $submit
62
     * @param AbstractForm $form
63
     *
64
     * @throws Exception\CsrfViolationException
65
     *
66
     * @return bool
67
     */
68 4
    public function isValid(array $submit, AbstractForm $form)
69
    {
70 4
        $isValid = $form->apply($submit);
71
72 4
        return $isValid;
73
    }
74
75
    /**
76
     * Return arguments as named arguments.
77
     *
78
     * @param MethodInvocation $invocation
79
     *
80
     * @return array
81
     */
82 4
    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...
83
    {
84 4
        $submit = [];
85 4
        $params = $invocation->getMethod()->getParameters();
86 4
        $args = $invocation->getArguments()->getArrayCopy();
87 4
        foreach ($params as $param) {
88 4
            $arg = array_shift($args);
89 4
            $submit[$param->getName()] = $arg;
90
        }
91
        // has token ?
92 4
        if (isset($_POST[AntiCsrf::TOKEN_KEY])) {
93
            $submit[AntiCsrf::TOKEN_KEY] = $_POST[AntiCsrf::TOKEN_KEY];
94
        }
95
96 4
        return $submit;
97
    }
98
99
    /**
100
     * Return form property
101
     *
102
     * @param AbstractValidation $formValidation
103
     * @param object             $object
104
     *
105
     * @return mixed
106
     */
107 4
    private function getFormProperty(AbstractValidation $formValidation, $object)
108
    {
109 4
        if (! property_exists($object, $formValidation->form)) {
110
            throw new InvalidFormPropertyException($formValidation->form);
111
        }
112 4
        $prop = (new \ReflectionClass($object))->getProperty($formValidation->form);
113 4
        $prop->setAccessible(true);
114 4
        $form = $prop->getValue($object);
115 4
        if (! $form instanceof AbstractForm) {
116
            throw new InvalidFormPropertyException($formValidation->form);
117
        }
118
119 4
        return $form;
120
    }
121
}
122