|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Devpri\Tinre\Jobs; |
|
4
|
|
|
|
|
5
|
|
|
use DeviceDetector\DeviceDetector; |
|
6
|
|
|
use Devpri\Tinre\Models\Click; |
|
7
|
|
|
use Devpri\Tinre\Models\Url; |
|
8
|
|
|
use Illuminate\Bus\Queueable; |
|
9
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
|
10
|
|
|
use Illuminate\Foundation\Bus\Dispatchable; |
|
11
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
|
12
|
|
|
use Illuminate\Queue\SerializesModels; |
|
13
|
|
|
|
|
14
|
|
|
class ProcessClick implements ShouldQueue |
|
15
|
|
|
{ |
|
16
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
protected $url; |
|
19
|
|
|
|
|
20
|
|
|
protected $createdAt; |
|
21
|
|
|
|
|
22
|
|
|
protected $ip; |
|
23
|
|
|
|
|
24
|
|
|
protected $userAgent; |
|
25
|
|
|
|
|
26
|
|
|
protected $referer; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Create a new job instance. |
|
30
|
|
|
* |
|
31
|
|
|
* @return void |
|
32
|
|
|
*/ |
|
33
|
3 |
|
public function __construct(Url $url, $createdAt, $ip, $userAgent, $referer) |
|
34
|
|
|
{ |
|
35
|
3 |
|
$this->url = $url; |
|
36
|
3 |
|
$this->createdAt = $createdAt; |
|
37
|
3 |
|
$this->ip = $ip; |
|
38
|
3 |
|
$this->userAgent = $userAgent; |
|
39
|
3 |
|
$this->referer = $referer; |
|
40
|
3 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Execute the job. |
|
44
|
|
|
* |
|
45
|
|
|
* @return void |
|
46
|
|
|
*/ |
|
47
|
3 |
|
public function handle() |
|
48
|
|
|
{ |
|
49
|
3 |
|
$device = new DeviceDetector($this->userAgent); |
|
50
|
3 |
|
$device->discardBotInformation(); |
|
51
|
3 |
|
$device->parse(); |
|
52
|
|
|
|
|
53
|
3 |
|
if ($device->isBot()) { |
|
54
|
1 |
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
$osInfo = $device->getOs() ?? null; |
|
58
|
2 |
|
$client = $device->getClient() ?? null; |
|
59
|
2 |
|
$location = geoip()->getLocation($this->ip); |
|
60
|
|
|
|
|
61
|
2 |
|
Click::create([ |
|
62
|
2 |
|
'url_id' => $this->url->id, |
|
63
|
2 |
|
'country' => $location->getAttribute('iso_code') ?? null, |
|
64
|
2 |
|
'region' => $location->getAttribute('state_name') ?? null, |
|
65
|
2 |
|
'city' => $location->getAttribute('city') ?? null, |
|
66
|
2 |
|
'device_type' => $device->getDeviceName() ? $device->getDeviceName() : null, |
|
67
|
2 |
|
'device_brand' => $device->getBrandName() ? $device->getBrandName() : null, |
|
68
|
2 |
|
'device_model' => $device->getModel() ? $device->getModel() : null, |
|
69
|
2 |
|
'user_agent' => $this->userAgent, |
|
70
|
2 |
|
'os' => $osInfo['short_name'] ?? null, |
|
71
|
2 |
|
'browser' => $client['name'] ?? null, |
|
72
|
2 |
|
'referer' => $this->referer, |
|
73
|
2 |
|
'referer_host' => parse_url($this->referer, PHP_URL_HOST), |
|
74
|
2 |
|
'created_at' => $this->createdAt, |
|
75
|
|
|
]); |
|
76
|
|
|
|
|
77
|
2 |
|
$this->url->timestamps = false; |
|
78
|
2 |
|
$this->url->increment('total_clicks'); |
|
79
|
2 |
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|