SHARE
TWEET

Untitled

a guest Oct 19th, 2017 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace App\Repositories;
  4.  
  5. use App\Exceptions\ImportModelNotFoundException;
  6. use App\Exceptions\SupplierNotFoundException;
  7. use App\Models\FrtsModel;
  8. use App\Models\Supplier;
  9. use LaravelDoctrine\ORM\Facades\EntityManager;
  10.  
  11. class ImportModels
  12. {
  13.     const TAG_NAME = 'frts_models';
  14.  
  15.     public function get($id, $type, $ignoreCheck = false)
  16.     {
  17.         // check if frts is all 0's
  18.         if (empty((integer) $id) && !$ignoreCheck) return null;
  19.         if (null !== $cachedModel = CachedModels::get($type, $id)) {
  20.             try {
  21.                 return EntityManager::getReference($type, $cachedModel);
  22.             } catch (\Exception $e) {
  23.                 dd($id);
  24.             }
  25.         }
  26.         $model = collect(EntityManager::getUnitOfWork()->getScheduledEntityInsertions())->first(function($model) use ($id, $type) {
  27.             if ($model instanceof FrtsModel) {
  28.                 return $model->getFrtsId() == $id && (get_class($model) == $type || is_subclass_of(get_class($model), $type));
  29.             }
  30.             return false;
  31.         }, null);
  32.  
  33.         $model = EntityManager::getRepository($type)->findOneByFrtsId($id);
  34.  
  35.         if (null === $model && $type == Supplier::class){
  36.             throw new SupplierNotFoundException($id);
  37.         } elseif (null === $model) {
  38.             // we can optimise this adding the frts id as an index
  39.             throw new ImportModelNotFoundException($type, $id);
  40.         } else {
  41.             CachedModels::store($model);
  42.         }
  43.  
  44.  
  45.         return $model;
  46.     }
  47.  
  48.     public function put(FrtsModel $model)
  49.     {
  50.         EntityManager::persist($model);
  51.     }
  52.  
  53. }
RAW Paste Data
Top