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

/**
 * PKIStatusInfo implementation.
 *
 *<pre>
 * PKIStatusInfo ::= SEQUENCE {
 *      status        PKIStatus,
 *      statusString  PKIFreeText     OPTIONAL,
 *      failInfo      PKIFailureInfo  OPTIONAL
 * }
 *</pre>
 *
 * <pre>
 * PKIStatus ::= INTEGER {
 *      accepted               (0),
 *      grantedWithMods        (1),
 *      rejection              (2),
 *      waiting                (3),
 *      revocationWarning      (4),
 *      revocationNotification (5),
 *      keyUpdateWarning       (6)
 * }
 * </pre>
 *
 * <pre>
 * PKIFailureInfo ::= BIT STRING {
 *      badAlg              (0),
 *      badMessageCheck     (1),
 *      badRequest          (2),
 *      badTime             (3),
 *      badCertId           (4),
 *      badDataFormat       (5),
 *      wrongAuthority      (6),
 *      incorrectData       (7),
 *      missingTimeStamp    (8),
 *      badPOP              (9),
 *      certRevoked         (10),
 *      certConfirmed       (11),
 *      wrongIntegrity      (12),
 *      badRecipientNonce   (13),
 *      timeNotAvailable    (14),
 *      unacceptedPolicy    (15),
 *      unacceptedExtension (16),
 *      addInfoNotAvailable (17),
 *      badSenderNonce      (18),
 *      badCertTemplate     (19),
 *      signerNotTrusted    (20),
 *      transactionIdInUse  (21),
 *      unsupportedVersion  (22),
 *      notAuthorized       (23),
 *      systemUnavail       (24),
 *      systemFailure       (25),
 *      duplicateCertReq    (26),
 *      extendLater        (100), -- GuardTime specific extension
 *      extensionOverdue   (101)  -- GuardTime specific extension
 * }
 * </pre>
 *
 * <pre>
 * PKIFreeText ::= SEQUENCE SIZE (1..MAX) OF UTF8String
 * </pre>
 *
 * @package asn1
 * @subpackage cmp
 *
 * @link http://tools.ietf.org/html/rfc4210#section-5.2.3 RFC 4210: Certificate Management Protocol
 */
class PKIStatusInfo {

    private $status;
    private $statusString;
    private $failInfo;

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

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

        if (!$object instanceof ASN1Sequence) {
            throw new GTException("Expecting an ASN1Sequence");
        }

        $this->status = (int) $object->getObjectAt(0)->getValue();

        for ($i = 1; $i < $object->getObjectCount(); $i++) {

            $item = $object->getObjectAt($i);

            if ($item instanceof ASN1BitString) {
                $this->failInfo = $item->getValue();

            } else if ($item instanceof ASN1Sequence) {

                $statusString = array();

                foreach ($item->getObjects() as $line) {
                    array_push($statusString, $line->getValue());
                }

                $this->statusString = $statusString;

            } else {

                throw new GTException("Unexpected item: " . get_class($item));
            }
        }
    }

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

    /**
     * Sets the status code.
     *
     * @param  int $status
     * @return void
     */
    public function setStatus($status) {
        $this->status = $status;
    }

    /**
     * Gets the status message.
     *
     * @return string status message
     */
    public function getStatusMessage() {

        if (!empty($this->statusString)) {
            return implode("\n", $this->statusString);
        }

        return "";

    }

    /**
     * Gets the failure info bit string.
     *
     * @return string failure info bit string
     */
    public function getFailInfo() {
        return $this->failInfo;
    }
}

?>
