Ajout Exception.

This commit is contained in:
Cassandre Cantet 2017-10-03 18:10:59 +02:00
parent 6608d12b1c
commit 9d418d5623
3 changed files with 27 additions and 5 deletions

View File

@ -0,0 +1,8 @@
<?php
namespace Meoran\Images\Exception;
class InvalidContent extends \Exception
{
}

View File

@ -3,7 +3,10 @@
namespace Meoran\Images\Model;
use Illuminate\Database\Eloquent\Model;
use Intervention\Image\Exception\NotReadableException;
use Meoran\Images\Exception\InvalidContent;
use Spatie\ImageOptimizer\OptimizerChain;
use \Intervention\Image\Image as InterventionImage;
/**
* Class Image
@ -15,6 +18,8 @@ class Image extends Model
protected $_content;
protected $table = 'images';
public $fillable = [
'content',
'position',
@ -36,7 +41,10 @@ class Image extends Model
static::creating(function (Image $model) {
if (empty($model->content)) {
throw new \Exception("Content of picture can't be empty at creation");
throw new InvalidContent("Content must be defined to save image");
}
if (!($model->content instanceof InterventionImage)) {
throw new InvalidContent("Content must be an instance of Intervention\Image");
}
$model->savePicture();
});
@ -91,6 +99,13 @@ class Image extends Model
public function getContentAttribute($value)
{
if (empty($this->_content)) {
try {
$this->_content = app('image')->make($this->getPath());
} catch (NotReadableException $e) {
return null;
}
}
return $this->_content;
}

View File

@ -14,7 +14,6 @@ class ImagesServiceProvider extends ServiceProvider
require_once __DIR__ . '/../functions.php';
$this->loadMigrationsFrom(__DIR__ . '/../../database/migrations');
$this->mergeConfigFrom(__DIR__ . '/../../config/image.php', 'image');