HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/quadcode-jobs/app/Repositories/LocationRepository.php
<?php

namespace App\Repositories;

use App\Models\LeverLocation;
use App\Models\Location;
use App\Models\LocationVacancyRelation;
use App\Models\Vacancy;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;

class LocationRepository
{

    private static function getQuery(): Builder
    {
        return LeverLocation::query();
    }

    public static function getLocations(): Collection
    {
        return self::getQuery()->where('count', '>', 0)->get();
    }

    public static function getCount(): int
    {
        return count(self::getLocations());
    }

    public static function sync(array $locationNames): array
    {
        $locations = [];
        foreach ($locationNames as $title) {
            $locations[] = self::getQuery()->updateOrCreate(['title' => $title], ['title' => $title]);
        }

        return $locations;
    }

    public static function relate(Vacancy $vacancy, array $locations): void
    {
        LocationVacancyRelation::query()->where('vacancy_id', $vacancy->id)->delete();

        foreach ($locations as $location) {
            $relation = new LocationVacancyRelation(['vacancy_id' => $vacancy->id, 'location_id' => $location->id]);
            $relation->save();
        }
    }

    public static function updateVacanciesCount(): void
    {
        $mapLocationsData = OfficeRepository::getOffices();
        $mapLocations = [];
        foreach ($mapLocationsData as $mapLocation) {
            /** @var Location $mapLocation */
            $mapLocation->count_positions = 0;
            $mapLocations[$mapLocation->title] = $mapLocation;
        }

        $locations = LeverLocation::all();
        if ($locations->isEmpty()) {
            return;
        }

        foreach ($locations as $location) {
            $count = $location->vacancies->count();
            $location->update(['count' => $count]);
            $country = OfficeRepository::CYPRUS === $location->title ? OfficeRepository::LIMASSOL : $location->title;
            if (!empty($mapLocations[$country])) {
                $mapLocations[$country]->count_positions += $count;
            }
        }

        foreach ($mapLocations as $mapLocation) {
            $mapLocation->save();
        }
    }

    public static function getListByIds(array $ids): Collection
    {
        return self::getQuery()->whereIn('id', $ids)->get()->keyBy('id');
    }

}