<?php

/**
 * @copyright     Copyright (c) 2009-2020 Ryan Demmer. All rights reserved
 * @license       GNU/GPL 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * JCE is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses
 */
class WFS3FileSystemCacheJoomla
{
    private $cache = null;

    public function __construct()
    {
        $this->cache = JCache::getInstance('output', array('defaultgroup' => 'com_jce', 'caching' => true, 'lifetime' => 1440));
    }

    /**
     * @return array|mixed
     */
    public function get($path = '')
    {
        $data = $this->cache->get('wf_s3_cache');

        if (is_string($data)) {
            $data = json_decode($data);
        }

        if (empty($data)) {
            return array();
        }

        if ($path && array_key_exists($path, $data)) {
            return $data[$path];
        }

        return $data;
    }

    public function store($data, $path = '')
    {
        $store = $this->get(); 

        $store[$path] = $data;
        
        $json = json_encode($store);

        $this->cache->store('wf_s3_cache', $json);
    }

    public function remove()
    {
        $this->cache->remove('wf_s3_cache');
    }
}
