nur-sery/nur_src/mapper/base/CapacitorConsumer.php

59 lines
1.5 KiB
PHP

<?php
namespace nur\mapper\base;
use nur\mapper\base\capacitor\Capacitor;
use nur\mapper\base\capacitor\ICapacitor;
class CapacitorConsumer extends Consumer {
function __construct(?ICapacitor $capacitor=null, $producer=null, ...$mappers) {
parent::__construct($producer, ...$mappers);
$this->setCapacitor($capacitor);
}
/** @var ICapacitor */
protected $capacitor;
function setCapacitor(?ICapacitor $capacitor): self {
if ($capacitor === null) $capacitor = new Capacitor();
$this->capacitor = $capacitor;
$this->setupCapacitor = true;
return $this;
}
/** @var string|null */
protected $channel;
function setChannel(?string $channel): self {
$this->channel = $channel;
return $this;
}
protected function _setupCapacitor(): void {
}
/** @var bool */
protected $setupCapacitor;
protected function capacitor(): ICapacitor {
if ($this->setupCapacitor) {
$this->_setupCapacitor();
$this->setupCapacitor = false;
}
return $this->capacitor;
}
function cook($item) {
$this->capacitor()->charge($item, $this->channel);
}
function getItem($pkvalues, ?string $channel=null) {
if ($channel === null) $channel = $this->channel;
return $this->capacitor()->getItem($pkvalues, $channel);
}
function discharge(bool $remove=true, ?string $channel=null): iterable {
if ($channel === null) $channel = $this->channel;
return $this->capacitor()->discharge($channel, $remove);
}
}