1
0
mirror of https://github.com/devcode-it/openstamanager.git synced 2025-02-05 18:27:32 +01:00
openstamanager/src/Hooks/CachedManager.php

49 lines
842 B
PHP
Raw Normal View History

2019-08-26 18:02:05 +02:00
<?php
namespace Hooks;
2020-03-03 10:31:01 +01:00
use Models\Cache;
use Models\Hook;
2019-08-26 18:02:05 +02:00
abstract class CachedManager extends Manager
{
2020-03-03 10:31:01 +01:00
protected $cache = null;
2019-08-26 18:02:05 +02:00
2020-03-03 10:31:01 +01:00
public function __construct(Hook $hook)
2019-08-29 11:25:13 +02:00
{
2020-03-03 10:31:01 +01:00
parent::__construct($hook);
$this->cache = Cache::get($this->getCacheName());
2019-08-29 11:25:13 +02:00
}
2020-03-03 10:31:01 +01:00
abstract public function cacheData();
2019-08-27 15:42:13 +02:00
2020-03-03 10:31:01 +01:00
abstract public function getCacheName();
2019-08-27 15:42:13 +02:00
2020-03-03 10:31:01 +01:00
public function needsExecution()
{
return !$this->isCached();
2019-08-27 15:42:13 +02:00
}
2019-08-26 18:02:05 +02:00
2020-03-03 10:31:01 +01:00
public function execute()
2019-08-26 18:02:05 +02:00
{
2020-03-03 10:31:01 +01:00
if (!$this->isCached()) {
$data = $this->cacheData();
2019-08-29 11:25:13 +02:00
2020-03-03 10:31:01 +01:00
$this->getCache()->set($data);
2019-08-26 18:02:05 +02:00
}
2020-03-03 10:31:01 +01:00
return $this->getCache()->content;
2019-08-26 18:02:05 +02:00
}
2020-03-03 10:31:01 +01:00
public function getCache()
2019-08-26 18:02:05 +02:00
{
2020-03-03 10:31:01 +01:00
return $this->cache;
2019-08-26 18:02:05 +02:00
}
2020-03-03 10:31:01 +01:00
public function isCached()
2019-08-26 18:02:05 +02:00
{
2020-03-03 10:31:01 +01:00
return $this->getCache()->isValid();
2019-08-26 18:02:05 +02:00
}
}