diff options
author | diosmosis <benaka@piwik.pro> | 2015-03-05 07:04:51 +0300 |
---|---|---|
committer | diosmosis <benaka@piwik.pro> | 2015-03-05 07:04:51 +0300 |
commit | 67677a192e220833ca0fc0e3279e3f792f1f1af5 (patch) | |
tree | ebadc26b8b7fcfe388c46af6bf1b31d8ddbf26cb /core/Concurrency | |
parent | 13bdf6749222c7335e81511480ac867b3a859cc3 (diff) |
Refs #7181, move distributed list used to hold archives to purge to separate class from InvalidatedReports. Since list is only used to purge during scheduled tasks, moved to CoreAdminHome plugin where related task resides. Introduced common base type core\Concurrency\DistributedList in order to avoid introducing code redundancy when splitting InvalidatedReports up.
Diffstat (limited to 'core/Concurrency')
-rw-r--r-- | core/Concurrency/DistributedList.php | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/core/Concurrency/DistributedList.php b/core/Concurrency/DistributedList.php new file mode 100644 index 0000000000..2f37fe0e4b --- /dev/null +++ b/core/Concurrency/DistributedList.php @@ -0,0 +1,109 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ +namespace Piwik\Concurrency; + +use Piwik\Option; + +/** + * TODO + * + * TODO: tests + */ +class DistributedList +{ + /** + * TODO + * + * @var string + */ + private $optionName; + + /** + * TODO + */ + public function __construct($optionName) + { + $this->optionName = $optionName; + } + + /** + * TODO + * + * @return array + */ + public function getAll() + { + Option::clearCachedOption($this->optionName); + $array = Option::get($this->optionName); + + if ($array + && ($array = unserialize($array)) + && count($array) + ) { + return $array; + } + return array(); + } + + /** + * TODO + * + * @param string[] $items + */ + public function setAll($items) + { + foreach ($items as &$item) { + $item = (string)$item; + } + + Option::set($this->optionName, serialize($items)); + } + + /** + * TODO + * + * @param string|array $item + */ + public function add($item) + { + $allItems = $this->getAll(); + if (is_array($item)) { + $allItems = array_merge($allItems, $item); + } else { + $allItems[] = $item; + } + + $this->setAll($allItems); + } + + /** + * TODO + * TODO: support removing multiple + * + * @param string|array $items + */ + public function remove($items) + { + if (!is_array($items)) { + $items = array($items); + } + + $allItems = $this->getAll(); + + foreach ($items as $item) { + $existingIndex = array_search($item, $allItems); + if ($existingIndex === false) { + return; + } + + unset($allItems[$existingIndex]); + } + + $this->setAll($allItems); + } +}
\ No newline at end of file |