Completed
Pull Request — master (#2)
by ARCANEDEV
09:30
created

CookieTracker::getModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace Arcanedev\LaravelTracker\Trackers;
2
3
use Arcanedev\LaravelTracker\Contracts\Trackers\CookieTracker as CookieTrackerContract;
4
use Arcanedev\LaravelTracker\Models\AbstractModel;
5
use Ramsey\Uuid\Uuid;
6
7
/**
8
 * Class     CookieTracker
9
 *
10
 * @package  Arcanedev\LaravelTracker\Trackers
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class CookieTracker extends AbstractTracker implements CookieTrackerContract
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Getters and Setters
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     * Get the model.
21
     *
22
     * @return \Arcanedev\LaravelTracker\Models\Cookie
23
     */
24 6
    protected function getModel()
25
    {
26 6
        return $this->makeModel(AbstractModel::MODEL_COOKIE);
27
    }
28
29
    /* ------------------------------------------------------------------------------------------------
30
     |  Main Functions
31
     | ------------------------------------------------------------------------------------------------
32
     */
33
    /**
34
     * Track the cookie.
35
     *
36
     * @param  mixed  $cookie
37
     *
38
     * @return int
39
     */
40 6
    public function track($cookie)
41
    {
42 6
        if ( ! $cookie) {
43
            $this->cookie()->queue(
44
                $this->cookie()->make($this->getConfig('cookie.name'), $cookie = (string) Uuid::uuid4())
45
            );
46
        }
47
48 6
        return $this->getModel()
49 6
                    ->firstOrCreate(['uuid' => $cookie])->id;
50
    }
51
52
    /* ------------------------------------------------------------------------------------------------
53
     |  Other Functions
54
     | ------------------------------------------------------------------------------------------------
55
     */
56
    /**
57
     * Get the cookie instance.
58
     *
59
     * @return \Illuminate\Cookie\CookieJar
60
     */
61
    private function cookie()
62
    {
63
        return $this->make(\Illuminate\Contracts\Cookie\QueueingFactory::class);
64
    }
65
}
66