Shortener   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getShortUrl() 0 6 1
A getRedirectTo() 0 6 1
A routes() 0 4 1
1
<?php
2
3
namespace DmitryBubyakin\Shortener;
4
5
use Hashids\Hashids;
6
use Illuminate\Support\Facades\Route;
7
8
class Shortener
9
{
10
    /** @var Hashids */
11
    protected $hash;
12
13
    /** @var string */
14
    protected $route;
15
16
    public function __construct(Hashids $hash, string $route)
17
    {
18
        $this->hash = $hash;
19
        $this->route = $route;
20
    }
21
22
    public function getShortUrl(string $url): string
23
    {
24
        $id = ShortUrl::firstOrCreate(compact('url'))->id;
25
26
        return url($this->route, $this->hash->encode($id));
27
    }
28
29
    public function getRedirectTo(string $hash): string
30
    {
31
        [$id] = $this->hash->decode($hash);
0 ignored issues
show
Bug introduced by
The variable $id does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
32
33
        return ShortUrl::findOrFail($id)->url;
34
    }
35
36
    public function routes(): void
37
    {
38
        Route::get("{$this->route}/{hash}", '\DmitryBubyakin\Shortener\Http\Controllers\Redirect');
39
    }
40
}
41