| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace Shoot\Shoot\Middleware; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Psr\Container\ContainerExceptionInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Psr\Container\ContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Shoot\Shoot\Context; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Shoot\Shoot\HasPresenterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Shoot\Shoot\MiddlewareInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Shoot\Shoot\PresenterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Shoot\Shoot\View; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Resolves presenters as defined by presentation models using a PSR-11 compliant DI container. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | final class PresenterMiddleware implements MiddlewareInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** @var ContainerInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param ContainerInterface $container A PSR-11 compliant DI container holding the presenters. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 5 |  |     public function __construct(ContainerInterface $container) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 5 |  |         $this->container = $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 5 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param View     $view    The view to be processed by this middleware. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param Context  $context The context in which to process the view. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param callable $next    The next middleware to call | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @throws ContainerExceptionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @return View The processed view. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 4 |  |     public function process(View $view, Context $context, callable $next): View | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 4 |  |         $presentationModel = $view->getPresentationModel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 4 |  |         if ($presentationModel instanceof HasPresenterInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 4 |  |             $presenter = $this->loadPresenter($presentationModel); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 4 |  |             $presentationModel = $presenter->present($context, $presentationModel); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 4 |  |             $view = $view->withPresentationModel($presentationModel); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 4 |  |         return $next($view); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * Load the presenter from the container. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param HasPresenterInterface $hasPresenter A presentation model which has a presenter defined. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @throws ContainerExceptionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @return PresenterInterface The presenter as returned by the container. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 4 |  |     private function loadPresenter(HasPresenterInterface $hasPresenter): PresenterInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 4 |  |         return $this->container->get($hasPresenter->getPresenter()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  |