Completed
Push — master ( 970adc...5463db )
by Nikola
02:02
created

InMemoryStorage::get()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 2
nop 2
crap 3
1
<?php
2
/**
3
 * This file is part of the Rate Limit package.
4
 *
5
 * Copyright (c) Nikola Posa
6
 *
7
 * For full copyright and license information, please refer to the LICENSE file,
8
 * located at the package root folder.
9
 */
10
11
declare(strict_types=1);
12
13
namespace RateLimit\Storage;
14
15
/**
16
 * @author Nikola Posa <[email protected]>
17
 */
18
final class InMemoryStorage implements StorageInterface
19
{
20
    /**
21
     * @var array
22
     */
23
    protected $store = [];
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 10
    public function get(string $key, $default = false)
29
    {
30
        if (
31 10
            !$this->has($key)
32 10
            || $this->hasExpired($key)
33
        ) {
34 8
            return $default;
35
        }
36
37 8
        return $this->store[$key]['data'];
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 10
    public function set(string $key, $value, int $ttl)
44
    {
45 10
        $this->store[$key] = [
46 10
            'data' => $value,
47 10
            'expires' => time() + $ttl,
48
        ];
49 10
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 7
    public function increment(string $key, int $by)
55
    {
56 7
        $this->store[$key]['data'] += $by;
57 7
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 8 View Code Duplication
    public function ttl(string $key) : int
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
    {
64 8
        if (!isset($this->store[$key]['expires'])) {
65 1
            return -1;
66
        }
67
68 7
        return max($this->store[$key]['expires'] - time(), 0);
69
    }
70
71 10
    private function has(string $key) : bool
72
    {
73 10
        return array_key_exists($key, $this->store);
74
    }
75
76 9 View Code Duplication
    private function hasExpired(string $key) : bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78 9
        if (!isset($this->store[$key]['expires'])) {
79
            return false;
80
        }
81
82 9
        return time() > $this->store[$key]['expires'];
83
    }
84
}
85