ldc-crm/app/Models/Service.php

136 lines
3.6 KiB
PHP
Raw Normal View History

2020-12-01 17:02:10 +01:00
<?php
namespace App\Models;
2021-03-14 10:45:42 +01:00
use Illuminate\Auth\AuthenticationException;
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;
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 ($service) {
$service->generateBearerToken();
2021-08-02 11:46:08 +02:00
if (Hash::needsRehash($service->bearer_pwd)) {
$service->bearer_pwd = Hash::make($service->bearer_pwd);
}
});
static::updating(function ($service) {
if (Hash::needsRehash($service->bearer_pwd)) {
$service->bearer_pwd = Hash::make($service->bearer_pwd);
}
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
{
$serviceStat = ServiceStat::firstOrNew(['date' => date('Y-m-d')]);
$serviceStat->nb_calls += 1;
$serviceStat->service()->associate($this);
$serviceStat->brand()->associate($this->brand);
$serviceStat->saveOrFail();
2021-03-14 10:45:42 +01:00
}
/**
* Get Service from apiToken with its brand
*
* @param string $apiToken
* @return \App\Models\Service|null
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public static function getFromApiToken(string $apiToken)
{
return static::where('api_token', $apiToken)->with('brand')->firstOrFail();
}
/**
* 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)
{
return static::where('bearer_token', $bearerToken)->with('brand')->firstOrFail();
}
/**
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 void
2021-08-02 11:46:08 +02:00
* @throws \Illuminate\Auth\AuthenticationException
2021-03-14 10:45:42 +01:00
*/
2021-08-02 11:46:08 +02:00
public function authenticateApi(array $guards, string $apiToken)
2021-03-14 10:45:42 +01:00
{
if ($this->api_token !== $apiToken) {
throw new AuthenticationException('Unauthenticated.', $guards);
}
if ((abs(strtotime($this->api_token_date) - time()) * 60) > (config('api.tokenTimeout') * 60)) {
throw new AuthenticationException('Unauthenticated.', $guards);
}
}
/**
* 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
}
}