marcoszf
3/22/2016 - 5:49 PM

mod images - emidcms - Laravel - Repository pattern

mod images - emidcms - Laravel - Repository pattern

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Foto extends Model {

	protected $table = "tb_fotos";
	protected $primaryKey = "fot_id";
	public $timestamps = false;

	public function scopeCapa($query, $id, $modName){
		return $this->select('fot_titulo')
								->where(['fot_vinculo' => $id, 'fot_modulo' => $modName, 'fot_capa' => 1]);
	}
}
<?php
namespace App\Repositories;

interface ImagesRepositoryInterface{

	public function imgCrop();
	public function capeById($id, $modName);

}
<?php
namespace App\Services;

use App\Repositories\ImagesRepositoryInterface;
use Image;
use Config;
use File;

class ImagesService
{
	private $imagesRepository;

	public function __construct(ImagesRepositoryInterface $imagesRepository)
	{
		$this->imagesRepository = $imagesRepository;
	}

	public function imgCrop($idRow, $modName, $x, $y){
		
		$imgCapa = $this->imagesRepository->capeById($idRow, $modName);

		if( !$imgCapa ){
				$imgToSave = Image::make('arquivos/img-indisponivel/foto_indisponivel.png');
				$imgToCrop =  'arquivos/img-indisponivel/' . $x .'-'. $y .'foto_indisponivel.png';

				if (!File::exists($imgToCrop)) {
					$imgToSave->fit($x, $y)->save($imgToCrop);
   				$imgToSave->destroy();
				}
				return Config::get('constants.base_url') .$imgToCrop;
			}else{
				$pathImgsCrop = sprintf(Config::get('constants.path_images_crop'), $modName, $idRow);
				$imgToSave = Image::make( sprintf(Config::get('constants.path_images'), $modName, $idRow, 'original', $imgCapa->fot_titulo));
				$imgToCrop = $pathImgsCrop . $x .'-'. $y .$imgCapa->fot_titulo;

				if (!File::exists($imgToCrop)) {
					 if (!File::exists($pathImgsCrop)) {
					 	File::makeDirectory($pathImgsCrop, 0775, true);
					 }
					 		$imgToSave->fit($x, $y)->save($imgToCrop);
	     				$imgToSave->destroy();
	     	}
	     	return Config::get('constants.base_url') . sprintf(Config::get('constants.path_images_crop'), $modName, $idRow). $x .'-'. $y .$imgCapa->fot_titulo;
     	}

	}
}
<?php
namespace App\Repositories\Eloquent;

use App\Repositories\ImagesRepositoryInterface;
use App\Foto;

class MysqlImagesRepository implements ImagesRepositoryInterface
{
	public function imgCrop(){
		return Foto::ativo()->get();
	}

	public function capeById($id, $modName){
		return Foto::capa($id, $modName)->first();
	}

}