ldc-crm/app/Models/Service.php
Jérémy Munsch 3619593485 Mise à plat CRM v2
- ajout une colonne new country pour enum
- fix des test v2
- fix du handler
- fix des middleware
- fix du timestamp checker
- fix des stats fix du api handler
- ajout du middleware json only
- remove crm-id du client en faveur du hashcode seulement
- fix du route service provider
- déplacement de la route arcradia en dehors du customer
2021-08-04 13:01:33 +02:00

135 lines
3.6 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Auth\AuthenticationException;
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;
class Service extends Model
{
use HasFactory;
protected $fillable = [
'name'
];
protected static function booted()
{
static::creating(function ($service) {
$service->generateBearerToken();
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);
}
});
}
/**
* 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
{
$serviceStat = ServiceStat::firstOrNew(['date' => date('Y-m-d')]);
$serviceStat->nb_calls += 1;
$serviceStat->service()->associate($this);
$serviceStat->brand()->associate($this->brand);
$serviceStat->saveOrFail();
}
/**
* 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
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public static function getFromBearerToken(string $bearerToken)
{
return static::where('bearer_token', $bearerToken)->with('brand')->firstOrFail();
}
/**
* Authenticate from the api token string
*
* @param array $guards
* @param string $apiToken
* @return void
* @throws \Illuminate\Auth\AuthenticationException
*/
public function authenticateApi(array $guards, string $apiToken)
{
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);
}
/**
* ServiceStat hasMany relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function servicesStats()
{
return $this->hasMany(ServiceStat::class);
}
}