Files
opensem/app/Repositories/Shop/Customers.php
2022-08-18 18:20:44 +02:00

176 lines
4.6 KiB
PHP

<?php
namespace App\Repositories\Shop;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use Laravolt\Avatar\Avatar;
use App\Repositories\Core\File;
use App\Models\Shop\Customer;
class Customers
{
public static function getAvatar($id = false)
{
$customer = $id ? self::get($id) : self::getAuth();
$file = self::makeAvatarFilename($customer);
if (!File::checkFile($file)) {
self::createAvatar($customer);
}
return self::getPublic(self::getAvatarFilename($customer));
}
public static function createAvatar($customer)
{
$filename = self::makeAvatarFilename($customer);
$name = $customer->first_name . ' ' . $customer->last_name;
$avatar = new Avatar();
$ret = $avatar->create($name)
->setBackground('#F2B90F')
->setForeground('#335012')
->setBorder(1, '#28a745')
->setFontFamily('Roboto Condensed')
->setDimension(36)
->setFontSize(16)
->save($filename);
return $ret;
}
public static function makeAvatarFilename($customer)
{
$path = storage_path(self::getStorage());
if (File::checkDirOrCreate($path)) {
$filename = $path . self::getAvatarFilename($customer);
}
return $filename ?? false;
}
public static function getAvatarFilename($customer)
{
return 'user-' . $customer->uuid . '.png';
}
public static function getAddresses($id = false)
{
$customer = self::getWithAddresses($id);
return $customer ? $customer->addresses : false;
}
public static function getWithAddresses($id = false)
{
return self::get($id, 'addresses');
}
public static function getName($id = false)
{
$user = $id ? self::get($id) : self::getAuth();
return $user ? $user->first_name . ' ' . $user->last_name : '';
}
public static function getAuth()
{
return self::guard()->user();
}
public static function getId()
{
return self::guard()->id();
}
public static function isConnected()
{
return self::guard()->check();
}
public static function get($id = false, $relations = false)
{
$id = $id ? $id : self::getId();
return $id ? ($relations ? Customer::with($relations)->findOrFail($id) : Customer::findOrFail($id)) : false;
}
public static function edit($id)
{
$customer = self::get($id, 'addresses');
$data = $customer->toArray();
$data['deliveries'] = $customer->deliveries->pluck('id')->toArray();
return $data;
}
public static function storeFull($data)
{
$deliveries = $data['deliveries'];
$addresses = $data['addresses'];
unset($data['deliveries']);
unset($data['addresses']);
$customer = self::store($data);
self::storeDeliveries($customer, $deliveries);
self::storeAddresses($customer, $addresses);
return $customer->id;
}
public static function store($data)
{
return ($data['id'] ?? false) ? self::update($data) : self::create($data);
}
public static function storeDeliveries($customer, $deliveries)
{
if (!$deliveries) {
return false;
}
$deliveries = collect($deliveries)->transform(
function ($item, $key) {
return (int) $item;
}
)->toArray();
return $customer->deliveries()->sync($deliveries);
}
public static function storeAddresses($customer, $addresses)
{
foreach ($addresses as $address) {
$address['customer_id'] = $customer->id;
CustomerAddresses::store($address);
}
}
public static function create($data)
{
$data['uuid'] = Str::uuid()->toString();
return Customer::create($data);
}
public static function update($data, $id = false)
{
$id = $id ? $id : $data['id'];
$customer = self::get($id);
$customer->update($data);
return $customer;
}
public static function delete($id)
{
return Customer::destroy($id);
}
public static function getStorage($filename = false)
{
$path = '/app/public/Customers/';
return $filename ? $path . $filename : $path;
}
public static function getPublic($filename = false)
{
$path = '/storage/Customers/';
return $filename ? $path . $filename : $path;
}
public static function guard()
{
return Auth::guard('customer');
}
}