<?php
/*
 * Copyright 2008-2010 GuardTime AS
 *
 * This file is part of the GuardTime PHP SDK.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @package asn1
 * @subpackage gt
 */

/**
 * GT CertTokenResponse implementation.
 *
 * <pre>
 * CertTokenResponse ::= SEQUENCE {
 *    status      PKIStatusInfo,
 *    certToken   [0] CertToken OPTIONAL
 * }
 * </pre>
 *
 * @package asn1
 * @subpackage gt
 */
class GTCertTokenResponse {

    private $status;
    private $token;

    /**
     * Constructs a new instance of GTCertTokenResponse.
     */
    public function __construct() {
    }

    /**
     * Decodes the given ASN1Sequence as GTCertTokenResponse.
     *
     * @throws GTException
     * @param  ASN1Sequence $object GTCertTokenResponse encoded as ASN1Sequence
     * @return void
     */
    public function decode($object) {

        if (!$object instanceof ASN1Sequence) {
            throw new GTException("object must be an instance of ASN1Sequence");
        }

        if ($object->getObjectCount() < 1 || $object->getObjectCount() > 2) {
            throw new GTException("invalid sequence size");
        }

        $status = new PKIStatusInfo();
        $status->decode($object->getObjectAt(0));

        $this->status = $status;

        $code = $this->getStatusCode();

        if ($code == 0 || $code == 1) {

            if ($object->getObjectCount() != 2) {
                throw new GTException("token is missing");
            }

            $tag = $object->getObjectAt(1);

            if ($tag->getTagValue() != 0) {
                throw new GTException("Unexpected tag value: " . $tag->getTagValue());
            }

            $token = new GTCertToken();
            $token->decode($tag->getObjectAs(ASN1_TAG_SEQUENCE));

            $this->token = $token;

        } else if ($code >= 2 && $code <= 5) {

            if ($object->getObjectCount() == 2) {
                throw new GTException("unexpected timestamp token");
            }

        } else {

            throw new GTException("invalid status code: {$code}");
        }

    }

    /**
     * Gets the statsu.
     *
     * @return PKIStatusInfo status
     */
    public function getStatus() {
        return $this->status;
    }

    /**
     * Gets the status code.
     *
     * @return int status code
     */
    public function getStatusCode() {
        return $this->status->getStatus();
    }

    /**
     * Gets the failure code.
     *
     * @throws GTException
     * @return string failure code
     */
    public function getFailCode() {

        $info = $this->status->getFailInfo();

        if (empty($info)) {
            return -1;
        }

        $code = strpos($this->status->getFailInfo(), '1');

        if ($code === false) {
            return -1;
        }

        return $code;
    }

    /**
     * Gets the cert token.
     *
     * @return GTCertToken token
     */
    public function getToken() {
        return $this->token;
    }

}

?>
