Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.87 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Creditsuite\Services\GlobalDataService;
  4.  
  5. use App\Creditsuite\Domain\Models\FieldType;
  6. use App\Creditsuite\Domain\Models\Identity;
  7. use Exception;
  8. use Throwable;
  9.  
  10. /**
  11.  * Class FieldService
  12.  * @package App\Creditsuite\Services\GlobalDataService
  13.  */
  14. class FieldService
  15. {
  16.     /**
  17.      * @var string
  18.      */
  19.     protected $fieldSlug;
  20.  
  21.     /**
  22.      * @var integer
  23.      */
  24.     protected $fieldId;
  25.  
  26.     /**
  27.      * @var array
  28.      */
  29.     protected $identities;
  30.  
  31.     /**
  32.      * @param $fieldSlug
  33.      * @return $this
  34.      */
  35.     public function setFieldSlug($fieldSlug)
  36.     {
  37.         $this->fieldSlug = $fieldSlug;
  38.         return $this;
  39.     }
  40.  
  41.     /**
  42.      * @param $fieldId
  43.      * @return $this
  44.      */
  45.     public function setFieldId($fieldId)
  46.     {
  47.         $this->fieldId = $fieldId;
  48.         return $this;
  49.     }
  50.  
  51.     /**
  52.      * @param $identities
  53.      * @return $this
  54.      */
  55.     public function setIdentities($identities)
  56.     {
  57.         $this->identities = $identities;
  58.         return $this;
  59.     }
  60.  
  61.  
  62.     /**
  63.      * @throws Exception
  64.      */
  65.     protected function checkFieldId()
  66.     {
  67.         if($this->fieldId) {
  68.             return;
  69.         }
  70.  
  71.         try {
  72.             $this->fieldId = FieldType::where('slug', $this->fieldSlug)->firstOrFail()
  73.                 ->id;
  74.         } catch (Throwable $exception) {
  75.             throw new Exception('FieldService incoming parameters are invalid: fieldType not found');
  76.         }
  77.  
  78.     }
  79.  
  80.     /**
  81.      * @throws Exception
  82.      */
  83.     public function validate()
  84.     {
  85.         $this->checkFieldId();
  86.  
  87.         if(!$this->fieldId || !is_array($this->identities)) {
  88.             throw new Exception('FieldService incoming parameters are invalid');
  89.         }
  90.     }
  91.  
  92.     /**
  93.      * @param array $identityKeys
  94.      * @param string $fieldSlug
  95.      * @return mixed
  96.      * @throws Exception
  97.      */
  98.     public function versions()
  99.     {
  100.         $this->validate();
  101.  
  102.         $identities = Identity::whereIn('key', $this->identities)
  103.                 ->with([
  104.                 'fields' => function($query) {
  105.                     $query
  106.                         ->where('field_type_id', $this->fieldId);
  107.                 },
  108.             ])
  109.             ->get();
  110.  
  111.         $fields = $identities->map(function($item) {
  112.             return $item->fields;
  113.         })->collapse()->unique('id')->sortByDesc('updated_at')->values();
  114.  
  115.         return $fields;
  116.     }
  117.  
  118.     /**
  119.      * @return null
  120.      * @throws Exception
  121.      */
  122.     public function first()
  123.     {
  124.         $versions = $this->versions();
  125.  
  126.         return $versions->isNotEmpty() ? $versions->first() : null;
  127.     }
  128.  
  129.     /**
  130.      * @return null
  131.      * @throws Exception
  132.      */
  133.     public function last()
  134.     {
  135.         $versions = $this->versions();
  136.  
  137.         return $versions->isNotEmpty() ? $versions->last() : null;
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement