<?php
 namespace App\AutoProcessRules; class ProcessRule { protected $idRule; protected $className_recordsList = null; protected $className_execution = null; public function __construct($idRule) { $this->idRule = $idRule; $msgs = new \App\MessagesCollector(); $this->check($msgs); if ($msgs->hasErrorOrWarning()) { throw new \App\MessagesException($msgs); } $ruleEnt = \Entity\AutoProcessRule::select($idRule); $this->className_recordsList = "\\App\\AutoProcessRules\\{$ruleEnt->type}_RecordsList"; $this->className_execution = "\\App\\AutoProcessRules\\{$ruleEnt->type}_Execution"; } protected function check(\App\MessagesCollector $msgs) { if (!$this->idRule) { $msgs->addError('No rule id provided.'); return ; } $ruleEnt = \Entity\AutoProcessRule::select($this->idRule); if (!$ruleEnt->recordExists()) { $msgs->addError('Invalid rule id provided.'); } if ($ruleEnt->deleted or !$ruleEnt->active) { $msgs->addWarning('Rule is deleted or inactive.'); } if ($ruleEnt->maxDailyExecutionsCount) { $dt = \Engine\DateTime::create($ruleEnt->lastExecutionTime); if ($dt->formatDateIso()==TODAY_ISO and $ruleEnt->currentDayExecutionsCount>=$ruleEnt->maxDailyExecutionsCount) { $msgs->addWarning('The rule cannot be executed again today.'); } } } public function setClassName_recordsList($className) { $this->className_recordsList = $className; } public function setClassName_execution($className) { $this->className_execution = $className; } public function process(\App\AutoProcessRules\Logs\Rule $ruleLogs) { $ruleLogs->addLog(\App\AutoProcessRules\Logs\Log::create('info', 'Start executing rule.')); $recordsListObj = new $this->className_recordsList($this->idRule); $recordsList = $recordsListObj->getRecords(); $ruleLogs->addLog( \App\AutoProcessRules\Logs\Log::create('info', sprintf('Number or records to be processed: %d.', count($recordsList)) ) ); $executeRuleObj = new $this->className_execution($this->idRule); $executeRuleObj->setRecords($recordsList); $executeRuleObj->execute($ruleLogs); } }