Files
kupshop/bundles/KupShop/KupShopBundle/Context/PosContext.php
2025-08-02 16:30:27 +02:00

95 lines
2.4 KiB
PHP

<?php
namespace KupShop\KupShopBundle\Context;
use KupShop\POSBundle\Util\PosEntity;
use Symfony\Component\HttpFoundation\Request;
class PosContext implements ContextInterface
{
/** Activated pos ID */
private ?int $idPos = null;
/** Loaded pos entity */
private ?PosEntity $posEntity = null;
/** Array of all pos entities */
private array $supported = [];
/** @var string - API version for POS
* this value must match the value defined in the package.json in the cash register repository.
*/
private string $apiVersion = '2.0.1';
/** Creates new PosEntity object based on provided id */
public function activate(string $id): void
{
$this->idPos = $id;
$this->clearCache();
}
/** Returns id active entity */
public function getActiveId(): ?int
{
return $this->idPos ?? null;
}
/** Returns active pod entity */
public function getActive(): ?PosEntity
{
if (empty($this->posEntity) && $this->getActiveId()) {
$this->posEntity = new PosEntity();
$this->posEntity->createFromDB($this->getActiveId())->activateContexts();
}
return $this->posEntity;
}
/** Returns array of all pos */
public function getSupported(): array
{
if (empty($this->supported)) {
$rows = sqlQueryBuilder()
->select('*')
->from('pos')
->execute()
->fetchAllAssociative();
foreach ($rows ?? [] as $row) {
$entity = new PosEntity();
$entity->createFromArray($row);
$this->supported[$row['id']] = $entity;
}
}
return $this->supported;
}
/** Deletes all cached objects */
public function clearCache(): void
{
$this->posEntity = null;
}
/** Check provided id is actually id from pos */
public function isValid(string $id): bool
{
return !empty($this->getSupported()[$id] ?? null);
}
public static function getIdFromRequest(Request $request): ?string
{
return $request->headers->get('Wpj-Id-Pos-Entity') ?? null;
}
public static function getVersionFromRequest(Request $request): ?string
{
return $request->headers->get('Wpj-Version-Pos') ?? null;
}
public function getApiVersion(): string
{
return $this->apiVersion;
}
}