| 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 |  |  |                 $artifacts->forAll(function($key, Artifact $artifact) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 2 |  |                     if ($this->artifacts->containsKey($key)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |                         throw new ProcessingException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |                             sprintf('Collection already contains artifact with key %s', $key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 2 |  |                     $this->addArtifact($artifact); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 2 |  |                 }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |         return new Response( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |             $this->getArtifact('data')->getValue(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 1 |  |             $this->getArtifact('status')->getValue() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param Artifact $artifact | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return RequestProcessorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 5 |  |     public function addArtifact(Artifact $artifact): RequestProcessorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 5 |  |         $this->artifacts->add($artifact); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 5 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @throws ProcessingException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @return Artifact | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 3 |  |     public function getArtifact(string $name) : Artifact | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 3 |  |         $artifact = $this->artifacts->get($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 3 |  |         if (!$artifact instanceof Artifact) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |             throw new ProcessingException(sprintf('You don\'t have "%s" artifact yet', $name)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 2 |  |         return $artifact; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 87 |  |  | } |