Files
kupshop/bundles/External/FlexiBeeBundle/Util/FlexiBeeApiWorker.php
2025-08-02 16:30:27 +02:00

104 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace External\FlexiBeeBundle\Util;
use AbraFlexi\Exception;
use AbraFlexi\RO;
use AbraFlexi\RW;
use External\FlexiBeeBundle\Exception\FlexiBeeException;
/**
* Class FlexiBeeApiWorker.
*
* @property $rowCount
* @property $urlParams
*
* @method updateColumnsInfo($columnsInfo = null, $evidence = null)
* @method insertToAbraFlexi($data = null)
* @method getAllFromAbraFlexi($conditions = null, $indexBy = null)
* @method deleteFromAbraFlexi($id = null)
* @method dateToFlexiDate(\DateTime $date)
* @method getNextRecordID($conditions = [])
* @method getEvidence()
* @method getLastInsertedId()
* @method getFlexiData(string $suffix = '', $conditions = null)
* @method getFlexiRow($recordID)
* @method getData()
* @method processInit($init)
* @method getInFormat($format, $reportName = null, $lang = null, $sign = false)
*/
class FlexiBeeApiWorker
{
private ?RO $worker = null;
public static function create(string $worker, array $config): self
{
$wrapper = new static();
return $wrapper->setWorker(
new $worker(null, $config)
);
}
public function setWorker(RO $worker): self
{
$this->worker = $worker;
return $this;
}
public function __get($name)
{
if (property_exists($this, $name)) {
return $this->{$name};
}
if (property_exists($this->worker, $name)) {
return $this->worker->{$name};
}
return null;
}
public function __set($name, $value)
{
if (property_exists($this->worker, $name)) {
return $this->worker->{$name} = $value;
}
return $this->{$name} = $value;
}
public function __call($name, $arguments)
{
try {
$result = call_user_func_array([$this->worker, $name], $arguments);
} catch (Exception $e) {
// ta knihovna ma nejaky rozbity message, ktere to vypisuje v exceptione, takze to tady jen catchnu
// a vyhodim nasi FlexiBeeException se spravnou message
foreach ($e->getErrorMessages() as $error) {
throw new FlexiBeeException(
sprintf('%s: %s', $error['code'] ?? 'ERROR', $error['message']),
!empty($error['code']) && is_numeric($error['code']) ? (int) $error['code'] : 0,
$e
);
}
throw new FlexiBeeException($e->getMessage(), $e->getCode(), $e);
}
return $result;
}
public function __destruct()
{
if ($this->worker instanceof RW) {
$this->worker->disconnect();
}
unset($this->worker);
}
}