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

AbstractModel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php namespace Arcanedev\LaravelTracker\Models;
2
3
use Arcanedev\Support\Bases\Model;
4
5
/**
6
 * Class     AbstractModel
7
 *
8
 * @package  Arcanedev\LaravelTracker\Models
9
 * @author   ARCANEDEV <[email protected]>
10
 *
11
 * @property  int  id
12
 *
13
 * @method  static  \Arcanedev\LaravelTracker\Models\AbstractModel  firstOrCreate(array $attributes, array $values = [])
14
 */
15
abstract class AbstractModel extends Model
16
{
17
    /* ------------------------------------------------------------------------------------------------
18
     |  Constants
19
     | ------------------------------------------------------------------------------------------------
20
     */
21
    const MODEL_AGENT                = 'agent';
22
    const MODEL_COOKIE               = 'cookie';
23
    const MODEL_DEVICE               = 'device';
24
    const MODEL_DOMAIN               = 'domain';
25
    const MODEL_ERROR                = 'error';
26
    const MODEL_GEOIP                = 'geoip';
27
    const MODEL_LANGUAGE             = 'language';
28
    const MODEL_PATH                 = 'path';
29
    const MODEL_QUERY                = 'query';
30
    const MODEL_REFERER              = 'referer';
31
    const MODEL_REFERER_SEARCH_TERM  = 'referer-search-term';
32
    const MODEL_ROUTE                = 'route';
33
    const MODEL_ROUTE_PATH           = 'route-path';
34
    const MODEL_ROUTE_PATH_PARAMETER = 'route-path-parameter';
35
    const MODEL_SESSION              = 'session';
36
    const MODEL_SESSION_ACTIVITY     = 'session-activity';
37
    const MODEL_USER                 = 'user';
38
39
    /* ------------------------------------------------------------------------------------------------
40
     |  Constructor
41
     | ------------------------------------------------------------------------------------------------
42
     */
43
    /**
44
     * Create a new Eloquent model instance.
45
     *
46
     * @param  array  $attributes
47
     */
48 82
    public function __construct(array $attributes = [])
49
    {
50 82
        $this->setConnection($this->getConfig('database.connection', null))
51 82
             ->setPrefix($this->getConfig('database.prefix', 'tracker_'));
52
53 82
        parent::__construct($attributes);
54 82
    }
55
56
    /* ------------------------------------------------------------------------------------------------
57
     |  Other Functions
58
     | ------------------------------------------------------------------------------------------------
59
     */
60
    /**
61
     * Get the tracker config.
62
     *
63
     * @param  string      $key
64
     * @param  mixed|null  $default
65
     *
66
     * @return mixed
67
     */
68 82
    protected function getConfig($key, $default = null)
69
    {
70 82
        return config("laravel-tracker.$key", $default);
71
    }
72
73
    /**
74
     * Get the model class.
75
     *
76
     * @param  string       $name
77
     * @param  string|null  $default
78
     *
79
     * @return string
80
     */
81 12
    protected function getModelClass($name, $default = null)
82
    {
83 12
        return $this->getConfig("models.$name", $default);
84
    }
85
86
    /* ------------------------------------------------------------------------------------------------
87
     |  Laravel 5.2 & 5.1 Support
88
     | ------------------------------------------------------------------------------------------------
89
     */
90
    /**
91
     * Handle dynamic method calls into the model.
92
     *
93
     * @param  string  $method
94
     * @param  array   $parameters
95
     *
96
     * @return mixed
97
     */
98 76
    public function __call($method, $parameters)
99
    {
100 76
        if (version_compare(app()->version(), '5.3.0', '<') && in_array($method, ['firstOrCreate']))
101 63
            return call_user_func_array([$this, 'custom'.ucfirst($method)], $parameters);
102
103 76
        return parent::__call($method, $parameters);
104
    }
105
106
    /**
107
     * Get the first record matching the attributes or create it.
108
     *
109
     * @param  array  $attributes
110
     * @param  array  $values
111
     *
112
     * @return self
113
     */
114 50
    public function customFirstOrCreate(array $attributes, array $values = [])
115
    {
116 50
        $instance = $this->newInstance();
117
118 50
        foreach ($attributes as $key => $value) {
119 50
            $instance = $instance->where($key, $value);
120 25
        }
121
122 50
        if ( ! is_null($first = $instance->first())) return $first;
123
124 50
        $instance = $this->newInstance($attributes + $values);
125 50
        $instance->save();
126
127 50
        return $instance;
128
    }
129
}
130