UrlRewriteController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 34
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 12 3
A forwardResponse() 0 9 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