Passed
Push — master ( a0bb2c...991b9b )
by Adam
19:05 queued 07:26
created

Predictions::refered()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 11
nc 5
nop 0
dl 0
loc 21
rs 9.2222
c 1
b 0
f 0
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $tags also could return the type Illuminate\Support\Collection which is incompatible with the documented return type array.
Loading history...
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) {
0 ignored issues
show
introduced by
$page is of type Coyote\Page, thus it always evaluated to true.
Loading history...
76
            return null;
77
        }
78
79
        $result = $this->tag->categorizedTags($page->tags);
80
81
        return count($result) ? $result : null;
82
    }
83
84
    /**
85
     * @return null|\Illuminate\Support\Collection
86
     */
87
    private function getUserPopularTags()
88
    {
89
        /** @var \Coyote\Guest $guest */
90
        $guest = $this->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
        // get only five top tags
100
        $result = $this->tag->categorizedTags(array_slice(array_keys($ratio), 0, 4));
101
102
        if (!count($result)) {
103
            return null;
104
        }
105
106
        // only one tag please...
107
        return collect()->push($result->random());
108
    }
109
}
110