Shortener::routes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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