BaseModel   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 4
Bugs 2 Features 1
Metric Value
wmc 19
c 4
b 2
f 1
lcom 2
cbo 6
dl 0
loc 193
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getTable() 0 4 1
A boot() 0 11 4
A savedEvent() 0 9 1
A deletingEvent() 0 9 1
A getSingular() 0 8 3
A getPlural() 0 8 3
A formatTableName() 0 4 1
B validate() 0 20 5
1
<?php namespace App\LaravelRestCms;
2
3
use Illuminate\Contracts\Validation\ValidationException;
4
use Illuminate\Database\Query\Builder;
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Foundation\Validation\ValidatesRequests;
7
use Illuminate\Support\MessageBag;
8
use Illuminate\Support\Pluralizer;
9
use Illuminate\Support\Singular;
10
11
/**
12
 * Class BaseModel
13
 * 
14
 * @method \Illuminate\Database\Query\Builder whereIn(string $column, mixed $values, string $boolean, bool $not)
15
 */
16
abstract class BaseModel extends Model {
17
18
	use CacheTrait;
19
	use SearchTrait;
20
	use ValidatesRequests;
21
22
	/**
23
	 * The table name
24
	 * 
25
	 * @var string
26
	 */
27
	protected $table;
28
	
29
	/**
30
	 * The singular version of the table name
31
	 * 
32
	 * @var string
33
	 */
34
	protected $singular;
35
	
36
	/**
37
	 * The plural version of the table name
38
	 * 
39
	 * @var string
40
	 */
41
	protected $plural;
42
43
	/**
44
	 * The Validator object
45
	 * 
46
	 * @var \Validator
47
	 */
48
	protected $validation;
49
50
	/**
51
	 * Rules to validate when creating a model
52
	 * 
53
	 * @var array
54
	 */
55
	protected static $createRules;
56
57
	/**
58
	 * Rules to validate when updating a model
59
	 * 
60
	 * @var array
61
	 */
62
	protected static $updateRules;
63
64
	/**
65
	 * Rules to validate when updating a model, which concatenates with static::$createRules
66
	 * Overide this if using different PKs
67
	 * Ignored if static::$updateRules is populated
68
	 * 
69
	 * @var array
70
	 */
71
	protected static $updateRulesConcat = [	
72
		'id' => 'required|integer'
73
	];
74
75
	/**
76
	 * Indicates if the model should be attributed with created_by and updated_by
77
	 * 
78
	 * @var bool
79
	 */
80
	public $attirbution = false;
81
82
	/**
83
	 * The "booting" method of the model.
84
	 *
85
	 * @return void
86
	 */
87
	protected static function boot()
88
	{
89
		if (!is_null(static::$updateRules) && !is_null(static::$updateRulesConcat) && sizeof(static::$updateRulesConcat)) {
90
			static::$updateRules = static::$createRules + static::$updateRulesConcat; 
91
		}
92
93
		parent::boot();
94
95
		static::savedEvent();
96
		static::deletingEvent();
97
	}	 
98
	
99
	/**
100
	 * Gets the table name of the model
101
	 * Override this for custom names to be used in the caching engine
102
	 * 
103
	 * @return string
104
	 */
105
	public function getTable()
106
	{
107
		return $this->table;
108
	}
109
110
	/**
111
	 * Hooks into the "saved" event
112
	 * 
113
	 * @return void
114
	 */
115
	protected static function savedEvent()
116
	{
117
		static::saved(function($model)
118
		{
119
			static::addToCache($model);
120
121
			return true;
122
		});
123
	}
124
125
	/**
126
	 * Hooks into the "deleting" event
127
	 * 
128
	 * @return void
129
	 */
130
	protected static function deletingEvent()
131
	{
132
		static::deleting(function($model)
133
		{
134
		   static::removeFromCache($model);
135
136
			return true;
137
		});
138
	}
139
	
140
	/**
141
	 * Retrieves the singular name of the table
142
	 * 
143
	 * @param  boolean $format
144
	 * @return string
145
	 */
146
	public function getSingular($format = false)
147
	{
148
		if (!isset($this->singular)) {
149
			$this->singular = Pluralizer::singular($this->table);
150
		}
151
152
		return $format ? $this->formatTableName($this->singular) : $this->singular;
153
	}	
154
	
155
	/**
156
	 * Retrieves the plural name of the table
157
	 * 
158
	 * @param  boolean $format
159
	 * @return string
160
	 */
161
	public function getPlural($format = false)
162
	{
163
		if (!isset($this->plural)) {
164
			$this->plural = Pluralizer::plural($this->table);
165
		}
166
167
		return $format ? $this->formatTableName($this->plural) : $this->plural;
168
	}
169
	
170
	/**
171
	 * Formats the table name into a human readable format
172
	 * 
173
	 * @param  string $name
174
	 * @return string
175
	 */
176
	protected static function formatTableName($name)
177
	{
178
		return ucwords(str_replace('_', ' ', $name));
179
	}
180
	
181
	/**
182
	 * Validates a model
183
	 * 
184
	 * @param  array  	$data     
185
	 * @param  boolean 	$isUpdate 
186
	 * @return boolean            
187
	 */
188
	public function validate($data, $isUpdate = false)
189
	{
190
		if ($isUpdate) {
191
			$rules = static::$updateRules;
192
		} else {
193
			$rules = static::$createRules;
194
		}
195
196
		if (is_null($rules)) {
197
			throw new \Exception(new MessageBag(['Could not find ' . ($isUpdate ? 'update' : 'create') . ' rules for ' . get_class($this)]));
198
		}
199
200
		$this->validation = \Validator::make($data, $rules);
201
		
202
		if (!$this->validation->passes()) {
203
			throw new ValidationException(new MessageBag($this->validation->errors()->all()));
204
		}
205
206
		return true;
207
	}
208
}