controller = sfContext::getInstance()->getController(); } public function started(): void { $this->setParameter('last_id', null); } public function count(): int { if (!stConfig::getInstance('stAllegroBackend')->get('access_token')) { return 0; } return AllegroAuctionPeer::doCount($this->getCriteria()); } public function execute(int $offset): int { $c = $this->getCriteria(); $lastId = $this->getParameter('last_id'); if ($lastId) { $c->add(AllegroAuctionPeer::ID, $lastId, Criteria::GREATER_THAN); } $c->setLimit(self::LIMIT); $publish = array(); $unpublish = array(); $api = stAllegroApi::getInstance(); /** * @var AllegroAuction $auction */ foreach (AllegroAuctionPeer::doSelect($c) as $auction) { stNewProductOptions::clearStaticPool(); $this->setParameter('last_id', $auction->getId()); $offerLink = $this->getOfferLink($auction->getAuctionId()); try { if (null === $auction->getAuctionId() || null === $auction->getProduct()) { $auction->delete(); $offer = null; } else { try { $offer = $api->getOffer($auction->getAuctionId()); } catch (stAllegroException $e) { $errors = stAllegroApi::getLastErrors(); if ($errors[0]->code == 'NOT_FOUND') { $messages = array(); foreach ($errors as $error) { $messages[] = $error->userMessage; } $auction->delete(); $this->getLogger()->error("Oferta %offer% nie została znaleziona", array( "%offer%" => $offerLink, )); $offer = null; } } } if ($auction->getAllowSync() && $offer) { if ($offer->publication->status == 'ACTIVE' || $offer->publication->status == 'ENDED') { $auction->getProductOptionsArray(); $stock = $auction->getProduct()->getStock(); $config = stConfig::getInstance('stAllegroBackend'); $ok = true; if ($stock > 0) { if ($config->get('offer_sync_product_price')) { $allegroCommission = new AllegroCommission(); $price = $allegroCommission->calculatePrice($auction->getProduct()->getPriceBrutto()); $offer->sellingMode->price->amount = $price; } $offer->stock->available = ceil($stock); $this->getLogger()->info('Synchronizacja oferty %offer%', array('%offer%' => $offerLink)); $response = $api->updateOffer($auction->getAuctionId(), $offer); if ($response->validation->errors) { $ok = false; $messages = array(); foreach ($response->validation->errors as $error) { $messages[] = $error->userMessage; } $this->getLogger()->error("Wystąpił błąd podczas synchronizacji oferty %offer%:\n%error%", array( "%offer%" => $offerLink, "%error%" => implode("\n", $messages), )); } } if ($ok) { if ($offer->publication->status == 'ENDED' && $stock > 0) { $publish[$auction->getAuctionId()] = $offerLink; } elseif ($offer->publication->status == 'ACTIVE' && !$stock) { $unpublish[$auction->getAuctionId()] = $offerLink; } } } } } catch (Exception $e) { $messages = array(); foreach (stAllegroApi::getLastErrors() as $error) { $messages[] = $error->userMessage; } $this->getLogger()->error("Wystąpił błąd podczas synchronizacji oferty %offer%:\n%error%", array( "%offer%" => $offerLink, "%error%" => implode("\n", $messages), )); } if (!$auction->isDeleted()) { if ($offer) { $auction->setEnded(($offer->publication->status == 'ENDED' || isset($unpublish[$auction->getAuctionId()])) && !isset($publish[$auction->getAuctionId()])); } $auction->setRequiresSync(false); $auction->save(); } $offset++; stNewProductOptions::clearStaticPool(); usleep(250000); } if ($unpublish) { $this->getLogger()->info("Zakończenie ofert %offers% z powodu braku produktów na magazynie", array('%offers%' => implode(", ", $unpublish))); $api->publishOffers(array_keys($unpublish), false); } if ($publish) { $this->getLogger()->info("Wznowienie ofert %offers% z powodu ponownej dostępności produktów na magazynie", array('%offers%' => implode(", ", $publish))); $api->publishOffers(array_keys($publish), true); } sleep(1); return $offset; } private function getCriteria(): Criteria { $c = new Criteria(); $c->add(AllegroAuctionPeer::REQUIRES_SYNC, true); $c->addAscendingOrderByColumn(AllegroAuctionPeer::ID); return $c; } private function getOfferLink($offerId): string { $url = $this->controller->genUrl('@stAllegroPlugin?action=edit&id='.$offerId); return sprintf('[%s](%s)', $offerId, $url); } }