Completed
Push — master ( a41bbe...18bc86 )
by ARCANEDEV
08:47
created

RefererTracker::trackDomain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
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 implements RefererTrackerContract
17
{
18
    /* ------------------------------------------------------------------------------------------------
19
     |  Properties
20
     | ------------------------------------------------------------------------------------------------
21
     */
22
    /** @var \Arcanedev\LaravelTracker\Contracts\Parsers\RefererParser */
23
    private $refererParser;
24
25
    /* ------------------------------------------------------------------------------------------------
26
     |  Constructor
27
     | ------------------------------------------------------------------------------------------------
28
     */
29
    /**
30
     * RefererTracker constructor.
31
     *
32
     * @param  \Arcanedev\LaravelTracker\Contracts\Parsers\RefererParser  $refererParser
33
     */
34 36
    public function __construct(RefererParser $refererParser)
35
    {
36 36
        $this->refererParser = $refererParser;
37 36
    }
38
39
    /* ------------------------------------------------------------------------------------------------
40
     |  Main Functions
41
     | ------------------------------------------------------------------------------------------------
42
     */
43
    /**
44
     * Track the referer and return the id.
45
     *
46
     * @param  string  $refererUrl
47
     * @param  string  $pageUrl
48
     *
49
     * @return int|null
50
     */
51 12
    public function track($refererUrl, $pageUrl)
52
    {
53 12
        $firstParsed = $this->refererParser->parseUrl($refererUrl);
54
55 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...
56 6
            $domainId   = $this->trackDomain($firstParsed['domain']);
57
            $attributes = [
58 6
                'url'               => $firstParsed['url'],
59 6
                'host'              => $firstParsed['host'],
60 6
                'domain_id'         => $domainId,
61 3
                'medium'            => null,
62 3
                'source'            => null,
63 3
                'search_terms_hash' => null,
64 3
            ];
65
66 6
            $secondParsed = $this->refererParser->parse($firstParsed['url'], $pageUrl);
67
68 6
            if ($secondParsed->isKnown()) {
69 6
                $attributes['medium']            = $secondParsed->getMedium();
70 6
                $attributes['source']            = $secondParsed->getSource();
71 6
                $attributes['search_terms_hash'] = sha1($secondParsed->getSearchTerm());
72 3
            }
73
74 6
            $referer = Referer::firstOrCreate(
75 6
                Arr::only($attributes, ['url', 'search_terms_hash']),
76
                $attributes
77 3
            );
78
79 6
            if ($secondParsed->isKnown()) {
80 6
                $this->trackRefererSearchTerms($referer->id, $secondParsed->getSearchTerm());
81 3
            }
82
83 6
            return $referer->id;
84
        }
85
86 6
        return null;
87
    }
88
89
    /**
90
     * Track the domain and get the id.
91
     *
92
     * @param  string  $name
93
     *
94
     * @return int
95
     */
96 6
    private function trackDomain($name)
97
    {
98 6
        $data = compact('name');
99
100 6
        return Domain::firstOrCreate($data, $data)->id;
101
    }
102
103
    /**
104
     * Store the referer's search terms.
105
     *
106
     * @param  int     $refererId
107
     * @param  string  $searchTerms
108
     */
109 6
    private function trackRefererSearchTerms($refererId, $searchTerms)
110
    {
111 6
        foreach (explode(' ', $searchTerms) as $term) {
112
            $attributes = [
113 6
                'referer_id'  => $refererId,
114 6
                'search_term' => $term,
115 3
            ];
116
117 6
            RefererSearchTerm::firstOrCreate($attributes, $attributes);
118 3
        }
119 6
    }
120
}
121