<?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 tsp
 */

/**
 * TSP TimeStampResp implementation.
 *
 * <pre>
 * TimeStampResp ::= SEQUENCE  {
 *      status                  PKIStatusInfo,
 *      timeStampToken          TimeStampToken     OPTIONAL
 * }
 * </pre>
 *
 * @package asn1
 * @subpackage tsp
 *
 * @link http://tools.ietf.org/html/rfc3161#section-2.4.2 RFC 3161: Time-Stamp Protocol
 */
class TSPTimeStampResp {

    private $status;
    private $token;

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

    /**
     * Decodes the given ASN1Sequence as TSPTimeStampResp.
     *
     * @throws GTException
     * @param  ASN1Sequence $object TSPTimeStampResp 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("timestamp token is missing");
            }

            $token = new CMSContentInfo();
            $token->decode($object->getObjectAt(1));

            $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 status.
     *
     * @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 timestamp token.
     *
     * @return CMSContentInfo timestamp token
     */
    public function getToken() {
        return $this->token;
    }

}

?>
