<?php

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

class UserJiraCacheHelper {
    public static $globals = 'users-jira';
    public static $key = 'users-jira';
    public static function getCacheSetting(){
        $users = Cache::get(self::$key, null);
        if (isset($GLOBALS[self::$globals]) && $users != null)
            return $GLOBALS[self::$globals];
        if ($users == null) {
            $jira_service = new JiraService;
            $user_db = User::all();
            $users = $jira_service->getUsersByEmails($user_db);
            if ($users) {
                Cache::put(self::$key, json_encode($users), (60 * CacheConstant::$expired) );
                $users = json_encode($users);
            } else {
                $users = null;
            }
        }
        if ($users == null) {
            $GLOBALS[self::$globals] = null;

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

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