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/farmfun.komma.pro/app/Komma/Locations/LocationService.php
<?php

namespace App\Komma\Locations;

use App\Komma\Base\Service;
use App\Komma\Kiyoh\KiyohService;
use App\Komma\Kms\Core\Attributes\Models\SelectOptionInterface;
use App\Komma\Locations\Models\Location;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;

final class LocationService extends Service
{
    private static $locations;

    public function __construct()
    {
        parent::__construct();

        // Set options first if needed
        if (! self::$locations) {
            $this->setLocations();
        }
    }

    public function setLocations()
    {
        self::$locations = Location::with('translation', 'images', 'availability')
            ->where('active', 1)
            ->has('translation')
            ->has('availability')
            ->get();

        /** @var KiyohService $kiyohService */
        $kiyohService = app()->make(KiyohService::class);
        foreach (self::$locations as $location) {
            $location->kiyoh = $kiyohService->getScoreForLocation($location->id);
        }
    }

    public function getLocations()
    {
        return self::$locations;
    }

    public function getLocationById($id)
    {
        $location = self::$locations->where('id', $id)->first();
        if (! isset($location)) {
            throw new \OutOfBoundsException(self::class.': No location found by id: '.$id.'. Or it is inactive or has no translation.');
        }

        return $location;
    }

    public function getLocationBySlug($slug)
    {
        $location = self::$locations->where('translation.slug', $slug)->first();
        if (! isset($location)) {
            throw new \OutOfBoundsException(self::class.': No location found byslug: '.$slug.'. Or it is inactive or has no translation.');
        }

        return $location;
    }

    /**
     * Get other locations ids then provided location
     * Preferred within the same province, else filled out with other locations with a province
     *
     * @param Location $location
     * @param int $total
     * @return Collection
     */
    public function getOtherLocationIds(Location $location, int $total = 3): Collection
    {
        $sameProvinceLocations = self::$locations->where('id', '!=', $location->id)
            ->where('province_page_id', '=', $location->province_page_id)
            ->shuffle()
            ->take($total)
            ->values();

        if ($sameProvinceLocations->count() == $total) {
            return $sameProvinceLocations;
        }

        $excludedIds = $sameProvinceLocations->pluck('id')->toArray();
        $excludedIds[] = $location->id;

        $otherLocationsWithProvince = self::$locations->whereNotIn('id', $excludedIds)
            ->where('province_page_id', '!=', null)
            ->shuffle()
            ->take($total - $sameProvinceLocations->count());

        return $sameProvinceLocations->merge($otherLocationsWithProvince)
            ->pluck('id');
    }

    public function getCountries(): Collection
    {
        $options = collect([
            (App::make(SelectOptionInterface::class))
                ->setContent(__('kms/global.none'))
                ->setHtmlContent(__('kms/global.none'))
                ->setValue(null),
        ]);

        $options->push(
            (App::make(SelectOptionInterface::class))
                ->setContent('Nederland')
                ->setHtmlContent('Nederland')
                ->setValue('NL')
        );

        $options->push(
            (App::make(SelectOptionInterface::class))
                ->setContent('Belgiƫ')
                ->setHtmlContent(' Belgiƫ')
                ->setValue('BE')
        );

        return $options;
    }

}