108 lines
3.4 KiB
PHP
108 lines
3.4 KiB
PHP
<?php
|
|
|
|
class stAllegroEventTask extends stTask
|
|
{
|
|
public function count(): int
|
|
{
|
|
if (!$this->getParameter('event_count'))
|
|
{
|
|
$this->setParameter('event_count', 1000);
|
|
}
|
|
|
|
return $this->getParameter('event_count');
|
|
}
|
|
|
|
public function execute(int $offset): int
|
|
{
|
|
try
|
|
{
|
|
$events = $this->getEvents();
|
|
|
|
if (empty($events))
|
|
{
|
|
return $this->doCount();
|
|
}
|
|
}
|
|
catch (Throwable $e)
|
|
{
|
|
$this->getLogger()->error("Wystąpił błąd podczas pobierania informacji o zdarzeniach:\n%error%", array('%error%' => stAllegroApi::getLastErrorsAsString()));
|
|
return $this->doCount();
|
|
}
|
|
|
|
foreach ($events as $event)
|
|
{
|
|
switch ($event->type)
|
|
{
|
|
case stAllegroApi::OFFER_ACTIVATED:
|
|
$auction = AllegroAuctionPeer::retrieveByAuctionNumber($event->offer->id);
|
|
|
|
if (null !== $auction)
|
|
{
|
|
$auction->setEnded(false);
|
|
$auction->save();
|
|
|
|
$this->getLogger()->info("Oferta %offer% została oznaczona jako aktywna", array(
|
|
'%offer%' => sprintf('[%s](%s)', $event->offer->id, stAllegroApi::getOfferUrl($event->offer->id)),
|
|
));
|
|
}
|
|
|
|
break;
|
|
|
|
case stAllegroApi::OFFER_ENDED:
|
|
$auction = AllegroAuctionPeer::retrieveByAuctionNumber($event->offer->id);
|
|
|
|
if (null !== $auction)
|
|
{
|
|
$auction->setEnded(true);
|
|
$auction->save();
|
|
|
|
$this->getLogger()->info("Oferta %offer% została oznaczona jako zakończona", array(
|
|
'%offer%' => sprintf('[%s](%s)', $event->offer->id, stAllegroApi::getOfferUrl($event->offer->id)),
|
|
));
|
|
}
|
|
break;
|
|
|
|
case stAllegroApi::OFFER_ARCHIVED:
|
|
$auction = AllegroAuctionPeer::retrieveByAuctionNumber($event->offer->id);
|
|
|
|
if (null !== $auction)
|
|
{
|
|
$auction->delete();
|
|
|
|
$this->getLogger()->info("Oferta %offer% została usunięta ze sklepu, ponieważ nie istnieje już na allegro", array(
|
|
'%offer%' => $event->offer->id,
|
|
));
|
|
}
|
|
break;
|
|
}
|
|
|
|
$offset++;
|
|
|
|
usleep(250000);
|
|
}
|
|
|
|
$this->updateLastEventId($event->id);
|
|
|
|
if ($offset >= $this->doCount())
|
|
{
|
|
$this->setParameter('event_count', $offset + 200);
|
|
$this->doCount(true);
|
|
}
|
|
|
|
return $offset;
|
|
}
|
|
|
|
protected function getEvents(): array
|
|
{
|
|
$api = stAllegroApi::getInstance();
|
|
$config = stConfig::getInstance('stAllegroBackend');
|
|
return $api->getOfferEvents($config->get('last_offer_event_id', null), [stAllegroApi::OFFER_ACTIVATED, stAllegroApi::OFFER_ARCHIVED, stAllegroApi::OFFER_ENDED], 100);
|
|
}
|
|
|
|
protected function updateLastEventId($id)
|
|
{
|
|
$config = stConfig::getInstance('stAllegroBackend');
|
|
$config->set('last_offer_event_id', $id);
|
|
$config->save();
|
|
}
|
|
} |