<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class Cache{

    
    public static function setConfig(){
        $redis = new \Redis(); 
        $redis->connect(REDIS_HOST, REDIS_PORT); 
        return $redis;
    }

    public static function pingpong() {
        dd(static::setConfig());
    }
    
    public static function write($key,$value) {
        static::setConfig()->set($key,$value);
    }
    
    public static function push($key,$value) {
        static::setConfig()->rpush($key,json_encode($value));
    }
    
    public static function pop($key) {
       return current(json_decode(static::setConfig()->rpop($key),true));
    }
    
    public static function enqueue($to, array $data, array $options = [])
    {
        
        $defaults = [
            'subject' => '',
            'send_at' => new \DateTime(),//'2021-08-28 05:17:56',//date('Y-m-d H:i:s'),
            'template' => 'no_template',
            'member_id' => 0,
            'layout' => 'default',
            'theme' => '',
            'format' => 'both',
            'headers' => [],
            'template_vars' => $data,
            'config' => 'default',
            'attachments' => []
        ];

        $email = $options + $defaults;
        if (!is_array($to)) {
            $to = [$to];
        }

        $emails = [];
        foreach ($to as $t) {
            $emails[] = ['email' => $t] + $email;
        }
        
        return static::push('cake_email_queue', $emails);
//        $new_emails = $emails;
//        if($this->read('email_queue')){
//            $new_emails = Cache::read('email_queue');
//            if(!empty($emails)){
//                $new_emails = array_merge($new_emails,$emails);
//                //array_push($new_emails,$value);
//            }
//            
//        }
//        $this->write('email_queue', $new_emails);

    }
    
}
