ClosureDispatcher::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace JetFire\Routing\Dispatcher;
4
5
use JetFire\Routing\Response;
6
use JetFire\Routing\ResponseInterface;
7
use JetFire\Routing\Route;
8
use JetFire\Routing\RouteCollection;
9
use JetFire\Routing\Router;
10
11
/**
12
 * Class ClosureDispatcher
13
 * @package JetFire\Routing\Dispatcher
14
 */
15
class ClosureDispatcher implements DispatcherInterface
16
{
17
    /**
18
     * @var Router
19
     */
20
    private $router;
21
22
    /**
23
     * @param Router $router
24
     */
25
    public function __construct(Router $router)
26
    {
27
        $this->router = $router;
28
    }
29
30
31
    /**
32
     * @description call anonymous function
33
     */
34
    public function call()
35
    {
36
        $classInstance = [
37
            Response::class => $this->router->response,
38
            Route::class => $this->router->route,
39
            RouteCollection::class => $this->router->collection,
40
        ];
41
42
        $params = empty($this->router->route->getParameters()) ? $classInstance : array_merge($this->router->route->getParameters(), $classInstance);
43
        $content = call_user_func_array($this->router->route->getTarget('closure'), $params);
44 View Code Duplication
        if ($content instanceof ResponseInterface) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
45
            $this->router->response = $content;
46
        } else {
47
            if (is_array($content)) {
48
                $this->router->route->addTarget('data', $content);
49
                $content = json_encode($content);
50
            }
51
            $this->router->callMiddleware('between');
52
            $this->router->response->setContent($content);
53
        }
54
    }
55
56
}
57