Completed
Push — master ( 553806...856339 )
by Krishnaprasad
02:34
created

RetrialQueueThrottler::getTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * The MIT License (MIT)
4
 *
5
 * Copyright (c) 2015 Krishnaprasad MG <[email protected]>
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in all
15
 * copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 */
25
26
namespace Sunspikes\Ratelimit\Throttle\Throttler;
27
28
use Sunspikes\Ratelimit\Time\TimeAdapterInterface;
29
30
final class RetrialQueueThrottler implements ThrottlerInterface
31
{
32
    /**
33
     * @var ThrottlerInterface
34
     */
35
    private $internalThrottler;
36
37
    /**
38
     * @var TimeAdapterInterface
39
     */
40
    private $timeProvider;
41
42
    /**
43
     * @param RetriableThrottlerInterface $internalThrottler
44
     * @param TimeAdapterInterface        $timeProvider
45
     */
46 12
    public function __construct(RetriableThrottlerInterface $internalThrottler, TimeAdapterInterface $timeProvider)
47
    {
48 12
        $this->internalThrottler = $internalThrottler;
49 12
        $this->timeProvider = $timeProvider;
50 12
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55 3
    public function access()
56
    {
57 3
        $status = $this->check();
58 3
        $this->hit();
59
60 3
        return $status;
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66 8
    public function hit()
67
    {
68 8
        if (0 !== $waitTime = $this->internalThrottler->getRetryTimeout()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sunspikes\Ratelimit\Thro...tler\ThrottlerInterface as the method getRetryTimeout() does only exist in the following implementations of said interface: Sunspikes\Ratelimit\Thro...\ElasticWindowThrottler, Sunspikes\Ratelimit\Thro...er\FixedWindowThrottler, Sunspikes\Ratelimit\Thro...er\LeakyBucketThrottler, Sunspikes\Ratelimit\Thro...r\MovingWindowThrottler.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
69 2
            $this->timeProvider->usleep(1e3 * $waitTime);
70 2
        }
71
72 8
        return $this->internalThrottler->hit();
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78 2
    public function clear()
79
    {
80 2
        $this->internalThrottler->clear();
81 2
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86 3
    public function count()
87
    {
88 3
        return $this->internalThrottler->count();
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94 6
    public function check()
95
    {
96 6
        return $this->internalThrottler->check();
97
    }
98
99
    /**
100
     * @inheritdoc
101
     */
102
    public function getTime()
103
    {
104
        return $this->internalThrottler->getTime();
105
    }
106
107
    /**
108
     * @inheritdoc
109
     */
110
    public function getLimit()
111
    {
112
        return $this->internalThrottler->getLimit();
113
    }
114
}
115