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);
}
}