|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Coyote\Services\Skills; |
|
4
|
|
|
|
|
5
|
|
|
use Coyote\Repositories\Contracts\GuestRepositoryInterface as GuestRepository; |
|
6
|
|
|
use Coyote\Repositories\Contracts\TagRepositoryInterface as TagRepository; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
use Coyote\Repositories\Contracts\PageRepositoryInterface as PageRepository; |
|
9
|
|
|
|
|
10
|
|
|
class Predictions |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var Request |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $request; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var PageRepository |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $page; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var GuestRepository |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $guest; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var TagRepository |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $tag; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param Request $request |
|
34
|
|
|
* @param PageRepository $page |
|
35
|
|
|
* @param GuestRepository $guest |
|
36
|
|
|
* @param TagRepository $tag |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(Request $request, PageRepository $page, GuestRepository $guest, TagRepository $tag) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->request = $request; |
|
41
|
|
|
$this->page = $page; |
|
42
|
|
|
$this->guest = $guest; |
|
43
|
|
|
$this->tag = $tag; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return array |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getTags() |
|
50
|
|
|
{ |
|
51
|
|
|
if (!($tags = $this->getRefererTags())) { |
|
|
|
|
|
|
52
|
|
|
$tags = $this->getUserPopularTags(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $tags; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return \Coyote\Tag[]|null |
|
60
|
|
|
*/ |
|
61
|
|
|
private function getRefererTags() |
|
62
|
|
|
{ |
|
63
|
|
|
$referer = filter_var($this->request->headers->get('referer'), FILTER_SANITIZE_URL); |
|
64
|
|
|
if (!$referer) { |
|
65
|
|
|
return null; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$path = parse_url(urldecode($referer), PHP_URL_PATH); |
|
69
|
|
|
if (!$path) { |
|
70
|
|
|
return null; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$page = $this->page->findByPath($path); |
|
74
|
|
|
|
|
75
|
|
|
if (!$page || !$page->tags) { |
|
|
|
|
|
|
76
|
|
|
return null; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $this->tag->getCategorizedTags($page->tags) ?? null; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return \Coyote\Tag[]|null |
|
84
|
|
|
*/ |
|
85
|
|
|
private function getUserPopularTags() |
|
86
|
|
|
{ |
|
87
|
|
|
/** @var \Coyote\Guest $guest */ |
|
88
|
|
|
$guest = $this->guest->find($this->request->session()->get('guest_id')); |
|
89
|
|
|
|
|
90
|
|
|
if (empty($guest) || empty($guest->interests)) { |
|
91
|
|
|
return null; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$ratio = $guest->interests['ratio']; |
|
95
|
|
|
arsort($ratio); |
|
96
|
|
|
|
|
97
|
|
|
// get only five top tags |
|
98
|
|
|
$result = $this->tag->getCategorizedTags(array_slice(array_keys($ratio), 0, 4)); |
|
99
|
|
|
|
|
100
|
|
|
if (!$result) { |
|
|
|
|
|
|
101
|
|
|
return null; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// only one tag please... |
|
105
|
|
|
return collect()->push($result->random()); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.