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/Products/ProductableController.php
<?php

namespace App\Products;

use App\Base\Controller;
use App\Catalog\CatalogService;
use App\Vat\VatService;
use Illuminate\Http\Resources\Json\Resource as JsonResource;
use Komma\KMS\Globalization\RegionInfoInterface;
use Symfony\Component\HttpKernel\Exception\HttpException;

abstract class ProductableController extends Controller
{
    protected $pagePrefix = '';

    /** @var CatalogService */
    protected $catalogService;

    /** @var VatService */
    protected $rateService;

    protected $showViewName = '';

    public function __construct()
    {
        parent::__construct();
        $this->catalogService = app(CatalogService::class);
        $this->rateService =  new VatService();
    }

    /**
     * @return HttpException|\Illuminate\Contracts\View\View
     * @throws \Exception
     */
    public function index()
    {
        return redirect()->to('/');
    }

    /**
     * @param AbstractProductable $productable
     *
     * @return \Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse
     */
    public function show(AbstractProductable $productable)
    {
        //Eager load some relations.
        $productable->load('translation', 'images', 'documents', 'properties.key.translation', 'properties.values.translation');

        return (!request()->ajax()) ? $this->regularShow($productable) : $this->ajaxShow($productable);
    }

    protected function regularShow(AbstractProductable $productable) {
        //Determine the product image
        $productImageUrl = $productable->images->where('medium_image_url', '!=', null)->first()->medium_image_url ?? url('/img/placeholders/productable_placeholder.svg');

        //TODO Can we put this at a higher level?
        //Get regional information to format prices properly
        $regionInfo = app(RegionInfoInterface::class);

        //Add vat information in the productable using the transient vat properties. @see TransientVatProperties trait.
        $this->rateService->calculateVatForModelWithVatScenarioEnum($productable);

        //TODO check if we are still going to use this
        //$productableCategoryParent = $this->catalogService->getProductableParentCategoryUsingReferrer($productable);

        // Todo improve link class
        $page = $this->links->products->node;

        // Return view
        return view($this->showViewName,[
            'page' => $page,
            'productable' => $productable,
            'productableImageUrl' => $productImageUrl,
            'links' => $this->links,
            'regionInfo' => $regionInfo,
        ]);
    }

    protected function ajaxShow(AbstractProductable $productable) {
        $regionInfo = app(RegionInfoInterface::class);

        //Add vat information in the productable using the transient vat properties. @see TransientVatProperties trait.
        $this->rateService->calculateVatForModelWithVatScenarioEnum($productable);

        $priceFormatted = $regionInfo->getCurrencySymbol().$regionInfo->getNumberFormat()->centsToCurrency($productable->price_inc, true, true);
        if($productable->productGroupBehaviour->name !== 'and') $priceFormatted = __('productable.starting_at').' '.$priceFormatted;

        return response()->json($this->makeResource($productable));
    }

    abstract function makeResource(AbstractProductable $productable): JsonResource;
}