*/ class DeliveryExecutionCounterService extends ConfigurableService implements DeliveryExecutionCounterInterface { const OPTION_PERSISTENCE = 'persistence'; //concatenation in constants allowed since php 5.6 const KEY_PREFIX = self::class . '_'; /** * Get number of delivery executions of given status. * @param $statusUri * @return integer */ public function count($statusUri) { $persistence = $this->getPersistence(); $key = $this->getStatusKey($statusUri); return intval($persistence->get($key)); } /** * @param DeliveryExecutionState $event */ public function executionStateChanged(DeliveryExecutionState $event) { $fromStatusKey = $this->getStatusKey($event->getPreviousState()); $toStatusKey = $this->getStatusKey($event->getState()); $persistence = $this->getPersistence(); if ($persistence->exists($fromStatusKey) && $persistence->get($fromStatusKey) > 0) { $persistence->decr($fromStatusKey); } if (!$persistence->exists($toStatusKey)) { $persistence->set($toStatusKey, 1); } else { $persistence->incr($toStatusKey); } } /** * @param DeliveryExecutionCreated $event * @throws \common_exception_NotFound */ public function executionCreated(DeliveryExecutionCreated $event) { $persistence = $this->getPersistence(); $state = $event->getDeliveryExecution()->getState()->getUri(); $toStatusKey = $this->getStatusKey($state); if (!$persistence->exists($toStatusKey)) { $persistence->set($toStatusKey, 1); } else { $persistence->incr($toStatusKey); } } /** * @return \common_persistence_KvDriver */ protected function getPersistence() { return $this->getServiceLocator() ->get(\common_persistence_Manager::class)->getPersistenceById($this->getOption(self::OPTION_PERSISTENCE)); } /** * @param string $statusUri * @return string */ protected function getStatusKey($statusUri) { return self::KEY_PREFIX . $statusUri; } /** * @param $statusUri */ public function refresh($statusUri) { //this implementation do not support refreshing } }