Visitor::uniqueIdExists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * Visitor.php
5
 *
6
 * The visitor model represents data from one single unique visitor to your site.
7
 * It has a hasMany relationship with the VisitorRequest model that stores all
8
 * requests made by this unique visitor.
9
 *
10
 * PHP version 7.2
11
 *
12
 * @category Models
13
 * @package  RedboxTracker
14
 * @author   Johnny Mast <[email protected]>
15
 * @license  https://opensource.org/licenses/MIT MIT
16
 * @link     https://github.com/johnnymast/redbox-tracker
17
 * @since    GIT:1.0
18
 */
19
20
namespace Redbox\Tracker;
21
22
use Illuminate\Database\Eloquent\Relations\HasMany;
23
use Illuminate\Database\Eloquent\Model;
24
25
/**
26
 * Visitor class
27
 *
28
 * Model for website visitors.
29
 *
30
 * PHP version 7.2
31
 *
32
 * @category Models
33
 * @package  RedboxTracker
34
 * @author   Johnny Mast <[email protected]>
35
 * @license  https://opensource.org/licenses/MIT MIT
36
 * @link     https://github.com/johnnymast/redbox-tracker
37
 * @since    GIT:1.0
38
 */
39
class Visitor extends Model
40
{
41
    
42
    /**
43
     * The attributes that are mass assignable.
44
     *
45
     * @var array
46
     */
47
    protected $fillable = [
48
      'visitor_id',
49
      'user_id',
50
      'user_agent',
51
      'browser_version',
52
      'browser',
53
      'ip',
54
      'os'
55
    ];
56
    
57
    /**
58
     * Return a new unique id for a visitor.
59
     *
60
     * @return int|void
61
     */
62 10
    public static function createUniqueID(): int
63
    {
64 10
        $number = mt_rand(1000000000, 9999999999); // better than rand()
65
        
66 10
        if (self::uniqueIdExists($number)) {
67
            return self::createUniqueID();
68
        }
69
        
70 10
        return $number;
71
    }
72
    
73
    /**
74
     * Check to see if a unique id already exists in
75
     * the database.
76
     *
77
     * @param string $id The unique id to check for
78
     *
79
     * @return mixed
80
     */
81 10
    public static function uniqueIdExists($id)
82
    {
83 10
        return Visitor::whereUniqueId($id)->exists();
84
    }
85
    
86
    /**
87
     * Return the requests made by this visitor.
88
     *
89
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
90
     */
91 10
    public function requests(): HasMany
92
    {
93 10
        return $this->hasMany(VisitorRequest::class);
94
    }
95
}
96