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/centrum8a/centrum8a.com/app/KommaApp/Shop/Checkout/CheckoutService.php
<?php


namespace App\KommaApp\Shop\Checkout;


use App\KommaApp\Shop\Cart\ShoppingCartItemInterface;
use App\KommaApp\Shop\Orders\Kms\OrderServiceInterface;
use App\KommaApp\Shop\Orders\Models\Order;
use App\KommaApp\Shop\Products\Product\Product;
use App\KommaApp\Shop\Products\Product\ProductServiceInterface;
use App\KommaApp\Shop\Products\ProductComposite\ProductComposite;
use App\KommaApp\Shop\Products\ProductComposite\ProductCompositeService;
use App\KommaApp\Shop\Products\ProductComposite\ProductCompositeServiceInterface;
use App\KommaApp\Shop\Products\ProductGroup\ProductGroup;
use App\KommaApp\Shop\Products\ProductGroup\ProductGroupService;
use App\KommaApp\Shop\Products\ProductGroup\ProductGroupServiceInterface;
use App\KommaApp\Users\Models\User;

class CheckoutService implements CheckoutServiceInterface
{
    /** @var OrderServiceInterface $orderService */
    private $orderService;

    /** @var ProductServiceInterface $productService */
    private $productService;

    /** @var ProductGroupService  $productGroupService*/
    private $productGroupService;

    /** @var ProductCompositeService $productCompositeService */
    private $productCompositeService;


    public function __construct()
    {
        $this->orderService = \App::make(OrderServiceInterface::class);
        $this->productService = \App::make(ProductServiceInterface::class);
        $this->productGroupService = \App::make(ProductGroupServiceInterface::class);
        $this->productCompositeService = \App::make(ProductCompositeServiceInterface::class);
    }

    /**
     * @param ShoppingCartItemInterface[] $shoppingCartItems
     * @param User $customer
     * @return Order|null
     * @throws \Throwable
     */
    public function createOrderFromShoppingCartItemsForCustomer($shoppingCartItems, User $customer): ?Order
    {
        foreach($shoppingCartItems as $shoppingCartItem) if(!is_a($shoppingCartItem, ShoppingCartItemInterface::class)) throw new \RuntimeException('The given "ShoppingCartItem" wasn\'t an instance of: '.ShoppingCartItemInterface::class);

        $order = null;
        \DB::transaction(function () use ($shoppingCartItems, $customer, &$order) {
            /** @var Order $order */
            $order = $this->orderService->newModel();
            $order->customer()->associate($customer);
            $order->save();

            foreach ($shoppingCartItems as $shoppingCartItem) {
                $productable = $shoppingCartItem->getProductable();

                $shoppingCartItem->getTotal(true);
                $shoppingCartItem->getAmount();
                $productableClassName = get_class($productable);

                switch ($productableClassName) {
                    case Product::class:
                        /** @var Product $productable */
                        $this->productService->createOrderedProductFromProduct($productable, $order);
                        break;
                    case ProductGroup::class:
                        /** @var ProductGroup $productable */
                        $this->productGroupService->createOrderedProductGroupFromProductGroup($productable, $order);
                        break;
                    case ProductComposite::class;
                        /** @var ProductComposite $productable */
                        $this->productCompositeService->createOrderedProductCompositeProductComposite($productable, $order);
                        break;
                    default:
                        return null;
                }
            };

            $order = $this->orderService->fillOrderWithCustomerInfo($order, $customer);
            $this->orderService->calculateTotalOrderPrice($order);

            $order->save();
        });

        return $order;
    }
}