<?php

namespace iDServer\Basic;

use iDServer\Exception\iDServerException;
use iDServer\AbstractKeyStore;

class KeyStore extends AbstractKeyStore
{
    /**
     * @var array
     */
    private $certificates = array();

    /**
     * @var string|null
     */
    private $signingKey = null;

    /**
     * @var string|null
     */
    private $signingCertificate = null;

    /**
     * @var string|null
     */
    private $cryptKey = null;

    /**
     * Set the certificate attached to this issuer.
     *
     * @param string $issuerName The name of the entity issuing the certificate.
     * @param array|string $cert The certificate in PEM format.
     * @return $this
     */
    public function setCertificate($issuerName, $cert)
    {
        $this->certificates[$issuerName] = $cert;
        return $this;
    }

    /**
     * @param string $issuerName
     * @return string|string[]
     */
    public function getCertificate($issuerName)
    {
        if (!array_key_exists($issuerName, $this->certificates)) {
            throw new iDServerException("Issuer " . $issuerName . " not found in the certificate store");
        }

        return $this->certificates[$issuerName];
    }

    /**
     * @param string $signingKey
     * @return $this
     */
    public function setSigningKey($signingKey)
    {
        $this->signingKey = $signingKey;
        return $this;
    }

    /**
     * @return string|null
     */
    public function getSigningKey()
    {
        return $this->signingKey;
    }

    /**
     * @param string $signingCertificate
     * @return $this
     */
    public function setSigningCertificate($signingCertificate)
    {
        $this->signingCertificate = $signingCertificate;
        return $this;
    }

    /**
     * @return string|null
     */
    public function getSigningCertificate()
    {
        return $this->signingCertificate;
    }

    /**
     * @param string $key
     * @return $this
     */
    public function setCryptKey($key)
    {
        $this->cryptKey = $key;
        return $this;
    }

    /**
     * @return string|null
     */
    public function getCryptKey()
    {
        return $this->cryptKey;
    }
}
