Referer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 90
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A store() 0 34 3
A storeSearchTerms() 0 14 2
1
<?php
2
3
namespace PragmaRX\Tracker\Data\Repositories;
4
5
use PragmaRX\Tracker\Support\RefererParser;
6
7
class Referer extends Repository
8
{
9
    /**
10
     * @var RefererParser
11
     */
12
    private $refererParser;
13
14
    /**
15
     * @var
16
     */
17
    private $currentUrl;
18
19
    /**
20
     * @var
21
     */
22
    private $searchTermModel;
23
24
    /**
25
     * Create repository instance.
26
     *
27
     * @param RefererParser $refererParser
28
     */
29
    public function __construct($model, $searchTermModel, $currentUrl, RefererParser $refererParser)
30
    {
31
        parent::__construct($model);
32
33
        $this->refererParser = $refererParser;
34
35
        $this->currentUrl = $currentUrl;
36
37
        $this->searchTermModel = $searchTermModel;
38
    }
39
40
    /**
41
     * @param $refererUrl
42
     * @param $host
43
     * @param $domain_id
44
     *
45
     * @return mixed
46
     */
47
    public function store($refererUrl, $host, $domain_id)
48
    {
49
        $attributes = [
50
            'url'               => $refererUrl,
51
            'host'              => $host,
52
            'domain_id'         => $domain_id,
53
            'medium'            => null,
54
            'source'            => null,
55
            'search_terms_hash' => null,
56
        ];
57
58
        $parsed = $this->refererParser->parse($refererUrl, $this->currentUrl);
59
60
        if ($parsed->isKnown()) {
61
            $attributes['medium'] = $parsed->getMedium();
62
63
            $attributes['source'] = $parsed->getSource();
64
65
            $attributes['search_terms_hash'] = sha1($parsed->getSearchTerm());
66
        }
67
68
        $referer = $this->findOrCreate(
69
            $attributes,
70
            ['url', 'search_terms_hash']
71
        );
72
73
        $referer = $this->find($referer);
74
75
        if ($parsed->isKnown()) {
76
            $this->storeSearchTerms($referer, $parsed);
77
        }
78
79
        return $referer->id;
80
    }
81
82
    private function storeSearchTerms($referer, $parsed)
83
    {
84
        foreach (explode(' ', $parsed->getSearchTerm()) as $term) {
85
            $this->findOrCreate(
86
                [
87
                    'referer_id'  => $referer->id,
88
                    'search_term' => $term,
89
                ],
90
                ['referer_id', 'search_term'],
91
                $created,
92
                $this->searchTermModel
93
            );
94
        }
95
    }
96
}
97