1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Kami\Component\RequestProcessor; |
5
|
|
|
|
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class RequestProcessor |
10
|
|
|
* |
11
|
|
|
* @package Kami\Component\RequestProcessor |
12
|
|
|
*/ |
13
|
|
|
class RequestProcessor implements RequestProcessorInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var ArtifactCollection |
17
|
|
|
*/ |
18
|
|
|
protected $artifacts; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* RequestProcessor constructor. |
22
|
|
|
*/ |
23
|
6 |
|
public function __construct() |
24
|
|
|
{ |
25
|
6 |
|
$this->artifacts = new ArtifactCollection(); |
26
|
6 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param AbstractStrategy $strategy |
30
|
|
|
* @param Request $request |
31
|
|
|
* @return Response |
32
|
|
|
* |
33
|
|
|
* @throws ProcessingException |
34
|
|
|
*/ |
35
|
2 |
|
public function executeStrategy(AbstractStrategy $strategy, Request $request) : Response |
36
|
|
|
{ |
37
|
2 |
|
while ($step = $strategy->getNextStep()) { |
38
|
2 |
|
$step->setArtifacts($this->artifacts->getRequested($step->getRequiredArtifacts())); |
39
|
2 |
|
$artifacts = $step->execute($request); |
40
|
|
|
|
41
|
2 |
|
if ($artifacts instanceof ArtifactCollection) { |
42
|
2 |
|
foreach ($artifacts as $artifact) { |
43
|
2 |
|
$this->addArtifact($artifact); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
return new Response( |
49
|
2 |
|
$this->getArtifact('data')->getValue(), |
50
|
2 |
|
$this->getArtifact('status')->getValue() |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param Artifact $artifact |
57
|
|
|
* |
58
|
|
|
* @return RequestProcessorInterface |
59
|
|
|
*/ |
60
|
5 |
|
public function addArtifact(Artifact $artifact): RequestProcessorInterface |
61
|
|
|
{ |
62
|
5 |
|
$this->artifacts->add($artifact); |
63
|
|
|
|
64
|
5 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $name |
69
|
|
|
* @throws ProcessingException |
70
|
|
|
* |
71
|
|
|
* @return Artifact |
72
|
|
|
*/ |
73
|
4 |
|
public function getArtifact(string $name) : Artifact |
74
|
|
|
{ |
75
|
4 |
|
$artifact = $this->artifacts->get($name); |
76
|
4 |
|
if (!$artifact instanceof Artifact) { |
77
|
2 |
|
throw new ProcessingException(sprintf('You don\'t have "%s" artifact yet', $name)); |
78
|
|
|
} |
79
|
|
|
|
80
|
3 |
|
return $artifact; |
81
|
|
|
} |
82
|
|
|
} |