<?php

namespace Openguru\OpenCartFramework\Cache;

use Symfony\Component\Cache\Adapter\DoctrineDbalAdapter;

class SymfonyMySqlCache implements CacheInterface
{
    protected DoctrineDbalAdapter $cache;

    public function __construct(DoctrineDbalAdapter $adapter)
    {
        $this->cache = $adapter;
    }

    public function get(string $key)
    {
        $item = $this->cache->getItem($key);

        return $item->isHit() ? $item->get() : null;
    }

    public function set(string $key, $value, ?int $ttlSeconds = null): void
    {
        $item = $this->cache->getItem($key);
        $item->set($value);

        if ($ttlSeconds) {
            $item->expiresAfter($ttlSeconds);
        }

        $this->cache->save($item);
    }

    public function delete(string $key): void
    {
        $this->cache->deleteItem($key);
    }

    public function clear(): void
    {
        $this->cache->clear();
    }
}
