51 lines
1.2 KiB
PHP
Executable File
51 lines
1.2 KiB
PHP
Executable File
<?php
|
|
|
|
use App\Models\Cart;
|
|
use App\Models\PersonalAccessToken;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\Cookie;
|
|
|
|
if (! function_exists('getAuthUser')) {
|
|
function getAuthUser()
|
|
{
|
|
$bearer = request()->bearerToken();
|
|
|
|
if ($bearer) {
|
|
[$personalAccessTokenId, $token] = explode('|', $bearer, 2);
|
|
$personalAccessToken = PersonalAccessToken::find($personalAccessTokenId);
|
|
|
|
if ($personalAccessToken && hash_equals($personalAccessToken->token, hash('sha256', $token))) {
|
|
return User::find($personalAccessToken->tokenable_id);
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
if (! function_exists('getCart')) {
|
|
function getCart($productId = null)
|
|
{
|
|
$user = getAuthUser();
|
|
|
|
if ($user) {
|
|
$cart = Cart::where('user_id', $user->id)->get();
|
|
} else {
|
|
$cart = Cart::where('token', Cookie::get('cart_token'))->get();
|
|
}
|
|
|
|
if ($productId) {
|
|
return $cart->where('product_id', $productId)->first();
|
|
}
|
|
|
|
return $cart;
|
|
}
|
|
}
|
|
|
|
if (! function_exists('ceiling')) {
|
|
function ceiling(float|int $num, $significance = 1)
|
|
{
|
|
return (ceil($num / $significance)) * $significance;
|
|
}
|
|
}
|