Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
103 - 1
<?php
2
/*
3
 * Copyright 2016 Google Inc.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
 
18
namespace Google\Auth\Cache;
19
 
20
use Psr\Cache\CacheItemInterface;
21
 
22
/**
23
 * A cache item.
24
 */
25
final class Item implements CacheItemInterface
26
{
27
    /**
28
     * @var string
29
     */
30
    private $key;
31
 
32
    /**
33
     * @var mixed
34
     */
35
    private $value;
36
 
37
    /**
38
     * @var \DateTime
39
     */
40
    private $expiration;
41
 
42
    /**
43
     * @var bool
44
     */
45
    private $isHit = false;
46
 
47
    /**
48
     * @param string $key
49
     */
50
    public function __construct($key)
51
    {
52
        $this->key = $key;
53
    }
54
 
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getKey()
59
    {
60
        return $this->key;
61
    }
62
 
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function get()
67
    {
68
        return $this->isHit() ? $this->value : null;
69
    }
70
 
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function isHit()
75
    {
76
        if (!$this->isHit) {
77
            return false;
78
        }
79
 
80
        if ($this->expiration === null) {
81
            return true;
82
        }
83
 
84
        return new \DateTime() < $this->expiration;
85
    }
86
 
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function set($value)
91
    {
92
        $this->isHit = true;
93
        $this->value = $value;
94
 
95
        return $this;
96
    }
97
 
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function expiresAt($expiration)
102
    {
103
        if ($this->isValidExpiration($expiration)) {
104
            $this->expiration = $expiration;
105
 
106
            return $this;
107
        }
108
 
109
        $implementationMessage = interface_exists('DateTimeInterface')
110
            ? 'implement interface DateTimeInterface'
111
            : 'be an instance of DateTime';
112
 
113
        $error = sprintf(
114
            'Argument 1 passed to %s::expiresAt() must %s, %s given',
115
            get_class($this),
116
            $implementationMessage,
117
            gettype($expiration)
118
        );
119
 
120
        $this->handleError($error);
121
    }
122
 
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function expiresAfter($time)
127
    {
128
        if (is_int($time)) {
129
            $this->expiration = new \DateTime("now + $time seconds");
130
        } elseif ($time instanceof \DateInterval) {
131
            $this->expiration = (new \DateTime())->add($time);
132
        } elseif ($time === null) {
133
            $this->expiration = $time;
134
        } else {
135
            $message = 'Argument 1 passed to %s::expiresAfter() must be an ' .
136
                       'instance of DateInterval or of the type integer, %s given';
137
            $error = sprintf($message, get_class($this), gettype($expiration));
138
 
139
            $this->handleError($error);
140
        }
141
 
142
        return $this;
143
    }
144
 
145
    /**
146
     * Handles an error.
147
     *
148
     * @param string $error
149
     * @throws \TypeError
150
     */
151
    private function handleError($error)
152
    {
153
        if (class_exists('TypeError')) {
154
            throw new \TypeError($error);
155
        }
156
 
157
        trigger_error($error, E_USER_ERROR);
158
    }
159
 
160
    /**
161
     * Determines if an expiration is valid based on the rules defined by PSR6.
162
     *
163
     * @param mixed $expiration
164
     * @return bool
165
     */
166
    private function isValidExpiration($expiration)
167
    {
168
        if ($expiration === null) {
169
            return true;
170
        }
171
 
172
        // We test for two types here due to the fact the DateTimeInterface
173
        // was not introduced until PHP 5.5. Checking for the DateTime type as
174
        // well allows us to support 5.4.
175
        if ($expiration instanceof \DateTimeInterface) {
176
            return true;
177
        }
178
 
179
        if ($expiration instanceof \DateTime) {
180
            return true;
181
        }
182
 
183
        return false;
184
    }
185
}