| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types = 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace Innmind\Rest\ServerBundle\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Innmind\Rest\ServerBundle\{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     Routing\RouteFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |     Exception\InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Innmind\Rest\Server\{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     Definition\Directory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     Action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Innmind\Http\{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     Message\ServerRequestInterface, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     Message\ResponseInterface, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     Message\Response, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     Message\StatusCode, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     Message\ReasonPhrase, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     Header\HeaderInterface, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     Header\LinkValue, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     Header\Link, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     Header\ParameterInterface, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     Header\HeaderValueInterface, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     Headers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use Innmind\Url\Url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use Innmind\Filesystem\Stream\StringStream; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use Innmind\Immutable\{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     Map, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     Set, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     MapInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | final class CapabilitiesController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     private $directories; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     private $routeFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     private $generator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 2 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         MapInterface $directories, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         RouteFactory $routeFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         UrlGeneratorInterface $generator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         if ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 2 |  |             (string) $directories->keyType() !== 'string' || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 2 |  |             (string) $directories->valueType() !== Directory::class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |             throw new InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |         $this->directories = $directories; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 1 |  |         $this->routeFactory = $routeFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |         $this->generator = $generator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 59 | 1 |  |     public function capabilitiesAction(ServerRequestInterface $request): ResponseInterface | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         $links = $this | 
            
                                                        
            
                                    
            
            
                | 62 | 1 |  |             ->directories | 
            
                                                        
            
                                    
            
            
                | 63 | 1 |  |             ->reduce( | 
            
                                                        
            
                                    
            
            
                | 64 | 1 |  |                 [], | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |                 function(array $carry, string $name, Directory $directory) { | 
            
                                                        
            
                                    
            
            
                | 66 | 1 |  |                     return array_merge( | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |                         $carry, | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |                         $directory | 
            
                                                        
            
                                    
            
            
                | 69 | 1 |  |                             ->flatten() | 
            
                                                        
            
                                    
            
            
                | 70 | 1 |  |                             ->reduce( | 
            
                                                        
            
                                    
            
            
                | 71 | 1 |  |                                 [], | 
            
                                                        
            
                                    
            
            
                | 72 | 1 |  |                                 function(array $carry, string $name) { | 
            
                                                        
            
                                    
            
            
                | 73 | 1 |  |                                     $carry[$name] = $this->generator->generate( | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |                                         $this | 
            
                                                        
            
                                    
            
            
                | 75 | 1 |  |                                             ->routeFactory | 
            
                                                        
            
                                    
            
            
                | 76 | 1 |  |                                             ->makeRoute( | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |                                                 $name, | 
            
                                                        
            
                                    
            
            
                | 78 | 1 |  |                                                 new Action(Action::OPTIONS) | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |                                             ) | 
            
                                                        
            
                                    
            
            
                | 80 | 1 |  |                                             ->getPath() | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |                                     ); | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 83 | 1 |  |                                     return $carry; | 
            
                                                        
            
                                    
            
            
                | 84 | 1 |  |                                 } | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |                             ) | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |                     ); | 
            
                                                        
            
                                    
            
            
                | 87 | 1 |  |                 } | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |             ); | 
            
                                                        
            
                                    
            
            
                | 89 | 1 |  |         $set = new Set(HeaderValueInterface::class); | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 91 | 1 |  |         foreach ($links as $name => $link) { | 
            
                                                        
            
                                    
            
            
                | 92 | 1 |  |             $set = $set->add(new LinkValue( | 
            
                                                        
            
                                    
            
            
                | 93 | 1 |  |                 Url::fromString($link), | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |                 $name, | 
            
                                                        
            
                                    
            
            
                | 95 | 1 |  |                 new Map('string', ParameterInterface::class) | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |             )); | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 99 | 1 |  |         return new Response( | 
            
                                                        
            
                                    
            
            
                | 100 | 1 |  |             $code = new StatusCode(StatusCode::codes()->get('OK')), | 
            
                                                        
            
                                    
            
            
                | 101 | 1 |  |             new ReasonPhrase(ReasonPhrase::defaults()->get($code->value())), | 
            
                                                        
            
                                    
            
            
                | 102 | 1 |  |             $request->protocolVersion(), | 
            
                                                        
            
                                    
            
            
                | 103 | 1 |  |             new Headers( | 
            
                                                        
            
                                    
            
            
                | 104 | 1 |  |                 (new Map('string', HeaderInterface::class)) | 
            
                                                        
            
                                    
            
            
                | 105 | 1 |  |                     ->put( | 
            
                                                        
            
                                    
            
            
                | 106 | 1 |  |                         'Link', | 
            
                                                        
            
                                    
            
            
                | 107 | 1 |  |                         new Link($set) | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |                     ) | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |             ), | 
            
                                                        
            
                                    
            
            
                | 110 | 1 |  |             new StringStream('') | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |         ); | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 113 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |  |