|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Coyote\Services\Skills; |
|
4
|
|
|
|
|
5
|
|
|
use Coyote\Guest; |
|
6
|
|
|
use Coyote\Repositories\Contracts\GuestRepositoryInterface as GuestRepository; |
|
7
|
|
|
use Coyote\Repositories\Contracts\TagRepositoryInterface as TagRepository; |
|
8
|
|
|
use Coyote\User; |
|
9
|
|
|
use Illuminate\Http\Request; |
|
10
|
|
|
use Coyote\Repositories\Contracts\PageRepositoryInterface as PageRepository; |
|
11
|
|
|
|
|
12
|
|
|
class Predictions |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Request |
|
16
|
|
|
*/ |
|
17
|
|
|
protected Request $request; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var PageRepository |
|
21
|
|
|
*/ |
|
22
|
|
|
protected PageRepository $page; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var TagRepository |
|
26
|
|
|
*/ |
|
27
|
|
|
protected TagRepository $tag; |
|
28
|
|
|
|
|
29
|
|
|
protected ?User $user; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param Request $request |
|
33
|
|
|
* @param PageRepository $page |
|
34
|
|
|
* @param TagRepository $tag |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(Request $request, PageRepository $page, TagRepository $tag) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->request = $request; |
|
39
|
|
|
$this->page = $page; |
|
40
|
|
|
$this->tag = $tag; |
|
41
|
|
|
$this->user = $this->request->user(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return array|null |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getTags() |
|
48
|
|
|
{ |
|
49
|
|
|
if ($tags = $this->refered()) { |
|
50
|
|
|
return $tags; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if ($tags = $this->skills()) { |
|
54
|
|
|
return $tags; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $this->popular(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return \Coyote\Tag[]|null |
|
62
|
|
|
*/ |
|
63
|
|
|
private function refered() |
|
64
|
|
|
{ |
|
65
|
|
|
$referer = filter_var($this->request->headers->get('referer'), FILTER_SANITIZE_URL); |
|
66
|
|
|
if (!$referer) { |
|
67
|
|
|
return null; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$path = parse_url(urldecode($referer), PHP_URL_PATH); |
|
71
|
|
|
if (!$path) { |
|
72
|
|
|
return null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$page = $this->page->findByPath($path); |
|
76
|
|
|
|
|
77
|
|
|
if (!$page || !$page->tags) { |
|
|
|
|
|
|
78
|
|
|
return null; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $this->tag->categorizedTags($page->tags) ?? null; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return \Coyote\Tag[]|null |
|
86
|
|
|
*/ |
|
87
|
|
|
private function popular() |
|
88
|
|
|
{ |
|
89
|
|
|
/** @var \Coyote\Guest $guest */ |
|
90
|
|
|
$guest = Guest::find($this->request->session()->get('guest_id')); |
|
91
|
|
|
|
|
92
|
|
|
if (empty($guest) || empty($guest->interests)) { |
|
93
|
|
|
return null; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$ratio = $guest->interests['ratio']; |
|
97
|
|
|
arsort($ratio); |
|
98
|
|
|
|
|
99
|
|
|
return $this->tag->categorizedTags(array_slice(array_keys($ratio), 0, 4)) ?? null; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function skills() |
|
103
|
|
|
{ |
|
104
|
|
|
if (empty($this->user) || !$this->user->skills) { |
|
|
|
|
|
|
105
|
|
|
return null; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $this->tag->categorizedTags($this->user->skills->pluck('name')->toArray()) ?? null; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|