AbstractModel   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 57.14%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 1
dl 0
loc 94
rs 10
c 0
b 0
f 0
ccs 12
cts 21
cp 0.5714

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getConfig() 0 4 1
A getModelClass() 0 4 1
A __call() 0 7 3
A customFirstOrCreate() 0 15 3
1
<?php namespace Arcanedev\LaravelTracker\Models;
2
3
use Arcanedev\Support\Database\Model;
4
5
/**
6
 * Class     AbstractModel
7
 *
8
 * @package  Arcanedev\LaravelTracker\Models
9
 * @author   ARCANEDEV <[email protected]>
10
 *
11
 * @property  int  id
12
 */
13
abstract class AbstractModel extends Model
14
{
15
    /* -----------------------------------------------------------------
16
     |  Constructor
17
     | -----------------------------------------------------------------
18
     */
19
20
    /**
21
     * Create a new Eloquent model instance.
22
     *
23
     * @param  array  $attributes
24
     */
25 45
    public function __construct(array $attributes = [])
26
    {
27 45
        $this->setConnection($this->getConfig('database.connection', null))
28 45
             ->setPrefix($this->getConfig('database.prefix', 'tracker_'));
29
30 45
        parent::__construct($attributes);
31 45
    }
32
33
    /* ------------------------------------------------------------------------------------------------
34
     |  Other Functions
35
     | ------------------------------------------------------------------------------------------------
36
     */
37
    /**
38
     * Get the tracker config.
39
     *
40
     * @param  string      $key
41
     * @param  mixed|null  $default
42
     *
43
     * @return mixed
44
     */
45 45
    protected function getConfig($key, $default = null)
46
    {
47 45
        return config("laravel-tracker.$key", $default);
48
    }
49
50
    /**
51
     * Get the model class.
52
     *
53
     * @param  string       $name
54
     * @param  string|null  $default
55
     *
56
     * @return string
57
     */
58 6
    protected function getModelClass($name, $default = null)
59
    {
60 6
        return $this->getConfig("models.$name", $default);
61
    }
62
63
    /* ------------------------------------------------------------------------------------------------
64
     |  Laravel 5.2 & 5.1 Support
65
     | ------------------------------------------------------------------------------------------------
66
     */
67
    /**
68
     * Handle dynamic method calls into the model.
69
     *
70
     * @param  string  $method
71
     * @param  array   $parameters
72
     *
73
     * @return mixed
74
     */
75 42
    public function __call($method, $parameters)
76
    {
77 42
        if (version_compare(app()->version(), '5.3.0', '<') && in_array($method, ['firstOrCreate']))
78
            return call_user_func_array([$this, 'custom'.ucfirst($method)], $parameters);
79
80 42
        return parent::__call($method, $parameters);
81
    }
82
83
    /**
84
     * Get the first record matching the attributes or create it.
85
     *
86
     * @param  array  $attributes
87
     * @param  array  $values
88
     *
89
     * @return self
90
     */
91
    public function customFirstOrCreate(array $attributes, array $values = [])
92
    {
93
        $instance = $this->newInstance();
94
95
        foreach ($attributes as $key => $value) {
96
            $instance = $instance->where($key, $value);
97
        }
98
99
        if ( ! is_null($first = $instance->first())) return $first;
100
101
        $instance = $this->newInstance($attributes + $values);
102
        $instance->save();
103
104
        return $instance;
105
    }
106
}
107