Completed
Push — master ( c4d057...a4831f )
by ARCANEDEV
07:52
created

RefererParser::getMedium()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 2
eloc 2
nc 2
nop 0
crap 6
1
<?php namespace Arcanedev\LaravelTracker\Parsers;
2
3
use Arcanedev\LaravelTracker\Contracts\Parsers\RefererParser as RefererParserContract;
4
5
/**
6
 * Class     RefererParser
7
 *
8
 * @package  Arcanedev\LaravelTracker\Parsers
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class RefererParser implements RefererParserContract
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Properties
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /**
18
     * Referer parser instance.
19
     *
20
     * @var \Snowplow\RefererParser\Parser
21
     */
22
    private $parser;
23
24
    /**
25
     * Parsed referer instance.
26
     *
27
     * @var \Snowplow\RefererParser\Referer
28
     */
29
    private $referer;
30
31
    /* ------------------------------------------------------------------------------------------------
32
     |  Constructor
33
     | ------------------------------------------------------------------------------------------------
34
     */
35
    /**
36
     * RefererParser constructor.
37
     *
38
     * @param  \Snowplow\RefererParser\Parser  $parser
39
     */
40 6
    public function __construct($parser)
41
    {
42 6
        $this->parser = $parser;
43 6
    }
44
45
    /* ------------------------------------------------------------------------------------------------
46
     |  Getters & Setters
47
     | ------------------------------------------------------------------------------------------------
48
     */
49
    /**
50
     * Set the referer.
51
     *
52
     * @param  \Snowplow\RefererParser\Referer  $referer
53
     *
54
     * @return self
55
     */
56
    public function setReferer($referer)
57
    {
58
        $this->referer = $referer;
59
60
        return $this;
61
    }
62
63
    /**
64
     * Get the search medium.
65
     *
66
     * @return string|null
67
     */
68
    public function getMedium()
69
    {
70
        return $this->isKnown() ? $this->referer->getMedium() : null;
71
    }
72
73
    /**
74
     * Get the search source.
75
     *
76
     * @return string|null
77
     */
78
    public function getSource()
79
    {
80
        return $this->isKnown() ? $this->referer->getSource() : null;
81
    }
82
83
    /**
84
     * Get the search term.
85
     *
86
     * @return string|null
87
     */
88
    public function getSearchTerm()
89
    {
90
        return $this->isKnown() ? $this->referer->getSearchTerm() : null;
91
    }
92
93
    /**
94
     * Check if the referer is known.
95
     *
96
     * @return bool
97
     */
98
    public function isKnown()
99
    {
100
        return $this->referer->isKnown();
101
    }
102
103
    /* ------------------------------------------------------------------------------------------------
104
     |  Main Functions
105
     | ------------------------------------------------------------------------------------------------
106
     */
107
    /**
108
     * Parse a referer.
109
     *
110
     * @param  string  $refererUrl
111
     * @param  string  $pageUrl
112
     *
113
     * @return self
114
     */
115
    public function parse($refererUrl, $pageUrl)
116
    {
117
        $this->setReferer($this->parser->parse($refererUrl, $pageUrl));
118
119
        return $this;
120
    }
121
122
    /**
123
     * Parse the referer url.
124
     *
125
     * @param  string  $referer
126
     *
127
     * @return array
128
     */
129 6
    public function parseUrl($referer)
130
    {
131 6
        if (is_null($referer)) return null;
132
133
        $parsed = parse_url($referer);
134
        $parts  = explode('.', $parsed['host']);
135
        $domain = array_pop($parts);
136
137
        if (count($parts) > 0)
138
            $domain = array_pop($parts).'.'.$domain;
139
140
        return [
141
            'url'    => $referer,
142
            'domain' => $domain,
143
            'host'   => $parsed['host'],
144
        ];
145
    }
146
}
147