<?php

namespace App\Helper\Cache;
use Modules\Admin\app\Models\Setting;
use Illuminate\Support\Facades\Cache;
use App\Helper\Constant\CacheConstant;

class SettingCacheHelper {
    public static $globals = 'setting';
    public static $key = 'setting';
    public static function getCacheSetting(){
        $setting = Cache::get(self::$key, null);
        if (isset($GLOBALS[self::$globals]) && $setting != null)
            return $GLOBALS[self::$globals];
        if ($setting == null) {
            $setting = Setting::first();
            if ($setting) {
                Cache::put(self::$key, json_encode($setting), (60 * CacheConstant::$expired) );
                $setting = json_encode($setting);
            } else {
                $setting = null;
            }
        }
        if ($setting == null) {
            $GLOBALS[self::$globals] = null;

        } else {
            $GLOBALS[self::$globals] = json_decode($setting);
        }
        return $GLOBALS[self::$globals];
    }

    public static function cleanCacheSetting(){
        Cache::forget(self::$key);
    }
}
