ldc-crm/app/Models/Service.php

236 lines
6.9 KiB
PHP
Raw Normal View History

2020-12-01 17:02:10 +01:00
<?php
namespace App\Models;
use App\Jobs\SetServiceStats;
2020-12-01 17:02:10 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2021-03-14 10:45:42 +01:00
use Illuminate\Database\Eloquent\ModelNotFoundException;
2021-08-02 11:46:08 +02:00
use Illuminate\Support\Facades\Hash;
2021-03-14 10:45:42 +01:00
use Illuminate\Support\Str;
use Kwaadpepper\CrudPolicies\Enums\CrudAction;
use Kwaadpepper\CrudPolicies\Enums\CrudType;
use Kwaadpepper\CrudPolicies\Traits\IsCrudModel;
use Rennokki\QueryCache\Traits\QueryCacheable;
2020-12-01 17:02:10 +01:00
class Service extends Model
{
use IsCrudModel;
2020-12-01 17:02:10 +01:00
use HasFactory;
use QueryCacheable;
protected $crudLabelColumn = 'name';
protected $crudValueColumn = 'id';
protected $cacheFor = 0;
2020-12-01 17:02:10 +01:00
2021-03-14 10:45:42 +01:00
protected $fillable = [
'name'
];
public function __construct(array $attributes = [])
{
$this->cacheFor = config('api.servicesDBCache') * 60;
self::$editableProperties = [
'name' => [
'label' => Str::ucfirst(trans('models.common.name')),
'type' => CrudType::string(),
'validate' => [
'min:5',
'max:255'
],
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::create(),
CrudAction::update()
]
],
'brand_id' => [
'label' => Str::ucfirst(trans('models.models.brand')),
'type' => CrudType::belongsTo(),
'belongsTo' => Brand::class,
'actions' => [
CrudAction::viewAny(),
CrudAction::view()
]
],
];
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',
];
}
2021-03-14 10:45:42 +01:00
protected static function booted()
{
static::creating(function (self $service) {
2021-03-14 10:45:42 +01:00
$service->generateBearerToken();
if (Hash::needsRehash($service->bearer_token)) {
$service->bearer_token = Hash::make($service->bearer_token);
}
2021-08-02 11:46:08 +02:00
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);
}
2021-08-02 11:46:08 +02:00
});
static::updating(function (self $service) {
if (Hash::needsRehash($service->bearer_token)) {
$service->bearer_token = Hash::make($service->bearer_token);
}
2021-08-02 11:46:08 +02:00
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);
}
2021-03-14 10:45:42 +01:00
});
static::created(function () {
// flush Eloquent cache
self::flushQueryCache();
});
static::updated(function () {
// flush Eloquent cache
self::flushQueryCache();
});
static::deleted(function () {
// flush Eloquent cache
self::flushQueryCache();
});
2021-03-14 10:45:42 +01:00
}
2021-08-02 11:46:08 +02:00
/**
* Generates a new Api Token
*
* @return string
*/
public function generateApiToken(): string
2021-03-14 10:45:42 +01:00
{
2021-08-02 11:46:08 +02:00
$this->api_token = hash('sha256', Str::uuid());
2021-03-14 10:45:42 +01:00
$this->api_token_date = now();
return $this->api_token;
}
2021-08-02 11:46:08 +02:00
/**
* Generates new bearer credentials
*
* @return string
*/
public function generateBearerToken(): string
2021-03-14 10:45:42 +01:00
{
2021-08-02 11:46:08 +02:00
$this->bearer_token = hash('sha256', Str::uuid());
2021-03-14 10:45:42 +01:00
$this->bearer_token_date = now();
2021-08-02 11:46:08 +02:00
$this->bearer_pwd = hash('ripemd160', Str::uuid());
2021-03-14 10:45:42 +01:00
return $this->bearer_token;
}
/**
* Increment API CRM Stat
2021-03-14 10:45:42 +01:00
*
* @return void
2021-03-14 10:45:42 +01:00
*/
public function incrementStat(): void
2021-03-14 10:45:42 +01:00
{
2021-08-23 21:46:10 +02:00
SetServiceStats::dispatch($this)->onConnection('database');
2021-03-14 10:45:42 +01:00
}
/**
* Get Service from apiToken with its brand
*
* @param string $apiToken
* @return \App\Models\Service
2021-03-14 10:45:42 +01:00
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public static function getFromApiToken(string $apiToken): self
2021-03-14 10:45:42 +01:00
{
$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');
2021-03-14 10:45:42 +01:00
}
/**
* Get Service from bearerToken with its brand
*
* @param string $bearerToken
* @return \App\Models\Service
2021-03-14 10:45:42 +01:00
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public static function getFromBearerToken(string $bearerToken): self
2021-03-14 10:45:42 +01:00
{
$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');
2021-03-14 10:45:42 +01:00
}
/**
2021-08-02 11:46:08 +02:00
* Authenticate from the api token string
2021-03-14 10:45:42 +01:00
*
* @param array $guards
* @param string $apiToken
* @return bool
2021-03-14 10:45:42 +01:00
*/
public function authenticateApi(string $apiToken): bool
2021-03-14 10:45:42 +01:00
{
return $this->api_token and Hash::check($apiToken, $this->api_token) and
((abs(strtotime($this->api_token_date) - time()) * 60) < (config('api.tokenTimeout') * 60));
2021-03-14 10:45:42 +01:00
}
/**
* Brand BelongsTo relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function brand()
{
return $this->belongsTo(Brand::class);
2020-12-01 17:02:10 +01:00
}
/**
* ServiceStat hasMany relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function servicesStats()
{
return $this->hasMany(ServiceStat::class);
2020-12-01 17:02:10 +01:00
}
}