HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/shop.komma.nl/app/Addresses/AddressService.php
<?php

namespace App\Addresses;


use App\Addresses\Models\Address;
use App\Users\SiteUserInterface;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Request;
use Komma\KMS\Core\ModelService;

class AddressService extends ModelService implements AddressServiceInterface
{
    function __construct()
    {
        parent::__construct();
        $this->setModelClassName(Address::class);
    }

    public function getAddressFromInput(): AddressInterface
    {
        /** @var AddressInterface $address */
        $address = app(AddressInterface::class);
        $address->fill([
            'street'            => Request::get('street'),
            'house_number'      => Request::get('house_number'),
            'postal_code'       => Request::get('postal_code'),
            'city'              => Request::get('city'),
            'country_iso3'      => Request::get('country'),
        ]);

        return $address;
    }

    /**
     * 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()->guard('site')->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'),
                'country_iso3'      => Request::get('invoice_country'),
            ]);

            if(Request::has('invoice_house_number_addition')) $addressForInvoice->house_number += Request::get('invoice_house_number_addition');
        }

        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::guard('site')->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'),
                'country_iso3'      => Request::get('shipping_country'),
            ]);
        }

        return $addressForShipping;
    }

    /**
     * Saves an address for a user.
     *
     * @param AddressInterface $address
     * @param SiteUserInterface $user
     * @throws \InvalidArgumentException
     *-
     * @return Address
     */
    public function saveAddressForUser(AddressInterface $address, SiteUserInterface $user):AddressInterface {
        $address->siteUser()->associate($user);
        $address->save();
        return $address;
    }

    /**
     * @param AddressInterface $addressA
     * @param AddressInterface $addressB
     *
     * @return bool
     */
    public function addressesAreEqual(AddressInterface $addressA, AddressInterface $addressB): bool {
        //Check if the addresses are equal. Case insensitive
        if($addressB) {
            $addressAttributes = $addressA->getAttributes();
            $possibleEqualAddressAttributes = $addressB->getAttributes();

            if(array_key_exists('id', $addressAttributes)) unset($addressAttributes['id']);
            if(array_key_exists('id', $possibleEqualAddressAttributes)) unset($possibleEqualAddressAttributes['id']);

            foreach($addressAttributes as $addressAttribute => $addressAttributeValue) {
                if(strtolower($addressAttributeValue) !== strtolower($possibleEqualAddressAttributes[$addressAttribute])) {
                    return false;
                }
            }

            return true;
        }
        return false;
    }

    /**
     * @throws \Exception
     *
     * @param AddressInterface $address
     */
    public function delete(AddressInterface $address) {
        $this->destroyForModel($address);
        $address->delete();
    }

    /**
     * @param Model $model
     * @return Model
     */
    public function destroyForModel(Model $model): Model
    {
        /** @var AddressInterface $model */
        /** @var SiteUserInterface $siteUser */

        //Get the users that have the address (model) as their shipping_address_id or invoice_address_id.
        $siteUsers = app(SiteUserInterface::class)::where('invoice_address_id', '=', $model->id)
            ->orWhere('shipping_address_id', '=', $model->id)->get();

        //Make sure that the site users have a shipping and invoice address set when this address is removed
        //and was one of them.
        foreach($siteUsers as $siteUser) {
            /** @var SiteUserInterface $siteUser */
            if($siteUser->shipping_address_id === $model->id) $siteUser->shipping_address_id = $siteUser->account_address_id;
            if($siteUser->invoice_address_id === $model->id) $siteUser->invoice_address_id = $siteUser->account_address_id;
            $siteUser->save();
        }

        return $model;
    }
}