Completed
Push — master ( 0ddb86...17d161 )
by Dmitry
14:45
created

AuraRouterController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
c 0
b 0
f 0
rs 9.9666
cc 1
nc 1
nop 4
1
<?php
2
3
namespace hiapi\controllers;
4
5
use Aura\Router\RouterContainer;
6
use hiqdev\yii\compat\yii;
7
use yii\base\Module;
8
use yii\web\Controller;
9
use yii\web\Response;
10
11
class AuraRouterController extends Controller
12
{
13
    /**
14
     * @var \yii\web\Response
15
     */
16
    protected $response;
17
    /**
18
     * @var RouterContainer
19
     */
20
    private $aura;
21
22 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
        $id, Module $module,
24
        RouterContainer $aura,
25
        $config = []
26
    ) {
27
        parent::__construct($id, $module, $config);
28
        $this->aura = $aura;
29
        $this->response = yii::getApp()->getResponse();
30
    }
31
32
    public function actionRoute()
33
    {
34
        $request = \GuzzleHttp\Psr7\ServerRequest::fromGlobals();
35
        /** @var \Aura\Router\RouterContainer $router */
36
        $router = $this->aura;
37
38
        $route = $router->getMatcher()->match($request);
39
        if (!$route) {
40
            $response = (new \GuzzleHttp\Psr7\Response())
41
                ->withStatus(404);
42
        } else {
43
            foreach ($route->attributes as $attribute => $value) {
44
                $request = $request->withAttribute($attribute, $value);
45
            }
46
            $handle = $route->handler;
47
            $response = $handle($request);
48
        }
49
50
        $this->response->format = Response::FORMAT_RAW;
51
        $this->response->setHeaders($response->getHeaders());
52
        $this->response->setStatusCode($response->getStatusCode());
53
        $this->response->setBody($response->getBody());
54
55
        return $this->response->send();
56
    }
57
}
58