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/farmfun/reserveren.farmfun.be/app/Komma/Locations/LocationComposer.php
<?php

namespace App\Komma\Locations;

use App\Komma\Kiyoh\KiyohService;
use App\Komma\Products\ProductService;
use Illuminate\View\View;

final class LocationComposer
{
    private static $locations;

    /** @var KiyohService */
    private $kiyohService;

    public function __construct()
    {
        $this->kiyohService = app()->make(KiyohService::class);
    }

    /**
     * Bind data to the view.
     *
     * @param View $view
     * @return void
     */
    public function compose(View $view)
    {
        if (self::$locations === null) {
            $locationService = app(LocationService::class);
            $productService = app(ProductService::class);

            $locations = $locationService->getLocations();

            foreach ($locations as $location) {
                $location->kiyoh = $this->kiyohService->getScoreForLocation($location->id);
                
                if($location->boundProducts->isNotEmpty()) {
                    $location->products = $productService->getActivityProductsWhereIn(
                        $location->boundProducts->pluck('id')->toArray()
                    );
                }
                else $location->products = collect();

            }
            self::$locations = $locations;
        }

        $view->with('composedLocations', self::$locations);
    }
}