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

AuraRouterController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 19.15 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 9
loc 47
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 1
A actionRoute() 0 25 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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