<commit_before><?php

namespace Kibo\Phast\Filters\Image;

use Kibo\Phast\Cache\Cache;
use Kibo\Phast\Retrievers\Retriever;
use Kibo\Phast\ValueObjects\URL;

class CachedCompositeImageFilter extends CompositeImageFilter {

    /**
     * @var Cache
     */
    private $cache;

    /**
     * @var Retriever
     */
    private $retriever;

    /**
     * @var array
     */
    private $request;

    /**
     * @var array
     */
    private $filtersNames = [];

    /**
     * CachedCompositeImageFilter constructor.
     *
     * @param Cache $cache
     * @param Retriever $retriever
     * @param array $request
     */
    public function __construct(Cache $cache, Retriever $retriever, array $request) {
        $this->cache = $cache;
        $this->retriever = $retriever;
        $this->request = $request;
    }

    /**
     * @param ImageFilter $filter
     */
    public function addImageFilter(ImageFilter $filter) {
        parent::addImageFilter($filter);
        $this->filtersNames[] = get_class($filter);
    }

    /**
     * @param Image $image
     * @return Image
     * @throws \Exception
     */
    public function apply(Image $image) {
        $url = URL::fromString($this->request['src']);
        $lastModTime = $this->retriever->getLastModificationTime($url);
        sort($this->filtersNames);
        $key = implode("\n", array_merge([$lastModTime, $this->request['src']], $this->filtersNames));
        if (isset ($this->request['width'])) {
            $key .= $this->request['width'];
        }
        if (isset ($this->request['height'])) {
            $key .= $this->request['height'];
        }
        if (isset ($this->request['preferredType'])) {
            $key .= $this->request['preferredType'];
        }
        $filtered = $this->cache->get($key, function () use ($image) {
            try {
                return parent::apply($image);
            } catch (\Exception $e) {
                return $e;
            }
        });
        if ($filtered instanceof \Exception) {
            throw $filtered;
        }
        return $filtered;
    }

}<commit_msg>Add request params to delimited cache key<commit_after><?php

namespace Kibo\Phast\Filters\Image;

use Kibo\Phast\Cache\Cache;
use Kibo\Phast\Retrievers\Retriever;
use Kibo\Phast\ValueObjects\URL;

class CachedCompositeImageFilter extends CompositeImageFilter {

    /**
     * @var Cache
     */
    private $cache;

    /**
     * @var Retriever
     */
    private $retriever;

    /**
     * @var array
     */
    private $request;

    /**
     * @var array
     */
    private $filtersNames = [];

    /**
     * CachedCompositeImageFilter constructor.
     *
     * @param Cache $cache
     * @param Retriever $retriever
     * @param array $request
     */
    public function __construct(Cache $cache, Retriever $retriever, array $request) {
        $this->cache = $cache;
        $this->retriever = $retriever;
        $this->request = $request;
    }

    /**
     * @param ImageFilter $filter
     */
    public function addImageFilter(ImageFilter $filter) {
        parent::addImageFilter($filter);
        $this->filtersNames[] = get_class($filter);
    }

    /**
     * @param Image $image
     * @return Image
     * @throws \Exception
     */
    public function apply(Image $image) {
        $url = URL::fromString($this->request['src']);
        $lastModTime = $this->retriever->getLastModificationTime($url);
        sort($this->filtersNames);
        $key = array_merge([$lastModTime, $this->request['src']], $this->filtersNames);
        if (isset ($this->request['width'])) {
            $key[] = $this->request['width'];
        }
        if (isset ($this->request['height'])) {
            $key[] = $this->request['height'];
        }
        if (isset ($this->request['preferredType'])) {
            $key[] = $this->request['preferredType'];
        }
        $key = implode("\n", $key);
        $filtered = $this->cache->get($key, function () use ($image) {
            try {
                return parent::apply($image);
            } catch (\Exception $e) {
                return $e;
            }
        });
        if ($filtered instanceof \Exception) {
            throw $filtered;
        }
        return $filtered;
    }

}
{'fraction_non_alphanumeric': 0.15465605989705195, 'fraction_numerical': 0.0, 'mean_word_length': 2.064516129032258, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, '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': '6432495', 'n_tokens_mistral': 1286, 'n_tokens_neox': 1253, 'n_words': 250}