Completed
Push — master ( 89b65c...bde278 )
by Alexander
02:30
created

PreconditionContract::fetchAllContracts()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 3
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\Contract;
12
13
use Go\Aop\Intercept\MethodInvocation;
14
use PhpDeal\Contract\Fetcher\ParentClass\MethodConditionWithInheritDocFetcher;
15
use PhpDeal\Exception\ContractViolation;
16
use PhpDeal\Annotation\Verify;
17
18
class PreconditionContract extends Contract
19
{
20
    /**
21
     * @param MethodInvocation $invocation
22
     * @Before("@execution(PhpDeal\Annotation\Verify)")
23
     * @throws ContractViolation
24
     */
25 19
    public function check(MethodInvocation $invocation)
26
    {
27 19
        $object = $invocation->getThis();
28 19
        $args   = $this->getMethodArguments($invocation);
29 19
        $scope  = $invocation->getMethod()->getDeclaringClass()->name;
30
31 19
        $allContracts = $this->makeContractsUnique($this->fetchAllContracts($invocation));
32 19
        $this->fulfillContracts($allContracts, $object, $scope, $args, $invocation);
33 6
    }
34
35
    /**
36
     * @param MethodInvocation $invocation
37
     * @return array
38
     */
39 19
    private function fetchAllContracts(MethodInvocation $invocation)
40
    {
41 19
        $allContracts = $this->fetchParentsContracts($invocation);
42
43 19
        foreach ($invocation->getMethod()->getAnnotations() as $annotation) {
44 19
            if ($annotation instanceof Verify) {
45 19
                $allContracts[] = $annotation;
46
            }
47
        }
48
49 19
        return $allContracts;
50
    }
51
52
    /**
53
     * @param MethodInvocation $invocation
54
     * @return array
55
     */
56 19
    private function fetchParentsContracts(MethodInvocation $invocation)
57
    {
58 19
        return (new MethodConditionWithInheritDocFetcher(Verify::class))->getConditions(
59 19
            $invocation->getMethod()->getDeclaringClass(),
60 19
            $this->reader,
61 19
            $invocation->getMethod()->name
62
        );
63
    }
64
}
65