Refactor thrown class into its own method.

This commit is contained in:
michaelachrisco 2017-03-31 10:58:44 -07:00
parent 4854827098
commit 07453dad8e
2 changed files with 25 additions and 39 deletions

View file

@ -2,6 +2,4 @@
namespace MichaelAChrisco\ReadOnly; namespace MichaelAChrisco\ReadOnly;
use RuntimeException; use RuntimeException;
class ReadOnlyException extends RuntimeException{ class ReadOnlyException extends RuntimeException{}
}

View file

@ -4,6 +4,11 @@ use Illuminate\Database\Eloquent\Builder;
use MichaelAChrisco\ReadOnly\ReadOnlyException; use MichaelAChrisco\ReadOnly\ReadOnlyException;
trait ReadOnlyTrait { trait ReadOnlyTrait {
static function readOnly($class){
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
}
/** /**
* throws ReadOnlyException on create * throws ReadOnlyException on create
* @method create * @method create
@ -11,8 +16,7 @@ trait ReadOnlyTrait {
* *
*/ */
static function create(array $attributes = []){ static function create(array $attributes = []){
$class = get_called_class(); ReadOnlyTrait::readOnly(get_called_class());
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
} }
/** /**
@ -22,8 +26,7 @@ trait ReadOnlyTrait {
* *
*/ */
static function forceCreate(array $attributes){ static function forceCreate(array $attributes){
$class = get_called_class(); ReadOnlyTrait::readOnly(get_called_class());
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
} }
/** /**
@ -33,8 +36,7 @@ trait ReadOnlyTrait {
* *
*/ */
public function save(array $options = []){ public function save(array $options = []){
$class = get_called_class(); ReadOnlyTrait::readOnly(get_called_class());
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
} }
/** /**
@ -45,8 +47,7 @@ trait ReadOnlyTrait {
* *
*/ */
public function update(array $attributes = [], array $options = []){ public function update(array $attributes = [], array $options = []){
$class = get_called_class(); ReadOnlyTrait::readOnly(get_called_class());
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
} }
/** /**
@ -56,8 +57,7 @@ trait ReadOnlyTrait {
* *
*/ */
static function firstOrCreate(array $arr){ static function firstOrCreate(array $arr){
$class = get_called_class(); ReadOnlyTrait::readOnly(get_called_class());
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
} }
/** /**
@ -67,8 +67,7 @@ trait ReadOnlyTrait {
* *
*/ */
static function firstOrNew(array $arr){ static function firstOrNew(array $arr){
$class = get_called_class(); ReadOnlyTrait::readOnly(get_called_class());
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
} }
/** /**
@ -77,8 +76,7 @@ trait ReadOnlyTrait {
* *
*/ */
public function delete(){ public function delete(){
$class = get_called_class(); ReadOnlyTrait::readOnly(get_called_class());
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
} }
/** /**
@ -88,8 +86,7 @@ trait ReadOnlyTrait {
* *
*/ */
static function destroy($ids){ static function destroy($ids){
$class = get_called_class(); ReadOnlyTrait::readOnly(get_called_class());
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
} }
/** /**
@ -98,8 +95,7 @@ trait ReadOnlyTrait {
* *
*/ */
public function restore(){ public function restore(){
$class = get_called_class(); ReadOnlyTrait::readOnly(get_called_class());
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
} }
/** /**
@ -108,8 +104,7 @@ trait ReadOnlyTrait {
* *
*/ */
public function forceDelete(){ public function forceDelete(){
$class = get_called_class(); ReadOnlyTrait::readOnly(get_called_class());
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
} }
/** /**
@ -118,8 +113,7 @@ trait ReadOnlyTrait {
* *
*/ */
public function performDeleteOnModel(){ public function performDeleteOnModel(){
$class = get_called_class(); ReadOnlyTrait::readOnly(get_called_class());
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
} }
/** /**
@ -128,8 +122,7 @@ trait ReadOnlyTrait {
* *
*/ */
public function push(){ public function push(){
$class = get_called_class(); ReadOnlyTrait::readOnly(get_called_class());
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
} }
/** /**
@ -138,8 +131,7 @@ trait ReadOnlyTrait {
* *
*/ */
public function finishSave(array $options){ public function finishSave(array $options){
$class = get_called_class(); ReadOnlyTrait::readOnly(get_called_class());
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
} }
/** /**
@ -148,8 +140,7 @@ trait ReadOnlyTrait {
* *
*/ */
public function performUpdate(Builder $query, array $options = []){ public function performUpdate(Builder $query, array $options = []){
$class = get_called_class(); ReadOnlyTrait::readOnly(get_called_class());
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
} }
/** /**
@ -158,8 +149,7 @@ trait ReadOnlyTrait {
* *
*/ */
public function touch(){ public function touch(){
$class = get_called_class(); ReadOnlyTrait::readOnly(get_called_class());
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
} }
/** /**
@ -168,8 +158,7 @@ trait ReadOnlyTrait {
* *
*/ */
public function insert(){ public function insert(){
$class = get_called_class(); ReadOnlyTrait::readOnly(get_called_class());
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
} }
/** /**
@ -178,7 +167,6 @@ trait ReadOnlyTrait {
* *
*/ */
public function truncate(){ public function truncate(){
$class = get_called_class(); ReadOnlyTrait::readOnly(get_called_class());
throw new ReadOnlyException("Not allowed to persist changes in read-only model {$class}");
} }
} }