ldc-crm/app/Models/Service.php
2021-08-23 21:46:10 +02:00

201 lines
5.8 KiB
PHP

<?php
namespace App\Models;
use App\Jobs\SetServiceStats;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Rennokki\QueryCache\Traits\QueryCacheable;
class Service extends Model
{
use HasFactory;
use QueryCacheable;
protected $cacheFor = 0;
protected $fillable = [
'name'
];
public function __construct(array $attributes = [])
{
$this->cacheFor = config('api.servicesDBCache') * 60;
parent::__construct($attributes);
}
/**
* Set the base cache tags that will be present
* on all queries.
*
*! This is used to flush caching
*
* @return array
*/
protected function getCacheBaseTags(): array
{
return [
'crm_services',
];
}
protected static function booted()
{
static::creating(function (self $service) {
$service->generateBearerToken();
if (Hash::needsRehash($service->bearer_token)) {
$service->bearer_token = Hash::make($service->bearer_token);
}
if (Hash::needsRehash($service->bearer_pwd)) {
$service->bearer_pwd = Hash::make($service->bearer_pwd);
}
if (Hash::needsRehash($service->api_token)) {
$service->api_token = Hash::make($service->api_token);
}
});
static::updating(function (self $service) {
if (Hash::needsRehash($service->bearer_token)) {
$service->bearer_token = Hash::make($service->bearer_token);
}
if (Hash::needsRehash($service->bearer_pwd)) {
$service->bearer_pwd = Hash::make($service->bearer_pwd);
}
if (Hash::needsRehash($service->api_token)) {
$service->api_token = Hash::make($service->api_token);
}
});
static::created(function () {
// flush Eloquent cache
self::flushQueryCache();
});
static::updated(function () {
// flush Eloquent cache
self::flushQueryCache();
});
static::deleted(function () {
// flush Eloquent cache
self::flushQueryCache();
});
}
/**
* Generates a new Api Token
*
* @return string
*/
public function generateApiToken(): string
{
$this->api_token = hash('sha256', Str::uuid());
$this->api_token_date = now();
return $this->api_token;
}
/**
* Generates new bearer credentials
*
* @return string
*/
public function generateBearerToken(): string
{
$this->bearer_token = hash('sha256', Str::uuid());
$this->bearer_token_date = now();
$this->bearer_pwd = hash('ripemd160', Str::uuid());
return $this->bearer_token;
}
/**
* Increment API CRM Stat
*
* @return void
*/
public function incrementStat(): void
{
SetServiceStats::dispatch($this)->onConnection('database');
}
/**
* Get Service from apiToken with its brand
*
* @param string $apiToken
* @return \App\Models\Service
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public static function getFromApiToken(string $apiToken): self
{
$services = static::with([
'brand' => function ($query) {
return $query->cacheTags(["service:author"]);
},
])->get();
/** @var \App\Models\Service $service */
foreach ($services as $service) {
if ($service->api_token and Hash::check($apiToken, $service->api_token)) {
return $service->load([
'brand' => function ($query) use ($service) {
return $query->cacheTags(["service:{$service->id}:brand"]);
},
]);
}
}
throw new ModelNotFoundException('Could not find service from Api Token');
}
/**
* Get Service from bearerToken with its brand
*
* @param string $bearerToken
* @return \App\Models\Service
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public static function getFromBearerToken(string $bearerToken): self
{
$services = static::all();
/** @var \App\Models\Service $service */
foreach ($services as $service) {
if ($service->bearer_token and Hash::check($bearerToken, $service->bearer_token)) {
return $service->load([
'brand' => function ($query) use ($service) {
return $query->cacheTags(["service:{$service->id}:brand"]);
},
]);
}
}
throw new ModelNotFoundException('Could not find service from Service Token');
}
/**
* Authenticate from the api token string
*
* @param array $guards
* @param string $apiToken
* @return bool
*/
public function authenticateApi(string $apiToken): bool
{
return $this->api_token and Hash::check($apiToken, $this->api_token) and
((abs(strtotime($this->api_token_date) - time()) * 60) < (config('api.tokenTimeout') * 60));
}
/**
* Brand BelongsTo relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function brand()
{
return $this->belongsTo(Brand::class);
}
/**
* ServiceStat hasMany relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function servicesStats()
{
return $this->hasMany(ServiceStat::class);
}
}