Completed
Push — master ( a4831f...a4416a )
by ARCANEDEV
07:50
created

RefererTracker::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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