2020-12-01 17:02:10 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2021-08-23 19:38:16 +02:00
|
|
|
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;
|
2021-09-08 16:43:44 +02:00
|
|
|
use Kwaadpepper\CrudPolicies\Enums\CrudAction;
|
|
|
|
|
use Kwaadpepper\CrudPolicies\Enums\CrudType;
|
|
|
|
|
use Kwaadpepper\CrudPolicies\Traits\IsCrudModel;
|
2020-12-01 17:02:10 +01:00
|
|
|
|
|
|
|
|
class Service extends Model
|
|
|
|
|
{
|
2021-09-08 16:43:44 +02:00
|
|
|
use IsCrudModel;
|
2020-12-01 17:02:10 +01:00
|
|
|
use HasFactory;
|
2021-08-23 20:03:17 +02:00
|
|
|
|
2021-09-08 16:43:44 +02:00
|
|
|
protected $crudLabelColumn = 'name';
|
|
|
|
|
|
|
|
|
|
protected $crudValueColumn = 'id';
|
|
|
|
|
|
2021-08-23 20:03:17 +02:00
|
|
|
protected $cacheFor = 0;
|
2020-12-01 17:02:10 +01:00
|
|
|
|
2021-03-14 10:45:42 +01:00
|
|
|
protected $fillable = [
|
2021-09-15 12:04:46 +02:00
|
|
|
'name', 'brand_id'
|
2021-03-14 10:45:42 +01:00
|
|
|
];
|
|
|
|
|
|
2021-08-23 20:03:17 +02:00
|
|
|
public function __construct(array $attributes = [])
|
|
|
|
|
{
|
|
|
|
|
$this->cacheFor = config('api.servicesDBCache') * 60;
|
2021-09-08 16:43:44 +02:00
|
|
|
|
|
|
|
|
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' => [
|
2021-09-09 15:15:23 +02:00
|
|
|
'label' => Str::ucfirst(trans('models.classes.brand')),
|
2021-09-08 16:43:44 +02:00
|
|
|
'type' => CrudType::belongsTo(),
|
|
|
|
|
'belongsTo' => Brand::class,
|
|
|
|
|
'actions' => [
|
|
|
|
|
CrudAction::viewAny(),
|
2021-09-14 13:13:36 +02:00
|
|
|
CrudAction::view(),
|
|
|
|
|
CrudAction::create(),
|
|
|
|
|
CrudAction::update()
|
2021-09-08 16:43:44 +02:00
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
];
|
2021-08-23 20:03:17 +02:00
|
|
|
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()
|
|
|
|
|
{
|
2021-08-09 17:35:28 +02:00
|
|
|
static::creating(function (self $service) {
|
2021-03-14 10:45:42 +01:00
|
|
|
$service->generateBearerToken();
|
2021-08-09 17:35:28 +02:00
|
|
|
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);
|
|
|
|
|
}
|
2021-08-09 17:35:28 +02:00
|
|
|
if (Hash::needsRehash($service->api_token)) {
|
|
|
|
|
$service->api_token = Hash::make($service->api_token);
|
|
|
|
|
}
|
2021-08-02 11:46:08 +02:00
|
|
|
});
|
2021-08-09 17:35:28 +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);
|
|
|
|
|
}
|
2021-08-09 17:35:28 +02:00
|
|
|
if (Hash::needsRehash($service->api_token)) {
|
|
|
|
|
$service->api_token = Hash::make($service->api_token);
|
|
|
|
|
}
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-08-04 09:42:48 +02:00
|
|
|
* Increment API CRM Stat
|
2021-03-14 10:45:42 +01:00
|
|
|
*
|
2021-08-04 09:42:48 +02:00
|
|
|
* @return void
|
2021-03-14 10:45:42 +01:00
|
|
|
*/
|
2021-08-04 09:42:48 +02:00
|
|
|
public function incrementStat(): void
|
2021-03-14 10:45:42 +01:00
|
|
|
{
|
2021-10-29 13:55:18 +02:00
|
|
|
SetServiceStats::dispatch($this)->afterResponse()
|
2021-11-05 16:22:36 +01:00
|
|
|
->onConnection('database')->onQueue('stats');
|
2021-03-14 10:45:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get Service from apiToken with its brand
|
|
|
|
|
*
|
|
|
|
|
* @param string $apiToken
|
2021-08-09 17:35:28 +02:00
|
|
|
* @return \App\Models\Service
|
2021-03-14 10:45:42 +01:00
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
|
|
|
|
*/
|
2021-08-09 17:35:28 +02:00
|
|
|
public static function getFromApiToken(string $apiToken): self
|
2021-03-14 10:45:42 +01:00
|
|
|
{
|
2021-08-23 20:03:17 +02:00
|
|
|
$services = static::with([
|
|
|
|
|
'brand' => function ($query) {
|
|
|
|
|
return $query->cacheTags(["service:author"]);
|
|
|
|
|
},
|
|
|
|
|
])->get();
|
|
|
|
|
/** @var \App\Models\Service $service */
|
2021-08-09 17:35:28 +02:00
|
|
|
foreach ($services as $service) {
|
2021-08-16 13:20:03 +02:00
|
|
|
if ($service->api_token and Hash::check($apiToken, $service->api_token)) {
|
2021-08-23 20:03:17 +02:00
|
|
|
return $service->load([
|
|
|
|
|
'brand' => function ($query) use ($service) {
|
|
|
|
|
return $query->cacheTags(["service:{$service->id}:brand"]);
|
|
|
|
|
},
|
|
|
|
|
]);
|
2021-08-09 17:35:28 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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
|
2021-08-04 09:42:48 +02:00
|
|
|
* @return \App\Models\Service
|
2021-03-14 10:45:42 +01:00
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
|
|
|
|
*/
|
2021-08-09 17:35:28 +02:00
|
|
|
public static function getFromBearerToken(string $bearerToken): self
|
2021-03-14 10:45:42 +01:00
|
|
|
{
|
2021-08-09 17:35:28 +02:00
|
|
|
$services = static::all();
|
2021-08-23 20:03:17 +02:00
|
|
|
/** @var \App\Models\Service $service */
|
2021-08-09 17:35:28 +02:00
|
|
|
foreach ($services as $service) {
|
2021-08-16 13:20:03 +02:00
|
|
|
if ($service->bearer_token and Hash::check($bearerToken, $service->bearer_token)) {
|
2021-08-23 20:03:17 +02:00
|
|
|
return $service->load([
|
|
|
|
|
'brand' => function ($query) use ($service) {
|
|
|
|
|
return $query->cacheTags(["service:{$service->id}:brand"]);
|
|
|
|
|
},
|
|
|
|
|
]);
|
2021-08-09 17:35:28 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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
|
2021-08-09 17:35:28 +02:00
|
|
|
* @return bool
|
2021-03-14 10:45:42 +01:00
|
|
|
*/
|
2021-08-09 17:35:28 +02:00
|
|
|
public function authenticateApi(string $apiToken): bool
|
2021-03-14 10:45:42 +01:00
|
|
|
{
|
2021-08-16 13:20:03 +02:00
|
|
|
return $this->api_token and Hash::check($apiToken, $this->api_token) and
|
2021-08-09 17:35:28 +02:00
|
|
|
((abs(strtotime($this->api_token_date) - time()) * 60) < (config('api.tokenTimeout') * 60));
|
2021-03-14 10:45:42 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-28 12:04:49 +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
|
|
|
}
|
|
|
|
|
|
2021-02-28 12:04:49 +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
|
|
|
}
|
|
|
|
|
}
|