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 Doctrine\Common\Annotations\Reader; |
14
|
|
|
use Go\Aop\Intercept\MethodInvocation; |
15
|
|
|
use PhpDeal\Contract\Fetcher\ParentClass\MethodConditionWithInheritDocFetcher; |
16
|
|
|
use PhpDeal\Exception\ContractViolation; |
17
|
|
|
use PhpDeal\Annotation\Verify; |
18
|
|
|
|
19
|
|
|
class PreconditionContract extends Contract |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var MethodConditionWithInheritDocFetcher |
23
|
|
|
*/ |
24
|
|
|
private $methodConditionFetcher; |
25
|
|
|
|
26
|
|
|
public function __construct(Reader $reader) |
27
|
|
|
{ |
28
|
|
|
parent::__construct($reader); |
29
|
|
|
$this->methodConditionFetcher = new MethodConditionWithInheritDocFetcher(Verify::class); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param MethodInvocation $invocation |
34
|
|
|
* @Before("@execution(PhpDeal\Annotation\Verify)") |
35
|
|
|
* @throws ContractViolation |
36
|
|
|
*/ |
37
|
19 |
|
public function check(MethodInvocation $invocation) |
38
|
|
|
{ |
39
|
19 |
|
$object = $invocation->getThis(); |
40
|
19 |
|
$args = $this->getMethodArguments($invocation); |
41
|
19 |
|
$scope = $invocation->getMethod()->getDeclaringClass()->name; |
42
|
|
|
|
43
|
19 |
|
$allContracts = $this->makeContractsUnique($this->fetchAllContracts($invocation)); |
44
|
19 |
|
$this->fulfillContracts($allContracts, $object, $scope, $args, $invocation); |
45
|
6 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param MethodInvocation $invocation |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
19 |
|
private function fetchAllContracts(MethodInvocation $invocation) |
52
|
|
|
{ |
53
|
19 |
|
$allContracts = $this->fetchParentsContracts($invocation); |
54
|
|
|
|
55
|
19 |
|
foreach ($invocation->getMethod()->getAnnotations() as $annotation) { |
56
|
19 |
|
if ($annotation instanceof Verify) { |
57
|
19 |
|
$allContracts[] = $annotation; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
19 |
|
return $allContracts; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param MethodInvocation $invocation |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
19 |
|
private function fetchParentsContracts(MethodInvocation $invocation) |
69
|
|
|
{ |
70
|
19 |
|
return $this->methodConditionFetcher->getConditions( |
71
|
19 |
|
$invocation->getMethod()->getDeclaringClass(), |
72
|
19 |
|
$this->reader, |
73
|
19 |
|
$invocation->getMethod()->name |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|