zalazdi

Untitled

Mar 8th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.66 KB | None | 0 0
  1. <?PHP
  2.  
  3. namespace App\Services;
  4.  
  5. use App\Contracts\ApiKeyContract;
  6. use App\Entities\ApiKey;
  7.  
  8. use Cartalyst\Sentinel\Sentinel as Sentinel;
  9.  
  10. class ApiKeyService
  11. {
  12.     /**
  13.      * ApiKeyContract container.
  14.      *
  15.      * @var ApiKeyContract
  16.      */
  17.     protected $apiKey;
  18.  
  19.     /**
  20.      * Sentinel container.
  21.      *
  22.      * @var Sentinel
  23.      */
  24.     protected $sentinel;
  25.  
  26.     /**
  27.      * CandidateService construct.
  28.      *
  29.      * @param ApiKeyContract $apiKey
  30.      * @param Sentinel $sentinel
  31.      */
  32.     public function __construct(ApiKeyContract $apiKey, Sentinel $sentinel)
  33.     {
  34.         $this->apiKey = $apiKey;
  35.         $this->sentinel = $sentinel;
  36.     }
  37.  
  38.     /*
  39.     |--------------------------------------------------------------------------
  40.     | Finding methods
  41.     |--------------------------------------------------------------------------
  42.     |
  43.     | Below are all methods for finding data in database
  44.     |
  45.     */
  46.  
  47.     /**
  48.      * Find all api keys
  49.      *
  50.      * @return \Illuminate\Database\Eloquent\Collection
  51.      */
  52.     public function findAll()
  53.     {
  54.         return $this->apiKey->findAll();
  55.     }
  56.  
  57.     /**
  58.      * Find all active api keys
  59.      *
  60.      * @return \Illuminate\Database\Eloquent\Collection
  61.      */
  62.     public function findActive()
  63.     {
  64.         return $this->apiKey->findActive();
  65.     }
  66.  
  67.     /**
  68.      * Find all archived api keys
  69.      *
  70.      * @return \Illuminate\Database\Eloquent\Collection
  71.      */
  72.     public function findArchived()
  73.     {
  74.         return $this->apiKey->findArchived();
  75.     }
  76.  
  77.     /**
  78.      * Find specific api key based on given ID
  79.      *
  80.      * @param integer $id
  81.      * @return ApiKey
  82.      */
  83.     public function findById($id)
  84.     {
  85.         return $this->apiKey->findById($id);
  86.     }
  87.  
  88.     /**
  89.      * Find specific api key based on given key
  90.      *
  91.      * @param string $key
  92.      * @return ApiKey
  93.      */
  94.     public function findByKey($key)
  95.     {
  96.         return $this->apiKey->findByKey($key);
  97.     }
  98.  
  99.     /*
  100.     |--------------------------------------------------------------------------
  101.     | Methods for modifying data
  102.     |--------------------------------------------------------------------------
  103.     |
  104.     | Below are all methods for finding and modifying data in database
  105.     |
  106.     */
  107.  
  108.     /**
  109.      * Create new api key and save to database
  110.      *
  111.      * @param array $data
  112.      * @return ApiKey
  113.      */
  114.     public function create($data = [])
  115.     {
  116.         $data['user_id'] = $this->sentinel->getUser()->id;
  117.         $data['key'] = str_random(32);
  118.  
  119.         return $this->apiKey->create($data);
  120.     }
  121.  
  122.     /**
  123.      * Update given api key
  124.      *
  125.      * @param ApiKey $apiKey
  126.      * @param array $data
  127.      * @return boolean
  128.      */
  129.     public function update(ApiKey $apiKey, $data = [])
  130.     {
  131.         if (isset($data['regenerate']) && $data['regenerate']) {
  132.             $data['key'] = str_random(32);
  133.         }
  134.  
  135.         return $this->apiKey->update($apiKey, $data);
  136.     }
  137.  
  138.     /**
  139.      * Soft delete specific api key
  140.      *
  141.      * @param ApiKey $apiKey
  142.      * @return boolean
  143.      */
  144.     public function archive(ApiKey $apiKey)
  145.     {
  146.         return $this->apiKey->archive($apiKey);
  147.     }
  148.  
  149.     /**
  150.      * Restore soft deleted api key
  151.      *
  152.      * @param ApiKey $apiKey
  153.      * @return boolean
  154.      */
  155.     public function restore(ApiKey $apiKey)
  156.     {
  157.         return $this->apiKey->restore($apiKey);
  158.     }
  159.  
  160.     /**
  161.      * Complete remove specific api key from database
  162.      *
  163.      * @param ApiKey $apiKey
  164.      * @return void
  165.      */
  166.     public function forceDelete(ApiKey $apiKey)
  167.     {
  168.         $this->apiKey->forceDelete($apiKey);
  169.     }
  170. }
Add Comment
Please, Sign In to add comment