Completed
Push — master ( 56c4be...a1e3bd )
by Alexander
7s
created

PreconditionCheckerAspect   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 80.95%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 6
c 4
b 0
f 0
lcom 1
cbo 3
dl 0
loc 60
ccs 17
cts 21
cp 0.8095
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A preConditionContract() 0 9 1
A fetchAllContracts() 0 12 3
A fetchParentsContracts() 0 7 1
1
<?php
2
/**
3
 * PHP Deal framework
4
 *
5
 * @copyright Copyright 2014, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace PhpDeal\Aspect;
12
13
use Doctrine\Common\Annotations\Reader;
14
use Go\Aop\Aspect;
15
use Go\Aop\Intercept\MethodInvocation;
16
use PhpDeal\Annotation\Verify;
17
use PhpDeal\Contract\Fetcher\ParentClass\MethodConditionWithInheritDocFetcher;
18
use PhpDeal\Exception\ContractViolation;
19
use Go\Lang\Annotation\Before;
20
21
class PreconditionCheckerAspect extends AbstractContractAspect implements Aspect
22
{
23
    /**
24
     * @var MethodConditionWithInheritDocFetcher
25
     */
26
    private $methodConditionFetcher;
27
28
    public function __construct(Reader $reader)
29
    {
30
        parent::__construct($reader);
31
        $this->methodConditionFetcher = new MethodConditionWithInheritDocFetcher(Verify::class, $reader);
32
    }
33
34
    /**
35
     * Verifies pre-condition contract for the method
36
     *
37
     * @param MethodInvocation $invocation
38
     * @Before("@execution(PhpDeal\Annotation\Verify)")
39
     *
40
     * @throws ContractViolation
41
     */
42 19
    public function preConditionContract(MethodInvocation $invocation)
43
    {
44 19
        $object = $invocation->getThis();
45 19
        $args   = $this->fetchMethodArguments($invocation);
46 19
        $scope  = $invocation->getMethod()->getDeclaringClass()->name;
47
48 19
        $allContracts = $this->fetchAllContracts($invocation);
49 19
        $this->ensureContracts($invocation, $allContracts, $object, $scope, $args);
50 6
    }
51
52
    /**
53
     * @param MethodInvocation $invocation
54
     * @return array
55
     */
56 19
    private function fetchAllContracts(MethodInvocation $invocation)
57
    {
58 19
        $allContracts = $this->fetchParentsContracts($invocation);
59
60 19
        foreach ($invocation->getMethod()->getAnnotations() as $annotation) {
61 19
            if ($annotation instanceof Verify) {
62 19
                $allContracts[] = $annotation;
63
            }
64
        }
65
66 19
        return array_unique($allContracts);
67
    }
68
69
    /**
70
     * @param MethodInvocation $invocation
71
     * @return array
72
     */
73 19
    private function fetchParentsContracts(MethodInvocation $invocation)
74
    {
75 19
        return $this->methodConditionFetcher->getConditions(
76 19
            $invocation->getMethod()->getDeclaringClass(),
77 19
            $invocation->getMethod()->name
78
        );
79
    }
80
}
81