HandlesUrlTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 4
Bugs 1 Features 4
Metric Value
wmc 5
c 4
b 1
f 4
lcom 0
cbo 1
dl 0
loc 29
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getUrlResource() 0 13 3
A findUrlByUri() 0 10 2
getUrlResourceResponse() 0 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