Completed
Push — master ( ec2990...7a5f39 )
by ARCANEDEV
07:59
created

RefererTracker::trackRefererSearchTerms()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
crap 2
1
<?php namespace Arcanedev\LaravelTracker\Trackers;
2
3
use Arcanedev\LaravelTracker\Contracts\Parsers\RefererParser;
4
use Arcanedev\LaravelTracker\Contracts\Trackers\RefererTracker as RefererTrackerContract;
5
use Arcanedev\LaravelTracker\Models\Domain;
6
use Arcanedev\LaravelTracker\Models\Referer;
7
use Arcanedev\LaravelTracker\Models\RefererSearchTerm;
8
use Illuminate\Support\Arr;
9
10
/**
11
 * Class     RefererTracker
12
 *
13
 * @package  Arcanedev\LaravelTracker\Trackers
14
 * @author   ARCANEDEV <[email protected]>
15
 */
16
class RefererTracker extends AbstractTracker implements RefererTrackerContract
17
{
18
    /* ------------------------------------------------------------------------------------------------
19
     |  Getters & Setters
20
     | ------------------------------------------------------------------------------------------------
21
     */
22
    /**
23
     * @return \Arcanedev\LaravelTracker\Contracts\Parsers\RefererParser
24
     */
25 12
    private function getRefererParser()
26
    {
27 12
        return $this->make(RefererParser::class);
28
    }
29
30
    /* ------------------------------------------------------------------------------------------------
31
     |  Main Functions
32
     | ------------------------------------------------------------------------------------------------
33
     */
34
    /**
35
     * Track the referer and return the id.
36
     *
37
     * @param  string  $refererUrl
38
     * @param  string  $pageUrl
39
     *
40
     * @return int|null
41
     */
42 12
    public function track($refererUrl, $pageUrl)
43
    {
44 12
        $firstParsed = $this->getRefererParser()->parseUrl($refererUrl);
45
46 12
        if ($firstParsed) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $firstParsed of type array 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...
47 6
            $domainId   = $this->trackDomain($firstParsed['domain']);
48
            $attributes = [
49 6
                'url'               => $firstParsed['url'],
50 6
                'host'              => $firstParsed['host'],
51 6
                'domain_id'         => $domainId,
52 3
                'medium'            => null,
53 3
                'source'            => null,
54 3
                'search_terms_hash' => null,
55 3
            ];
56
57 6
            $secondParsed = $this->getRefererParser()->parse($firstParsed['url'], $pageUrl);
58
59 6
            if ($secondParsed->isKnown()) {
60 6
                $attributes['medium']            = $secondParsed->getMedium();
61 6
                $attributes['source']            = $secondParsed->getSource();
62 6
                $attributes['search_terms_hash'] = sha1($secondParsed->getSearchTerm());
63 3
            }
64
65 6
            $referer = Referer::firstOrCreate(
66 6
                Arr::only($attributes, ['url', 'search_terms_hash']),
67
                $attributes
68 3
            );
69
70 6
            if ($secondParsed->isKnown()) {
71 6
                $this->trackRefererSearchTerms($referer->id, $secondParsed->getSearchTerm());
72 3
            }
73
74 6
            return $referer->id;
75
        }
76
77 6
        return null;
78
    }
79
80
    /**
81
     * Track the domain and get the id.
82
     *
83
     * @param  string  $name
84
     *
85
     * @return int
86
     */
87 6
    private function trackDomain($name)
88
    {
89 6
        $data = compact('name');
90
91 6
        return Domain::firstOrCreate($data, $data)->id;
92
    }
93
94
    /**
95
     * Store the referer's search terms.
96
     *
97
     * @param  int     $refererId
98
     * @param  string  $searchTerms
99
     */
100 6
    private function trackRefererSearchTerms($refererId, $searchTerms)
101
    {
102 6
        foreach (explode(' ', $searchTerms) as $term) {
103
            $attributes = [
104 6
                'referer_id'  => $refererId,
105 6
                'search_term' => $term,
106 3
            ];
107
108 6
            RefererSearchTerm::firstOrCreate($attributes, $attributes);
109 3
        }
110 6
    }
111
}
112