<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Product_Promotions_Cache' ) ) {

    class Pricely_Product_Promotions_Cache {

        private $product_list;
        private static $instance;

        public static function get_instance(): self {

            if ( !self::$instance ) {

                self::$instance = new self();
            }

            return self::$instance;
        }

        public function __construct() {

            $this->product_list = array();
        }

        public function clear() {

            $this->product_list = array();
        }

        public function get( $cache_key ) {

            //can't process empty product
            if ( empty( $cache_key ) ) {

                return false;
            }

            //return in memory promotions
            if ( isset( $this->product_list[ $cache_key ] ) ) {

                return $this->product_list[ $cache_key ];
            }

            return false;
        }

        public function set( $promotions, $cache_key ) {

            if ( is_array( $promotions ) ) {

                $this->product_list[ $cache_key ] = $promotions;
            }
        }

    }

}