Completed
Push — master ( 06f814...f23c49 )
by Tobias
03:33
created

ClientIdProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Happyr\GoogleAnalyticsBundle\Service;
4
5
use Symfony\Component\HttpFoundation\RequestStack;
6
7
/**
8
 * This service tries to fetch a cookie and return Googles client id. The client id is like a user id.
9
 *
10
 * @author Tobias Nyholm <[email protected]>
11
 */
12
class ClientIdProvider
13
{
14
    const COOKIE_NAME = '_ga';
15
16
    /**
17
     * @var RequestStack requestStack
18
     */
19
    protected $requestStack;
20
21 1
    public function __construct(RequestStack $requestStack)
22
    {
23 1
        $this->requestStack = $requestStack;
24 1
    }
25
26
    /**
27
     * Get client id from cookie... if we can.
28
     *
29
     * @return false|string
30
     */
31 1
    public function getClientId()
32
    {
33 1
        if (false === $clientId = $this->getClientIdFormCookie()) {
34
35
            /*
36
             * We could not find any cookie with a client id. We just have to randomize one
37
             */
38 1
            $clientId = mt_rand(10, 1000).round(microtime(true));
39
        }
40
41 1
        return $clientId;
42
    }
43
44
    /**
45
     * Return the value of a cookie.
46
     *
47
     * @param string $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
48
     *
49
     * @return mixed|false
50
     */
51
    protected function getClientIdFormCookie()
52
    {
53
        if (null === $request = $this->requestStack->getMasterRequest()) {
54
            return false;
55
        }
56
57
        $cookies = $request->cookies;
58
        if (!$cookies->has(self::COOKIE_NAME)) {
59
            return false;
60
        }
61
        $value = $cookies->get(self::COOKIE_NAME);
62
63
        return $this->extractCookie($value);
64
    }
65
66
    /**
67
     * Get the client id from the cookie value.
68
     *
69
     * The contents of the cookie might be "GA1.2.1110480476.1405690517"
70
     * The 3rd section is the user id.
71
     * The 4th section is the session id.
72
     * The client id is the user id + the session id
73
     *
74
     * @param $cookieValue
75
     *
76
     * @link http://stackoverflow.com/a/16107194/1526789
77
     *
78
     * @return string|bool clientId or boolean false
79
     */
80 1
    protected function extractCookie($cookieValue)
81
    {
82 1
        if (!preg_match('|[^\.]+\.[^\.]+\.([^\.]+\.[^\.]+)|si', $cookieValue, $matches)) {
83
            //match not found
84
            return false;
85
        }
86
87 1
        return $matches[1];
88
    }
89
}
90