Files
bx.discount/lib/ElementSpider.php
T
2026-02-04 14:50:08 +03:00

122 lines
2.8 KiB
PHP

<?php
namespace Bx\Discount;
use Wt\Core\Entity\AgentEntity;
class ElementSpider
{
protected $agent;
public function __construct()
{
}
public function next()
{
if(!$this->canDo()){
if($this->isDataExpired()){
$this->reset();
}
return false;
}
$lastId = $this->getLastId();
if($lastId == 0){
$this->setBeginTimestamp();
}
$iBlockId = discountApp()->config('CATALOG_IBLOCK_ID');
$ids = Tools::getElementList($lastId);
if(count($ids) == 0){
$this->setEndTimestamp();
}
$__lastId = 0;
foreach ($ids as $id){
$__lastId = $id;
Tools::setTicketValueFromDiscount($id);
}
$this->setLastId($__lastId);
return true;
}
public function canDo()
{
$resetTimestamp = $this->getResetTimestamp();
$spiderBeginTimestamp = $this->getBeginTimestamp();
$spiderEndTimestamp = $this->getEndTimestamp();
return ($resetTimestamp > $spiderEndTimestamp)
|| ($resetTimestamp > $spiderBeginTimestamp);
}
public function isDataExpired()
{
$spiderEndTimestamp = $this->getEndTimestamp();
$renewalPeriod = discountApp()->config('RENEWAL_PERIOD');
return $spiderEndTimestamp + $renewalPeriod < time();
}
public function getLastId()
{
return (int)discountApp()->setting()->get('last_id', 0);
}
public function setLastId($lastId)
{
discountApp()->setting()->set('last_id', (int)$lastId);
return $this;
}
public function setBeginTimestamp()
{
discountApp()->setting()->set('spider_begin_timestamp', time());
return $this;
}
public function getBeginTimestamp()
{
return (int)discountApp()->setting()->get('spider_begin_timestamp', 0);
}
public function setEndTimestamp()
{
discountApp()->setting()->set('spider_end_timestamp', time());
return $this;
}
public function getEndTimestamp()
{
return (int)discountApp()->setting()->get('spider_end_timestamp', 0);
}
public function setResetTimestamp()
{
discountApp()->setting()->set('spider_reset_timestamp', time());
return $this;
}
public function getResetTimestamp()
{
return (int)discountApp()->setting()->get('spider_reset_timestamp', 0);
}
public function reset()
{
$this->setLastId(0);
$this->setResetTimestamp();
return $this;
}
public function setAgent($agent)
{
$this->agent = $agent;
}
/**
* @return AgentEntity|null
*/
public function getAgent()
{
return $this->agent;
}
}