Completed
Push — master ( f328e9...1657f1 )
by ARCANEDEV
13s
created

AbstractModel   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 80
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getConfig() 0 4 1
A __call() 0 7 3
A customFirstOrCreate() 0 15 3
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
     |  Constructor
19
     | ------------------------------------------------------------------------------------------------
20
     */
21
    /**
22
     * Create a new Eloquent model instance.
23
     *
24
     * @param  array  $attributes
25
     */
26 66
    public function __construct(array $attributes = [])
27
    {
28 66
        $this->setConnection($this->getConfig('database.connection', null))
29 66
             ->setPrefix($this->getConfig('database.prefix', 'tracker_'));
30
31 66
        parent::__construct($attributes);
32 66
    }
33
34
    /* ------------------------------------------------------------------------------------------------
35
     |  Other Functions
36
     | ------------------------------------------------------------------------------------------------
37
     */
38
    /**
39
     * Get the tracker config.
40
     *
41
     * @param  string      $key
42
     * @param  mixed|null  $default
43
     *
44
     * @return mixed
45
     */
46 66
    protected function getConfig($key, $default = null)
47
    {
48 66
        return config("laravel-tracker.$key", $default);
49
    }
50
51
    /* ------------------------------------------------------------------------------------------------
52
     |  Laravel 5.2 & 5.1 Support
53
     | ------------------------------------------------------------------------------------------------
54
     */
55
    /**
56
     * Handle dynamic method calls into the model.
57
     *
58
     * @param  string  $method
59
     * @param  array   $parameters
60
     *
61
     * @return mixed
62
     */
63 60
    public function __call($method, $parameters)
64
    {
65 60
        if (version_compare(app()->version(), '5.3.0', '<') && in_array($method, ['firstOrCreate']))
66 50
            return call_user_func_array([$this, 'custom'.ucfirst($method)], $parameters);
67
68 60
        return parent::__call($method, $parameters);
69
    }
70
71
    /**
72
     * Get the first record matching the attributes or create it.
73
     *
74
     * @param  array  $attributes
75
     * @param  array  $values
76
     *
77
     * @return self
78
     */
79 40
    public function customFirstOrCreate(array $attributes, array $values = [])
80
    {
81 40
        $instance = $this->newInstance();
82
83 40
        foreach ($attributes as $key => $value) {
84 40
            $instance = $instance->where($key, $value);
85 20
        }
86
87 40
        if ( ! is_null($first = $instance->first())) return $first;
88
89 40
        $instance = $this->newInstance($attributes + $values);
90 40
        $instance->save();
91
92 40
        return $instance;
93
    }
94
}
95