<commit_before><?php
class Infusionsoft_SmartCache{
    private $ttl = 120;
    protected $name = '';
    private $dir = '';

    public function __construct($name, $ttl = 300, $dir = 'cache'){
        if (strpos($dir, '/') !== 0 && strpos($dir, ':') !== 1){
            $dir = APP . 'Vendor' . DS . 'php-infusionsoft-sdk' . DS . 'Infusionsoft' . DS . $dir;
        }
        $this->ttl = $ttl;
        $this->dir = $dir;
        $this->name = $name;
    }

    public function getData(){
        $data = $this->getDataFromCacheIfNotStale();
        if($data === false){
            $data = $this->getDataFromSource();
            $this->cacheData($data);
        } else {
            unset($data['expiration']);
        }
        return $data;
    }

    public function getCacheFileName(){
        return $this->dir . '/' . $this->name . '.cache';
    }

    public function expireCache(){
        unlink($this->getCacheFileName());
    }

    public function getDataFromSource(){
        throw new Exception("Must Override This Method");
    }

    public function isDataNotStale($data){
        $isGood = true;
        //If this is expired, return false, or if the creation data (extrapolated by expiration time stamp minus ttl) is in the future).
        if($data['expiration'] < time() || $data['expiration'] - $this->ttl > time()){
            $isGood = false;
        }
        return $isGood;
    }

    public function getDataFromCacheIfNotStale(){
        $data = $this->getDataFromCache();
        if($this->isDataNotStale($data)){
            return $data;
        } else {
            return false;
        }
    }

    public function getDataFromCache(){
        $data = false;
        if(file_exists($this->getCacheFileName())){
            $serialized_data = file_get_contents($this->getCacheFileName());<commit_msg>Disable SmartCache when in test mode<commit_after><?php
class Infusionsoft_SmartCache{
    private $ttl = 120;
    protected $name = '';
    private $dir = '';

    public function __construct($name, $ttl = 300, $dir = 'cache'){
        if (strpos($dir, '/') !== 0 && strpos($dir, ':') !== 1){
            $dir = APP . 'Vendor' . DS . 'php-infusionsoft-sdk' . DS . 'Infusionsoft' . DS . $dir;
        }
        $this->ttl = $ttl;
        $this->dir = $dir;
        $this->name = $name;
    }

    public function getData(){
        $data = $this->getDataFromCacheIfNotStale();
        if($data === false){
            $data = $this->getDataFromSource();
            $this->cacheData($data);
        } else {
            unset($data['expiration']);
        }
        return $data;
    }

    public function getCacheFileName(){
        return $this->dir . '/' . $this->name . '.cache';
    }

    public function expireCache(){
        unlink($this->getCacheFileName());
    }

    public function getDataFromSource(){
        throw new Exception("Must Override This Method");
    }

    public function isDataNotStale($data){
        $isGood = true;
        //If this is expired, return false, or if the creation data (extrapolated by expiration time stamp minus ttl) is in the future).
        if($data['expiration'] < time() || $data['expiration'] - $this->ttl > time() || defined('INFUSIONSOFT_SDK_TEST')){
            $isGood = false;
        }
        return $isGood;
    }

    public function getDataFromCacheIfNotStale(){
        $data = $this->getDataFromCache();
        if($this->isDataNotStale($data)){
            return $data;
        } else {
            return false;
        }
    }

    public function getDataFromCache(){
        $data = false;
        if(file_exists($this->getCacheFileName())){
            $serialized_data = file_get_contents($this->getCacheFileName());
{'fraction_non_alphanumeric': 0.15355704697986577, 'fraction_numerical': 0.004295302013422818, 'mean_word_length': 2.190068493150685, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, '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': '6929998', 'n_tokens_mistral': 1130, 'n_tokens_neox': 1091, 'n_words': 257}