Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.71 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Repositories;
  4.  
  5. use Illuminate\Support\Facades\Cache;
  6.  
  7. /**
  8.  * Classe abstrata para persistencia de dados utilizando
  9.  * EloquentORM e Cache
  10.  */
  11. abstract class BaseRepository{
  12.  
  13.     /**
  14.      * Modelo que o repo utilizarar para trabalhar
  15.      *
  16.      * @var [type]
  17.      */
  18.     protected $model;
  19.  
  20.     /**
  21.      * Array contendo nomes das tabelas
  22.      * para o eager loading do repository
  23.      *
  24.      * @var [type]
  25.      */
  26.     private $eagerLoading;
  27.  
  28.     /**
  29.      * Atribui model ao repository
  30.      *
  31.      * @param App\Model $model
  32.      * @return void
  33.      */
  34.     public function setModel($model)
  35.     {
  36.         $this->model = $model;
  37.     }
  38.  
  39.     /**
  40.      * Atribui valores para eager loading
  41.      *
  42.      * @param [array] $array
  43.      * @return void
  44.      */
  45.     public function setEager($array){
  46.         $this->eagerLoading = collect($array);
  47.     }
  48.  
  49.     /**
  50.      * Seta o model padrão e retorna o model
  51.      *
  52.      * @param [type] $id
  53.      * @return void
  54.      */
  55.     public function findById($id){
  56.         $this->model = $this->model->findOrFail($id);
  57.         return $this->model;
  58.     }
  59.  
  60.     /**
  61.      * Retorna valor da chave do cache
  62.      *
  63.      * @return string
  64.      */
  65.     protected final function getCacheKey(){
  66.         return explode('\\', get_class($this->model))[1];
  67.     }
  68.  
  69.     /**
  70.      * Esquece o valor salvo no cache
  71.      *
  72.      * @return void
  73.      */
  74.     protected final function forgetCache(){
  75.         Cache::forget($this->getCacheKey());
  76.     }
  77.  
  78.     /**
  79.      * Busca todos os registros do model do banco de dados,
  80.      * buscandoo do cache se existir
  81.      *
  82.      * @param [array] $eagerParams
  83.      * @return Collection
  84.      */
  85.     public function all(){
  86.         $cacheKey = $this->getCacheKey();
  87.  
  88.         if (Cache::has($cacheKey)) return Cache::get($cacheKey);
  89.         else {
  90.             if( !isset($this->eagerLoading) ) $collection = $this->model->all();
  91.             else $collection = $this->model->with($this->eagerLoading->toArray())->get();
  92.  
  93.             Cache::put($cacheKey,$collection);
  94.             return $collection;
  95.         }
  96.     }
  97.  
  98.     /**
  99.      * Insere novo model
  100.      *
  101.      * @param [array] $params
  102.      * @return App\Model
  103.      */
  104.     public function create($params){
  105.         $this->forgetCache();
  106.         return $this->model->create($params);
  107.     }
  108.  
  109.     /**
  110.      * Atualiza model
  111.      *
  112.      * @param [array] $params
  113.      * @return App\Model
  114.      */
  115.     public function update($params){
  116.         $this->forgetCache();
  117.         return $this->model->update($params);
  118.     }
  119.  
  120.     /**
  121.      * Deleta o model
  122.      */
  123.     public function delete(){
  124.         $this->forgetCache();
  125.         return $this->model->delete();
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement