ldc-crm/app/Models/Service.php

156 lines
4.4 KiB
PHP
Raw Normal View History

2020-12-01 17:02:10 +01:00
<?php
namespace App\Models;
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;
2020-12-01 17:02:10 +01:00
class Service extends Model
{
use HasFactory;
2021-03-14 10:45:42 +01:00
protected $fillable = [
'name'
];
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
});
}
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-10 10:20:52 +02:00
$serviceStat = ServiceStat::firstOrNew(['date' => date('Y-m-d')]);
if (!$serviceStat->exists) {
$serviceStat->service()->associate($this);
$serviceStat->brand()->associate($this->brand);
}
$serviceStat->nb_calls += 1;
$serviceStat->saveOrFail();
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::all();
foreach ($services as $service) {
if (Hash::check($apiToken, $service->api_token)) {
return $service;
}
}
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();
foreach ($services as $service) {
if (Hash::check($bearerToken, $service->bearer_token)) {
return $service;
}
}
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 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
}
}