<commit_before><?php
/**
 * @author <NAME> <EMAIL>
 * @copyright (c) <NAME> 2013
 * @license http://opensource.org/licenses/mit-license.php
 */

namespace StrokerCache\Service;

use StrokerCache\Exception\UnsupportedAdapterException;
use StrokerCache\IdGenerator\IdGeneratorInterface;
use Zend\Mvc\MvcEvent;
use StrokerCache\Event\CacheEvent;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerInterface;
use StrokerCache\Options\ModuleOptions;
use Zend\Cache\Storage\TaggableInterface;
use Zend\Cache\Storage\StorageInterface;

class CacheService
{
    /**
     * Prefix to use for the tag key
     * @var string
     */
    const TAG_PREFIX = 'strokercache_';

    /**
     * @var EventManagerInterface
     */
    protected $eventManager;

    /**
     * @var StorageInterface
     */
    protected $cacheStorage;

    /**
     * @var IdGeneratorInterface
     */
    protected $idGenerator;

    /**
     * @var ModuleOptions
     */
    protected $options;

    /**
     * Default constructor
     *
     * @param StorageInterface $cacheStorage
     * @param ModuleOptions $options
     * @param IdGeneratorInterface $idGenerator
     */
    public function __construct(StorageInterface $cacheStorage, ModuleOptions $options, IdGeneratorInterface $idGenerator = null)
    {
        $this->setCacheStorage($cacheStorage);
        $this->setOptions($options);
        $this->setIdGenerator($idGenerator);
    }

    /**
     * Check if a page is saved in the cache and return contents. Return null when no item is found.
     *
     * @param MvcEvent $mvcEvent
     * @return mixed|null
     */
    public function load(MvcEvent $mvcEvent)
    {
        $id = $this->getIdGenerator()->generate();
        if (!$this->getCacheStorage()->hasItem($id)) {
            return null;
        };

        $event = $this->createCacheEvent(CacheEvent::EVENT_LOAD, $mvcEvent);
        $event->setCacheKey($id);

        $results = $this->getEventManager()->triggerEventUntil(function ($result) {
            return ($result === false);
        }, $event);

        if ($results->stopped()) {
            return null;
        }

        return $this->getCacheStorage()->getItem($id);
    }

    /**
     * Save the page contents to the cache storage.
     *
     * @param MvcEvent $mvcEvent
     */
    public function save(MvcEvent $mvcEvent)
    {
        if (!$this->shouldCacheRequest($mvcEvent)) {
            return;
        }

        $id = $this->getIdGenerator()->generate();

        $item = ($this->getOptions()->getCacheResponse() === true) ? serialize($mvcEvent->getResponse()) : $mvcEvent->getResponse()->getContent();

        $this->getCacheStorage()->setItem($id, $item);

        $this->getEventManager()->triggerEvent($this->createCacheEvent(CacheEvent::EVENT_SAVE, $mvcEvent));

        $cacheStorage = $this->getCacheStorage();
        if ($cacheStorage instanceof TaggableInterface) {
            $cacheStorage->setTags($id, $this->getTags($mvcEvent));
        }
    }

    /**
     * Determine if we should cache the current request
     *
     * @param  MvcEvent $mvcEvent
     * @return bool
     */<commit_msg>Fixed bug when cache key was not set in EVENT_SAVE event<commit_after><?php
/**
 * @author <NAME> <EMAIL>
 * @copyright (c) <NAME> 2013
 * @license http://opensource.org/licenses/mit-license.php
 */

namespace StrokerCache\Service;

use StrokerCache\Exception\UnsupportedAdapterException;
use StrokerCache\IdGenerator\IdGeneratorInterface;
use Zend\Mvc\MvcEvent;
use StrokerCache\Event\CacheEvent;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerInterface;
use StrokerCache\Options\ModuleOptions;
use Zend\Cache\Storage\TaggableInterface;
use Zend\Cache\Storage\StorageInterface;

class CacheService
{
    /**
     * Prefix to use for the tag key
     * @var string
     */
    const TAG_PREFIX = 'strokercache_';

    /**
     * @var EventManagerInterface
     */
    protected $eventManager;

    /**
     * @var StorageInterface
     */
    protected $cacheStorage;

    /**
     * @var IdGeneratorInterface
     */
    protected $idGenerator;

    /**
     * @var ModuleOptions
     */
    protected $options;

    /**
     * Default constructor
     *
     * @param StorageInterface $cacheStorage
     * @param ModuleOptions $options
     * @param IdGeneratorInterface $idGenerator
     */
    public function __construct(StorageInterface $cacheStorage, ModuleOptions $options, IdGeneratorInterface $idGenerator = null)
    {
        $this->setCacheStorage($cacheStorage);
        $this->setOptions($options);
        $this->setIdGenerator($idGenerator);
    }

    /**
     * Check if a page is saved in the cache and return contents. Return null when no item is found.
     *
     * @param MvcEvent $mvcEvent
     * @return mixed|null
     */
    public function load(MvcEvent $mvcEvent)
    {
        $id = $this->getIdGenerator()->generate();
        if (!$this->getCacheStorage()->hasItem($id)) {
            return null;
        };

        $event = $this->createCacheEvent(CacheEvent::EVENT_LOAD, $mvcEvent);
        $event->setCacheKey($id);

        $results = $this->getEventManager()->triggerEventUntil(function ($result) {
            return ($result === false);
        }, $event);

        if ($results->stopped()) {
            return null;
        }

        return $this->getCacheStorage()->getItem($id);
    }

    /**
     * Save the page contents to the cache storage.
     *
     * @param MvcEvent $mvcEvent
     */
    public function save(MvcEvent $mvcEvent)
    {
        if (!$this->shouldCacheRequest($mvcEvent)) {
            return;
        }

        $id = $this->getIdGenerator()->generate();

        $item = ($this->getOptions()->getCacheResponse() === true) ? serialize($mvcEvent->getResponse()) : $mvcEvent->getResponse()->getContent();

        $this->getCacheStorage()->setItem($id, $item);

        $cacheEvent = $this->createCacheEvent(CacheEvent::EVENT_SAVE, $mvcEvent);
        $cacheEvent->setCacheKey($id);

        $this->getEventManager()->triggerEvent($cacheEvent);

        $cacheStorage = $this->getCacheStorage();
        if ($cacheStorage instanceof TaggableInterface) {
            $cacheStorage->setTags($id, $this->getTags($mvcEvent));
        }
    }

    /**
     * Determine if we should cache the current request
     *
     * @param  MvcEvent $mvcEvent
     * @return bool
     */
{'fraction_non_alphanumeric': 0.1327613104524181, 'fraction_numerical': 0.0012480499219968799, 'mean_word_length': 2.716521739130435, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7292934', 'n_tokens_mistral': 1857, 'n_tokens_neox': 1811, 'n_words': 381}