1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helldar\ShortUrl\Services; |
4
|
|
|
|
5
|
|
|
use Helldar\LaravelSupport\Traits\InitModelHelper; |
6
|
|
|
use Helldar\ShortUrl\Models\Short as ShortModel; |
7
|
|
|
use Helldar\ShortUrl\Variables\Model; |
8
|
|
|
use Helldar\Support\Facades\Helpers\Digit; |
9
|
|
|
use Helldar\Support\Facades\Helpers\Http; |
10
|
|
|
use Illuminate\Database\Eloquent\Builder; |
11
|
|
|
use Illuminate\Support\Str; |
12
|
|
|
|
13
|
|
|
class ShortService |
14
|
|
|
{ |
15
|
|
|
use InitModelHelper; |
16
|
|
|
|
17
|
|
|
/** @var int */ |
18
|
|
|
protected $key; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @throws \Helldar\ShortUrl\Exceptions\IncorrectModelKeyException |
22
|
|
|
*/ |
23
|
8 |
|
public function __construct() |
24
|
|
|
{ |
25
|
8 |
|
$this->key = config('short_url.key', 2); |
26
|
|
|
|
27
|
8 |
|
Model::verify($this->key); |
28
|
8 |
|
} |
29
|
|
|
|
30
|
2 |
|
public function get(string $key): string |
31
|
|
|
{ |
32
|
2 |
|
$item = $this->search($key); |
33
|
|
|
|
34
|
1 |
|
return $this->route($item->key); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $url |
39
|
|
|
* |
40
|
|
|
* @throws \Helldar\LaravelSupport\Exceptions\IncorrectModelException |
41
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
6 |
|
public function set(string $url): string |
46
|
|
|
{ |
47
|
6 |
|
$this->validateUrl($url); |
48
|
|
|
|
49
|
|
|
/** @var \Helldar\ShortUrl\Models\Short $item */ |
50
|
5 |
|
$item = $this->builder()->firstOrCreate(compact('url')); |
51
|
|
|
|
52
|
5 |
|
$this->setKey($item); |
53
|
|
|
|
54
|
5 |
|
return $this->route($item->key); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
5 |
|
public function search(string $key): ShortModel |
58
|
|
|
{ |
59
|
|
|
/** @var \Helldar\ShortUrl\Models\Short|\Illuminate\Database\Eloquent\Builder $item */ |
60
|
5 |
|
$item = $this->builder()->where('key', $key)->firstOrFail(); |
61
|
|
|
|
62
|
3 |
|
$item->increment('visited'); |
63
|
|
|
|
64
|
3 |
|
return $item; |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
5 |
|
protected function route(string $key): string |
68
|
|
|
{ |
69
|
5 |
|
$name = config('short_url.route_name', 'short_url'); |
70
|
|
|
|
71
|
5 |
|
return route($name, compact('key')); |
72
|
|
|
} |
73
|
|
|
|
74
|
6 |
|
protected function validateUrl(string $url) |
75
|
|
|
{ |
76
|
6 |
|
Http::validateUrl($url); |
77
|
5 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param \Helldar\ShortUrl\Models\Short $model |
81
|
|
|
* |
82
|
|
|
* @throws \Helldar\LaravelSupport\Exceptions\IncorrectModelException |
83
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
84
|
|
|
* |
85
|
|
|
* @return \Helldar\ShortUrl\Models\Short |
86
|
|
|
*/ |
87
|
5 |
|
protected function setKey(ShortModel $model): ShortModel |
88
|
|
|
{ |
89
|
5 |
|
if (empty($model->key)) { |
90
|
5 |
|
$key = $this->getModelKey($model); |
91
|
|
|
|
92
|
5 |
|
$model->update(compact('key')); |
93
|
|
|
} |
94
|
|
|
|
95
|
5 |
|
return $model; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param \Helldar\ShortUrl\Models\Short $model |
100
|
|
|
* |
101
|
|
|
* @throws \Helldar\LaravelSupport\Exceptions\IncorrectModelException |
102
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
5 |
|
protected function getModelKey(ShortModel $model): string |
107
|
|
|
{ |
108
|
5 |
|
return $this->key === Model::PRIMARY_KEY ? $this->getPrimaryKey($model) : $this->getUniqueKey(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param \Helldar\ShortUrl\Models\Short $model |
113
|
|
|
* |
114
|
|
|
* @throws \Helldar\LaravelSupport\Exceptions\IncorrectModelException |
115
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
5 |
|
protected function getPrimaryKey(ShortModel $model): string |
120
|
|
|
{ |
121
|
5 |
|
$primary = $this->model()->primaryKey($model); |
122
|
|
|
|
123
|
5 |
|
$id = $model->{$primary}; |
124
|
|
|
|
125
|
5 |
|
return Digit::shortKey($id); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
protected function getUniqueKey(): string |
129
|
|
|
{ |
130
|
|
|
return Str::slug(uniqid('', true)); |
131
|
|
|
} |
132
|
|
|
|
133
|
7 |
|
protected function builder(): Builder |
134
|
|
|
{ |
135
|
7 |
|
return ShortModel::query(); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|