Completed
Push — master ( c6d87b...7300b0 )
by ARCANEDEV
06:22
created

AbstractModel::customFirstOrCreate()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 9
cp 0
cc 3
eloc 8
nc 4
nop 2
crap 12
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 42
    public function __construct(array $attributes = [])
27
    {
28 42
        $this->setConnection($this->getConfig('database.connection', null))
29 42
             ->setPrefix($this->getConfig('database.prefix', 'tracker_'));
30
31 42
        parent::__construct($attributes);
32 42
    }
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 42
    protected function getConfig($key, $default = null)
47
    {
48 42
        return config("laravel-tracker.$key", $default);
49
    }
50
51
    /**
52
     * Get the model class.
53
     *
54
     * @param  string       $name
55
     * @param  string|null  $default
56
     *
57
     * @return string
58
     */
59 6
    protected function getModelClass($name, $default = null)
60
    {
61 6
        return $this->getConfig("models.$name", $default);
62
    }
63
64
    /* ------------------------------------------------------------------------------------------------
65
     |  Laravel 5.2 & 5.1 Support
66
     | ------------------------------------------------------------------------------------------------
67
     */
68
    /**
69
     * Handle dynamic method calls into the model.
70
     *
71
     * @param  string  $method
72
     * @param  array   $parameters
73
     *
74
     * @return mixed
75
     */
76 39
    public function __call($method, $parameters)
77
    {
78 39
        if (version_compare(app()->version(), '5.3.0', '<') && in_array($method, ['firstOrCreate']))
79 13
            return call_user_func_array([$this, 'custom'.ucfirst($method)], $parameters);
80
81 39
        return parent::__call($method, $parameters);
82
    }
83
84
    /**
85
     * Get the first record matching the attributes or create it.
86
     *
87
     * @param  array  $attributes
88
     * @param  array  $values
89
     *
90
     * @return self
91
     */
92
    public function customFirstOrCreate(array $attributes, array $values = [])
93
    {
94
        $instance = $this->newInstance();
95
96
        foreach ($attributes as $key => $value) {
97
            $instance = $instance->where($key, $value);
98
        }
99
100
        if ( ! is_null($first = $instance->first())) return $first;
101
102
        $instance = $this->newInstance($attributes + $values);
103
        $instance->save();
104
105
        return $instance;
106
    }
107
}
108