# Source of file Timestamp.php

Size: 1,713 Bytes - Last Modified: 2016-08-28T19:38:31+00:00

## /Users/dshafik/src/akamai-open/edgegrid-auth-php/src/Authentication/Timestamp.php

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
Covered by 50 test(s):

- Akamai\Open\EdgeGrid\Tests\Client\Authentication\TimestampTest::testTimestampFormat
- Akamai\Open\EdgeGrid\Tests\Client\Authentication\TimestampTest::testIsValid
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testDefaultTimestamp
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testDefaultNonce
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testTimestampTimeout
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testDefaultTimeout
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testTimeoutOption
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testSetTimeout
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testStaticDebugSingle
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testInstanceDebugSingle
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testDebugOverrideSingle
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testInstanceDebugOptionSingle
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testNonApiCall
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testLogging with data set #0
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testLogging with data set #1
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testLogging with data set #2
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testLogging with data set #3
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testLogging with data set #4
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testLogging with data set #5
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testLogging with data set #6
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testLoggingRedirect
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testLoggingRequestHandler
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testSetSimpleLog
- Akamai\Open\EdgeGrid\Tests\Handler\DebugTest::testInstanceDebug
- Akamai\Open\EdgeGrid\Tests\Handler\DebugTest::testStaticDebug
- Akamai\Open\EdgeGrid\Tests\Handler\DebugTest::testDebugOverride
- Akamai\Open\EdgeGrid\Tests\Handler\DebugTest::testInstanceOverrideStream
- Akamai\Open\EdgeGrid\Tests\Handler\DebugTest::testDebugMessages
- Akamai\Open\EdgeGrid\Tests\Handler\DebugTest::testResponseNoDetail
- Akamai\Open\EdgeGrid\Tests\Handler\DebugTest::testResponseNoBody
- Akamai\Open\EdgeGrid\Tests\Handler\DebugTest::testResponseNoJsonBody
- Akamai\Open\EdgeGrid\Tests\Handler\DebugTest::testDebugResponseExceptionNoCode
- Akamai\Open\EdgeGrid\Tests\Handler\DebugTest::testVerboseResponseExceptionWithCode
- Akamai\Open\EdgeGrid\Tests\Handler\DebugTest::testVerboseResponseExceptionWithBody
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testInstanceVerboseSingle
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testInstanceVerboseMultiple
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testStaticVerboseSingle
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testStaticVerboseMultiple
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testVerboseOverrideSingle
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testVerboseOverrideMultiple
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testVerboseRedirect
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testVerboseNonJson
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testVerboseRequestHandler
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testVerboseNoResponse
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testVerboseError
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testVerboseErrorNonJson
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testVerboseMixed
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testVerboseResponseExceptionNoCode
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testVerboseResponseExceptionWithCode
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testVerboseResponseExceptionWithBody

37
Covered by 50 test(s):

- Akamai\Open\EdgeGrid\Tests\Client\Authentication\TimestampTest::testTimestampFormat
- Akamai\Open\EdgeGrid\Tests\Client\Authentication\TimestampTest::testIsValid
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testDefaultTimestamp
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testDefaultNonce
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testTimestampTimeout
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testDefaultTimeout
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testTimeoutOption
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testSetTimeout
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testStaticDebugSingle
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testInstanceDebugSingle
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testDebugOverrideSingle
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testInstanceDebugOptionSingle
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testNonApiCall
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testLogging with data set #0
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testLogging with data set #1
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testLogging with data set #2
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testLogging with data set #3
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testLogging with data set #4
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testLogging with data set #5
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testLogging with data set #6
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testLoggingRedirect
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testLoggingRequestHandler
- Akamai\Open\EdgeGrid\Tests\Handler\AuthenticationTest::testSetSimpleLog
- Akamai\Open\EdgeGrid\Tests\Handler\DebugTest::testInstanceDebug
- Akamai\Open\EdgeGrid\Tests\Handler\DebugTest::testStaticDebug
- Akamai\Open\EdgeGrid\Tests\Handler\DebugTest::testDebugOverride
- Akamai\Open\EdgeGrid\Tests\Handler\DebugTest::testInstanceOverrideStream
- Akamai\Open\EdgeGrid\Tests\Handler\DebugTest::testDebugMessages
- Akamai\Open\EdgeGrid\Tests\Handler\DebugTest::testResponseNoDetail
- Akamai\Open\EdgeGrid\Tests\Handler\DebugTest::testResponseNoBody
- Akamai\Open\EdgeGrid\Tests\Handler\DebugTest::testResponseNoJsonBody
- Akamai\Open\EdgeGrid\Tests\Handler\DebugTest::testDebugResponseExceptionNoCode
- Akamai\Open\EdgeGrid\Tests\Handler\DebugTest::testVerboseResponseExceptionWithCode
- Akamai\Open\EdgeGrid\Tests\Handler\DebugTest::testVerboseResponseExceptionWithBody
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testInstanceVerboseSingle
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testInstanceVerboseMultiple
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testStaticVerboseSingle
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testStaticVerboseMultiple
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testVerboseOverrideSingle
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testVerboseOverrideMultiple
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testVerboseRedirect
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testVerboseNonJson
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testVerboseRequestHandler
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testVerboseNoResponse
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testVerboseError
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testVerboseErrorNonJson
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testVerboseMixed
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testVerboseResponseExceptionNoCode
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testVerboseResponseExceptionWithCode
- Akamai\Open\EdgeGrid\Tests\Handler\VerboseTest::testVerboseResponseExceptionWithBody

38 39 40 41 42 43 44 45 46
Covered by 4 test(s):

- Akamai\Open\EdgeGrid\Tests\Client\Authentication\TimestampTest::testIsValid
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testDefaultTimestamp
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testDefaultNonce
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testTimestampTimeout

47
Covered by 4 test(s):

- Akamai\Open\EdgeGrid\Tests\Client\Authentication\TimestampTest::testIsValid
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testDefaultTimestamp
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testDefaultNonce
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testTimestampTimeout

48 49
Covered by 4 test(s):

- Akamai\Open\EdgeGrid\Tests\Client\Authentication\TimestampTest::testIsValid
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testDefaultTimestamp
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testDefaultNonce
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testTimestampTimeout

50 51 52 53 54 55 56 57 58 59 60 61
Covered by 2 test(s):

- Akamai\Open\EdgeGrid\Tests\Client\Authentication\TimestampTest::testIsValid
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testTimestampTimeout

62
Covered by 2 test(s):

- Akamai\Open\EdgeGrid\Tests\Client\Authentication\TimestampTest::testIsValid
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testTimestampTimeout

63 64 65 66 67 68 69 70 71 72
Covered by 3 test(s):

- Akamai\Open\EdgeGrid\Tests\Client\Authentication\TimestampTest::testTimestampFormat
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testDefaultTimestamp
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testDefaultNonce

73 74 75

    <?php

    /**

     * Akamai {OPEN} EdgeGrid Auth for PHP

     *

     * @author Davey Shafik <dshafik@akamai.com>

     * @copyright Copyright 2015 Akamai Technologies, Inc. All rights reserved.

     * @license Apache 2.0

     * @link https://github.com/akamai-open/edgegrid-auth-php

     * @link https://developer.akamai.com

     * @link https://developer.akamai.com/introduction/Client_Auth.html

     */

    namespace Akamai\Open\EdgeGrid\Authentication;

    /**

     * Generates an Akamai formatted Date for each request

     *

     * @package Akamai {OPEN} EdgeGrid Auth

     */

    class Timestamp

    {

        /**

         * @var \DateTime Signing Timestamp

         */

        protected $timestamp;

        /**

         * @var string \DateInterval spec

         */

        protected $validFor = 'PT10S';

        /**

         * Signing Timestamp

         */

        public function __construct()

        {

            $this->timestamp = new \DateTime("now", new \DateTimeZone('UTC'));

        }

        /**

         * Return true is timestamp is less than 10s old

         *

         * @return bool

         */

        public function isValid()

        {

            $now = new \DateTime("now", new \DateTimeZone('UTC'));

            $timestamp = clone $this->timestamp;

            return $timestamp->add(new \DateInterval($this->validFor)) >= $now;

        }

        /**

         * Set how long the current timestamp is considered valid

         *

         * @see \DateInterval

         * @param string $interval A \DateInterval time spec

         * @return $this

         */

        public function setValidFor($interval)

        {

            $this->validFor = $interval;

            return $this;

        }

        /**

         * Return the timestamp when cast to string

         *

         * @return string Returns the date

         */

        public function __toString()

        {

            return $this->timestamp->format('Ymd\TH:i:sO');

        }

    }