AuraInputInterceptor   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 39
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A invoke() 0 21 3
1
<?php
2
/**
3
 * This file is part of the Ray.ValidateModule package
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
6
 */
7
namespace Ray\Validation;
8
9
use Doctrine\Common\Annotations\Reader;
10
use Ray\Aop\MethodInterceptor;
11
use Ray\Aop\MethodInvocation;
12
use Ray\Validation\Annotation\AuraInput;
13
use Ray\Validation\Exception\InvalidArgumentException;
14
15
class AuraInputInterceptor implements MethodInterceptor
16
{
17
    /**
18
     * @var Reader
19
     */
20
    private $reader;
21
22
    public function __construct(Reader $reader)
23
    {
24
        $this->reader = $reader;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     *
30
     * @throws InvalidArgumentException
31
     */
32
    public function invoke(MethodInvocation $invocation)
33
    {
34
        $params = $invocation->getMethod()->getParameters();
35
        $args = $invocation->getArguments();
36
        $submit = [];
37
        foreach ($params as $param) {
38
            $array = (array) $args;
39
            $submit[$param->getName()] = array_shift($array);
40
        }
41
        $object = $invocation->getThis();
42
        $isValid = $object->isValidForm($submit);
43
        if ($isValid === true) {
44
            // validation success
45
            return $invocation->proceed();
46
        }
47
        /* @var $auraInput AuraInput */
48
        $auraInput = $this->reader->getMethodAnnotation($invocation->getMethod(), AuraInput::class);
49
        $args = (array) $invocation->getArguments();
50
51
        return call_user_func_array([$invocation->getThis(), $auraInput->onFailure], $args);
52
    }
53
}
54