<?php

namespace CookieSession\Storage;

class Storage extends AbstractStorage
{
    /**
     * @return $this
     * @throws \Exception
     */
    public function encrypt()
    {
        $this->to_store = $this->getEncrypter()->encrypt($this->to_store);

        return $this;
    }


    /**
     * @return $this
     * @throws \Exception
     */
    public function decrypt()
    {
        $this->to_store = $this->getEncrypter()->decrypt($this->to_store);

        return $this;
    }


    /**
     * @return $this
     */
    public function compress()
    {
        $this->to_store = gzdeflate($this->to_store);

        return $this;
    }


    /**
     * @return $this
     */
    public function decompress()
    {
        $this->to_store = gzinflate($this->to_store);

        return $this;
    }


    /**
     * @return $this
     */
    public function encode()
    {
        $this->to_store = base64_encode($this->to_store);

        return $this;
    }


    /**
     * @return $this
     */
    public function decode()
    {
        $this->to_store = base64_decode($this->to_store);

        return $this;
    }


    /**
     * @return $this
     * @throws \Exception
     */
    public function jsonDecode()
    {
        $result = json_decode($this->to_store, true);

        if(false === $result)
        {
            throw new \Exception("Unable to decode JSON. Error reported: " . json_last_error_msg());
        }

        $this->to_store = $result;

        return $this;
    }

    /**
     * @param $string
     *
     * @return mixed
     * @throws \Exception
     */
    public function sign($string)
    {
        return $this->getEncrypter()->sign($string);
    }


    /**
     * @param $string
     * @param $signature
     *
     * @return bool
     * @throws \Exception
     */
    public function verify($string, $signature)
    {
        return $this->getEncrypter()->verify($string, $signature);
    }
}