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/Availability/Queries/SearchAmountQuery.php
<?php

namespace App\Komma\Availability\Queries;

use App\Komma\Availability\AvailabilityService;
use App\Komma\SearchAmountOfPersons\SearchAmountOfPersonsService;

final class SearchAmountQuery extends AbstractAvailabilityProductFilterQuery
{
    /** @var SearchAmountOfPersonsService */
    private $searchAmountOfPersonsService;

    public function __construct()
    {
        $this->searchAmountOfPersonsService = app(SearchAmountOfPersonsService::class);
    }

    /**
     * Check if we should run the filter
     *
     * @return bool
     */
    public function shouldRunFilter(): bool
    {
        return session()->get(AvailabilityService::SEARCH_AMOUNT_KEY, null) !== null;
    }

    public function filter($query)
    {
        $searchAmountOption = $this->searchAmountOfPersonsService->getOptionById(session()->get(AvailabilityService::SEARCH_AMOUNT_KEY));

        return $query->where('products.minimum_amount_of_persons', '<=', $searchAmountOption->maximum)
            ->where(function ($subQuery) use ($searchAmountOption) {
                $subQuery->where('products.maximum_amount_of_persons', '>=', $searchAmountOption->minimum)
                    ->orWhere('products.maximum_amount_of_persons', '=', 0);
            });
    }
}