HandlesUrlTrait::findUrlByUri()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 10
rs 9.4286
c 1
b 0
f 1
cc 2
eloc 6
nc 2
nop 1
1
<?php 
2
3
namespace Luminark\Url\Traits;
4
5
use Luminark\Url\Models\Url;
6
use Illuminate\Database\Eloquent\ModelNotFoundException;
7
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
8
9
trait HandlesUrlTrait 
10
{
11
    public function getUrlResource($uri)
12
    {
13
        $url = $this->findUrlByUri($uri);
14
        if ($url->redirectsTo) {
15
            while ($url->redirectsTo) {
16
                $url = $url->redirectsTo;
17
            }
18
19
            return redirect($url->url);
20
        }
21
22
        return $this->getUrlResourceResponse($url);
23
    }
24
    
25
    protected function findUrlByUri($uri)
26
    {
27
        try {
28
            $url = Url::findOrFail($uri);
29
        } catch (ModelNotFoundException $e) {
30
            throw new NotFoundHttpException(null, $e);
31
        }
32
        
33
        return $url;
34
    }
35
36
    abstract protected function getUrlResourceResponse(Url $url);
37
}
38