File: D:/HostingSpaces/SBogers10/kooken.komma.pro/app/Addresses/AddressService.php
<?php
namespace App\Addresses;
use App\Addresses\Models\Address;
use Illuminate\Support\Facades\Request;
use Komma\KMS\Core\ModelService;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
class AddressService extends ModelService
{
function __construct()
{
parent::__construct();
$this->setModelClassName(Address::class);
}
/**
* Use the input field "invoice_address" to retrieve an Address from the database when the value is not -1.
* Or when it is -1, make a new address (that does not exist in the database) and return that.
* The address is filled by using fields:
* "invoice_street","invoice_house_number","invoice_postal_code","invoice_city","invoice_telephone","invoice_country"
*
* @return Address|null Return the found address or null if it could not be found;
*/
public function getAddressForInvoiceFromInput(): ?Address
{
$addressForInvoiceId = Request::get('invoice_address'); //The id of an Address model that can be converted to a InvoiceAddress model. Or -1 when a new address was given
$addressForInvoice = null;
if($addressForInvoiceId !== '-1' && $addressForInvoiceId !== null) {
$addressForInvoice = Address::where('id', '=', $addressForInvoiceId)->whereHas('siteUsers', function (Builder $query) {
$query->where('addressable_id', '=', Auth::user()->id);
})->first();
}
if(!$addressForInvoice) {
$addressForInvoice = new Address([
'street' => Request::get('invoice_street'),
'house_number' => Request::get('invoice_house_number'),
'postal_code' => Request::get('invoice_postal_code'),
'city' => Request::get('invoice_city'),
'telephone' => Request::get('invoice_phone'),
'country_iso3' => Request::get('invoice_country'),
]);
}
return $addressForInvoice;
}
/**
* Use the input field "shipping_address" to retrieve an Address from the database when the value is not -1.
* Or when it is -1, make a new address (that does not exist in the database) and return that.
* The address is filled by using fields:
* "shipping_street","shipping_house_number","shipping_postal_code","shipping_city","shipping_telephone","shipping_country"
*
* @return Address|null Return the found address or null if it could not be found;
*/
public function getAddressForShippingFromInput(): ?Address
{
$addressForShippingId = Request::get('shipping_address'); //The id of an Address model that can be converted to a ShippingAddress model. Or -1 when a new address was given
$addressForShipping = null;
if($addressForShippingId !== '-1' && $addressForShippingId !== null) {
$addressForShipping = Address::where('id', '=', $addressForShippingId)->whereHas('siteUsers', function (Builder $query) {
$query->where('addressable_id', '=', \Auth::user()->id);
})->first();
}
if(!$addressForShipping) {
$addressForShipping = new Address([
'street' => Request::get('shipping_street'),
'house_number' => Request::get('shipping_house_number'),
'postal_code' => Request::get('shipping_postal_code'),
'city' => Request::get('shipping_city'),
'telephone' => Request::get('shipping_phone'),
'country' => Request::get('shipping_country'),
]);
}
return $addressForShipping;
}
/**
* Saves an address for a user if it does not exist already.
*
* You can also specify a possible equal address. If that address is
* the same as the address, that possible equal address will be returned
* and the original address will be ignored. The possible equal address must
* exist in the database because this method only returns saved addresses.
*
* @param Address $address
* @param Authenticatable $user
* @param Address $possibleEqualAddress
* @throws \InvalidArgumentException
*
* @return Address
*/
public function saveAddressForUser(Address $address, Authenticatable $user, Address $possibleEqualAddress = null):Address {
if($address->exists) return $address;
if($possibleEqualAddress && $possibleEqualAddress->exists === false) throw new \InvalidArgumentException('AddressService:saveAddressForUser. The possibleEqual address was given but does not exist in the database. Make sure you save it first');
//Check if the addresses are equal. Case insensitive
if($possibleEqualAddress) {
$addressAttributes = $address->getAttributes();
$possibleEqualAddressAttributes = $possibleEqualAddress->getAttributes();
$equal = true;
foreach($addressAttributes as $addressAttribute => $addressAttributeValue) {
if(strtolower($addressAttributeValue) !== strtolower($possibleEqualAddressAttributes[$addressAttribute])) {
$equal = false;
break;
}
}
if($equal) return $possibleEqualAddress;
}
$address->save();
$user->addresses()->attach($address);
return $address;
}
}