UrlRewriteController::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RuthgerIdema\UrlRewrite\Http;
6
7
use Illuminate\Http\Response;
8
use Illuminate\Support\Facades\Request;
9
use Illuminate\Support\Facades\Route;
10
use RuthgerIdema\UrlRewrite\Repositories\Interfaces\UrlRewriteInterface;
11
12
class UrlRewriteController
13
{
14
    /** @var UrlRewriteInterface */
15
    protected $repository;
16
17
    public function __construct(
18
        UrlRewriteInterface $repository
19
    ) {
20
        $this->repository = $repository;
21
    }
22
23
    public function __invoke($url): object
24
    {
25
        if (! $urlRewrite = $this->repository->getByRequestPath($url)) {
26
            abort(404);
27
        }
28
29
        if ($urlRewrite->isForward()) {
30
            return $this->forwardResponse($urlRewrite->target_path);
31
        }
32
33
        return redirect($urlRewrite->target_path, $urlRewrite->getRedirectType());
34
    }
35
36
    protected function forwardResponse($url): Response
37
    {
38
        return Route::dispatch(
39
            Request::create(
0 ignored issues
show
Bug introduced by
The method create() does not exist on Illuminate\Support\Facades\Request. Did you maybe mean createFreshMockInstance()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
40
                '/'.ltrim($url, '/'),
41
                request()->getMethod()
42
            )
43
        );
44
    }
45
}
46