File: /var/www/quadcode-jobs/app/Console/Commands/Lever.php
<?php
namespace App\Console\Commands;
use App\Repositories\CategoryRepository;
use App\Repositories\LocationRepository;
use App\Repositories\VacancyRepository;
use Exception;
use Illuminate\Console\Command;
use Lever\Api\Client;
use Symfony\Component\Console\Command\Command as CommandAlias;
class Lever extends Command
{
protected $signature = 'lever:update';
protected $description = 'Update vacancies';
const LIMIT = 50;
private function mapVacancy($posting): array
{
return [
'external_id' => $posting['id'],
'title' => $posting['text'],
'state' => $posting['state'],
'commitment' => $posting['categories']['commitment'],
'workplace_type' => $posting['workplaceType'],
'department' => $posting['categories']['department'],
'team' => $posting['categories']['team'],
'location' => $posting['categories']['location'],
'locations' => $posting['categories']['allLocations'],
'country' => $posting['country'] ?? '',
'url' => $posting['urls']['show'] ?? null,
];
}
public function handle(): int
{
$this->info($this->description);
$token = env('LEVER_TOKEN');
if (empty($token)) {
$this->error('LEVER_TOKEN need to set');
return CommandAlias::FAILURE;
}
try {
$leverClient = new Client(['authToken' => $token]);
$result = $leverClient->get('/postings', ['limit' => self::LIMIT]);
$postings = $result['data'];
while ($result['hasNext']) {
$result = $leverClient->get('/postings', ['limit' => self::LIMIT, 'offset' => $result['next']]);
$postings = array_merge($postings, $result['data']);
}
$this->line('Found vacancies: ' . count($postings));
foreach ($postings as $posting) {
VacancyRepository::sync($this->mapVacancy($posting));
}
CategoryRepository::updateVacanciesCount();
LocationRepository::updateVacanciesCount();
} catch (Exception $exception) {
$this->error($exception->getMessage());
return CommandAlias::FAILURE;
}
return CommandAlias::SUCCESS;
}
}