<?php

/*
 * This file is part of the overtrue/wechat.
 *
 * (c) overtrue <i@overtrue.me>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */
namespace EasyWeChat\Kernel\Traits;

use Doctrine\Common\Cache\PredisCache;
use EasyWeChat\Kernel\Cache\CacheProviderAdapter;
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
use EasyWeChat\Kernel\ServiceContainer;
use Illuminate\Support\Facades\Redis;
use ModStart\Core\Exception\BizException;
use Psr\Cache\CacheItemPoolInterface;
use Psr\SimpleCache\CacheInterface as SimpleCacheInterface;
use Symfony\Component\Cache\Adapter\DoctrineAdapter;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\DoctrineProvider;
use Symfony\Component\Cache\Psr16Cache;
use Symfony\Component\Cache\Simple\FilesystemCache;
/**
 * Trait InteractsWithCache.
 *
 * @author overtrue <i@overtrue.me>
 */
trait InteractsWithCache
{
    /**
     * @var \Psr\SimpleCache\CacheInterface
     */
    protected $cache;
    /**
     * Get cache instance.
     *
     * @return \Psr\SimpleCache\CacheInterface
     *
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     */
    public function getCache()
    {
        if ($this->cache) {
            return $this->cache;
        }
        // 处理缓存
        $driver = null;
        switch (config('easywechat.cache.driver')) {
            default:
                $driver = new \Doctrine\Common\Cache\FilesystemCache(config('easywechat.cache.filePath',storage_path('framework/cache/easywechat')));
                break;
        }
        BizException::throwsIfEmpty('EasyWechatCache Error', $driver);
        $this->cache = new CacheProviderAdapter($driver);
        $this->setCache($this->cache);
        return $this->cache;

//        if (property_exists($this, 'app') && $this->app instanceof ServiceContainer && isset($this->app['cache'])) {
//            $this->setCache($this->app['cache']);
//            // Fix PHPStan error
//            assert($this->cache instanceof \Psr\SimpleCache\CacheInterface);
//            return $this->cache;
//        }
//        return $this->cache = $this->createDefaultCache();
    }
    /**
     * Set cache instance.
     *
     * @param \Psr\SimpleCache\CacheInterface|\Psr\Cache\CacheItemPoolInterface $cache
     *
     * @return $this
     *
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     */
    public function setCache($cache)
    {
        if (empty(\array_intersect([SimpleCacheInterface::class, CacheItemPoolInterface::class], \class_implements($cache)))) {
            throw new InvalidArgumentException(\sprintf('The cache instance must implements %s or %s interface.', SimpleCacheInterface::class, CacheItemPoolInterface::class));
        }
        if ($cache instanceof CacheItemPoolInterface) {
            if (!$this->isSymfony43OrHigher()) {
                throw new InvalidArgumentException(sprintf('The cache instance must implements %s', SimpleCacheInterface::class));
            }
            $cache = new Psr16Cache($cache);
        }
        $this->cache = $cache;
        return $this;
    }
    /**
     * @return \Psr\SimpleCache\CacheInterface
     */
    protected function createDefaultCache()
    {
        if ($this->isSymfony43OrHigher()) {
            return new Psr16Cache(new FilesystemAdapter('easywechat', 1500));
        }
        return new FilesystemCache();
    }
    /**
     * @return bool
     */
    protected function isSymfony43OrHigher()
    {
        return \class_exists('Symfony\\Component\\Cache\\Psr16Cache');
    }
}