Completed
Push — master ( bf9518...963b04 )
by David
04:31
created

BaseModel::deletingEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 4
nc 1
nop 0
1
<?php namespace App\LaravelRestCms;
2
3
use Illuminate\Contracts\Validation\ValidationException;
4
use Illuminate\Database\Eloquent\Model;
5
use Illuminate\Foundation\Validation\ValidatesRequests;
6
use Illuminate\Support\MessageBag;
7
use Illuminate\Support\Pluralizer;
8
use Illuminate\Support\Singular;
9
10
abstract class BaseModel extends Model {
11
12
	use CacheTrait;
13
	use SearchTrait;
14
	use ValidatesRequests;
15
16
	/**
17
	 * The table name
18
	 * 
19
	 * @var string
20
	 */
21
	protected $table;
22
	
23
	/**
24
	 * The singular version of the table name
25
	 * 
26
	 * @var string
27
	 */
28
	protected $singular;
29
	
30
	/**
31
	 * The plural version of the table name
32
	 * 
33
	 * @var string
34
	 */
35
	protected $plural;
36
37
	/**
38
	 * The Validator object
39
	 * 
40
	 * @var \Validator
41
	 */
42
	protected $validation;
43
44
	/**
45
	 * Rules to validate when creating a model
46
	 * 
47
	 * @var array
48
	 */
49
	protected static $createRules;
50
51
	/**
52
	 * Rules to validate when updating a model
53
	 * 
54
	 * @var array
55
	 */
56
	protected static $updateRules;
57
58
	/**
59
	 * Rules to validate when updating a model, which concatenates with static::$createRules
60
	 * Overide this if using different PKs
61
	 * Ignored if static::$updateRules is populated
62
	 * 
63
	 * @var array
64
	 */
65
	protected static $updateRulesConcat = [	
66
		'id' => 'required|integer'
67
	];
68
69
	/**
70
	 * Indicates if the model should be attributed with created_by and updated_by
71
	 * 
72
	 * @var bool
73
	 */
74
	public $attirbution = false;
75
76
	/**
77
     * The "booting" method of the model.
78
     *
79
     * @return void
80
     */
81
    protected static function boot()
82
    {
83
        if (!is_null(static::$updateRules) && !is_null(static::$updateRulesConcat) && sizeof(static::$updateRulesConcat)) {
84
	        static::$updateRules = static::$createRules + static::$updateRulesConcat; 
85
	    }
86
87
	    parent::boot();
88
89
        static::savedEvent();
90
        static::deletingEvent();
91
    }	 
92
	
93
	/**
94
	 * Gets the table name of the model.
95
	 * Override this for custom names to be used in the caching engine.
96
	 * 
97
	 * @return string
98
	 */
99
	public function getTable()
100
	{
101
		return $this->table;
102
	}
103
104
    /**
105
     * Hooks into the "saved" event
106
     * 
107
     * @return void
108
     */
109
    protected static function savedEvent()
110
    {
111
        static::saved(function($model)
112
        {
113
            static::addToCache($model);
114
115
            return true;
116
        });
117
    }
118
119
    /**
120
     * Hooks into the "deleting" event
121
     * 
122
     * @return void
123
     */
124
    protected static function deletingEvent()
125
    {
126
        static::deleting(function($model)
127
        {
128
           static::removeFromCache($model);
129
130
            return true;
131
        });
132
    }
133
	
134
	/**
135
	 * Retrieves the singular name of the table
136
	 * 
137
	 * @param  boolean $format
138
	 * @return string
139
	 */
140
	public function getSingular($format = false)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
	{
142
		if (!isset($this->singular)) {
143
			$this->singular = Pluralizer::singular($this->table);
144
		}
145
146
		return $format ? $this->formatTableName($this->singular) : $this->singular;
147
	}	
148
	
149
	/**
150
	 * Retrieves the plural name of the table
151
	 * 
152
	 * @param  boolean $format
153
	 * @return string
154
	 */
155
	public function getPlural($format = false)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156
	{
157
		if (!isset($this->plural)) {
158
			$this->plural = Pluralizer::plural($this->table);
159
		}
160
161
		return $format ? $this->formatTableName($this->plural) : $this->plural;
162
	}
163
	
164
	/**
165
	 * Formats the table name into a human readable format
166
	 * 
167
	 * @param  string $name
168
	 * @return string
169
	 */
170
	protected static function formatTableName($name)
171
	{
172
		return ucwords(str_replace('_', ' ', $name));
173
	}
174
	
175
	/**
176
	 * Validates a model
177
	 * 
178
	 * @param  array  	$data     
179
	 * @param  boolean 	$isUpdate 
180
	 * @return boolean            
181
	 */
182
	public function validate($data, $isUpdate = false)
183
	{
184
		if ($isUpdate) {
185
			$rules = static::$updateRules;
186
		} else {
187
			$rules = static::$createRules;
188
		}
189
190
		if (is_null($rules)) {
191
			throw new \Exception(new MessageBag('Could not find ' . ($isUpdate ? 'update' : 'create') . ' rules for ' . get_class($this)));
0 ignored issues
show
Documentation introduced by
'Could not find ' . ($is...or ' . get_class($this) is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
192
		}
193
194
		$this->validation = \Validator::make($data, $rules);
195
		
196
		if (!$this->validation->passes()) {
197
			throw new ValidationException(new MessageBag($this->validation->errors()->all()));
198
		}
199
200
		return true;
201
	}
202
}