Passed
Push — master ( 753e62...f14647 )
by Adam
11:15
created

Predictions::skills()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 1
b 0
f 0
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) {
0 ignored issues
show
introduced by
$page is of type Coyote\Page, thus it always evaluated to true.
Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->user->skills of type Coyote\Tag[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
105
            return null;
106
        }
107
108
        return $this->tag->categorizedTags($this->user->skills->pluck('name')->toArray()) ?? null;
109
    }
110
}
111