Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- namespace App\Services;
- use App\Contracts\ApiKeyContract;
- use App\Entities\ApiKey;
- use Cartalyst\Sentinel\Sentinel as Sentinel;
- class ApiKeyService
- {
- /**
- * ApiKeyContract container.
- *
- * @var ApiKeyContract
- */
- protected $apiKey;
- /**
- * Sentinel container.
- *
- * @var Sentinel
- */
- protected $sentinel;
- /**
- * CandidateService construct.
- *
- * @param ApiKeyContract $apiKey
- * @param Sentinel $sentinel
- */
- public function __construct(ApiKeyContract $apiKey, Sentinel $sentinel)
- {
- $this->apiKey = $apiKey;
- $this->sentinel = $sentinel;
- }
- /*
- |--------------------------------------------------------------------------
- | Finding methods
- |--------------------------------------------------------------------------
- |
- | Below are all methods for finding data in database
- |
- */
- /**
- * Find all api keys
- *
- * @return \Illuminate\Database\Eloquent\Collection
- */
- public function findAll()
- {
- return $this->apiKey->findAll();
- }
- /**
- * Find all active api keys
- *
- * @return \Illuminate\Database\Eloquent\Collection
- */
- public function findActive()
- {
- return $this->apiKey->findActive();
- }
- /**
- * Find all archived api keys
- *
- * @return \Illuminate\Database\Eloquent\Collection
- */
- public function findArchived()
- {
- return $this->apiKey->findArchived();
- }
- /**
- * Find specific api key based on given ID
- *
- * @param integer $id
- * @return ApiKey
- */
- public function findById($id)
- {
- return $this->apiKey->findById($id);
- }
- /**
- * Find specific api key based on given key
- *
- * @param string $key
- * @return ApiKey
- */
- public function findByKey($key)
- {
- return $this->apiKey->findByKey($key);
- }
- /*
- |--------------------------------------------------------------------------
- | Methods for modifying data
- |--------------------------------------------------------------------------
- |
- | Below are all methods for finding and modifying data in database
- |
- */
- /**
- * Create new api key and save to database
- *
- * @param array $data
- * @return ApiKey
- */
- public function create($data = [])
- {
- $data['user_id'] = $this->sentinel->getUser()->id;
- $data['key'] = str_random(32);
- return $this->apiKey->create($data);
- }
- /**
- * Update given api key
- *
- * @param ApiKey $apiKey
- * @param array $data
- * @return boolean
- */
- public function update(ApiKey $apiKey, $data = [])
- {
- if (isset($data['regenerate']) && $data['regenerate']) {
- $data['key'] = str_random(32);
- }
- return $this->apiKey->update($apiKey, $data);
- }
- /**
- * Soft delete specific api key
- *
- * @param ApiKey $apiKey
- * @return boolean
- */
- public function archive(ApiKey $apiKey)
- {
- return $this->apiKey->archive($apiKey);
- }
- /**
- * Restore soft deleted api key
- *
- * @param ApiKey $apiKey
- * @return boolean
- */
- public function restore(ApiKey $apiKey)
- {
- return $this->apiKey->restore($apiKey);
- }
- /**
- * Complete remove specific api key from database
- *
- * @param ApiKey $apiKey
- * @return void
- */
- public function forceDelete(ApiKey $apiKey)
- {
- $this->apiKey->forceDelete($apiKey);
- }
- }
Add Comment
Please, Sign In to add comment