| Total Complexity | 10 |
| Total Lines | 40 |
| Duplicated Lines | 0 % |
| Coverage | 0% |
| Changes | 0 | ||
| 1 | <?php |
||
| 14 | trait AttachToRequestTrait |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @param ServerRequestInterface $request |
||
| 18 | * @return static |
||
| 19 | */ |
||
| 20 | public static function fromRequest(ServerRequestInterface $request) |
||
| 21 | { |
||
| 22 | $key = defined('static::ATTR_KEY') ? constant('static::ATTR_KEY') : static::class; |
||
| 23 | if (!$instance = $request->getAttribute($key)) { |
||
| 24 | throw new \RuntimeException('attribute empty:' . $key); |
||
| 25 | } |
||
| 26 | if (!$instance instanceof static) { |
||
| 27 | throw new \RuntimeException('instance error:' . $key); |
||
| 28 | } |
||
| 29 | |||
| 30 | return $instance; |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param ServerRequestInterface $request |
||
| 35 | * @return ServerRequestInterface |
||
| 36 | */ |
||
| 37 | protected function attachToRequest(ServerRequestInterface $request = null): ServerRequestInterface |
||
| 54 | } |
||
| 55 | } |
||
| 56 |