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

TimeAwareThrottlerFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 77
c 0
b 0
f 0
wmc 7
lcom 1
cbo 10
ccs 41
cts 41
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createThrottler() 0 11 2
B createNestableController() 0 38 4
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\Factory;
27
28
use Sunspikes\Ratelimit\Cache\Adapter\CacheAdapterInterface;
29
use Sunspikes\Ratelimit\Throttle\Entity\Data;
30
use Sunspikes\Ratelimit\Throttle\Settings\FixedWindowSettings;
31
use Sunspikes\Ratelimit\Throttle\Settings\LeakyBucketSettings;
32
use Sunspikes\Ratelimit\Throttle\Settings\MovingWindowSettings;
33
use Sunspikes\Ratelimit\Throttle\Settings\RetrialQueueSettings;
34
use Sunspikes\Ratelimit\Throttle\Settings\ThrottleSettingsInterface;
35
use Sunspikes\Ratelimit\Throttle\Throttler\FixedWindowThrottler;
36
use Sunspikes\Ratelimit\Throttle\Throttler\LeakyBucketThrottler;
37
use Sunspikes\Ratelimit\Throttle\Throttler\MovingWindowThrottler;
38
use Sunspikes\Ratelimit\Throttle\Throttler\RetrialQueueThrottler;
39
use Sunspikes\Ratelimit\Throttle\Throttler\ThrottlerInterface;
40
use Sunspikes\Ratelimit\Time\TimeAdapterInterface;
41
42
class TimeAwareThrottlerFactory extends ThrottlerFactory
43
{
44
    /**
45
     * @var TimeAdapterInterface
46
     */
47
    private $timeAdapter;
48
49
    /**
50
     * @param CacheAdapterInterface $cacheAdapter
51
     * @param TimeAdapterInterface  $timeAdapter
52
     */
53 29
    public function __construct(CacheAdapterInterface $cacheAdapter, TimeAdapterInterface $timeAdapter)
54
    {
55 29
        parent::__construct($cacheAdapter);
56 29
        $this->timeAdapter = $timeAdapter;
57 29
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62 28
    protected function createThrottler(Data $data, ThrottleSettingsInterface $settings)
63
    {
64 28
        if ($settings instanceof RetrialQueueSettings) {
65 6
            return new RetrialQueueThrottler(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Sunspikes\Ra...), $this->timeAdapter); (Sunspikes\Ratelimit\Thro...r\RetrialQueueThrottler) is incompatible with the return type of the parent method Sunspikes\Ratelimit\Thro...actory::createThrottler of type Sunspikes\Ratelimit\Thro...\ElasticWindowThrottler.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
66 6
                $this->createNestableController($data, $settings->getInternalThrottlerSettings()),
0 ignored issues
show
Compatibility introduced by
$this->createNestableCon...nalThrottlerSettings()) of type object<Sunspikes\Ratelim...ler\ThrottlerInterface> is not a sub-type of object<Sunspikes\Ratelim...ableThrottlerInterface>. It seems like you assume a child interface of the interface Sunspikes\Ratelimit\Thro...tler\ThrottlerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
67 6
                $this->timeAdapter
68 6
            );
69
        }
70
71 22
        return $this->createNestableController($data, $settings);
72
    }
73
74
    /**
75
     * @param Data                      $data
76
     * @param ThrottleSettingsInterface $settings
77
     *
78
     * @return ThrottlerInterface
79
     */
80 28
    private function createNestableController(Data $data, ThrottleSettingsInterface $settings)
81
    {
82 28
        if ($settings instanceof LeakyBucketSettings) {
83 6
            return new LeakyBucketThrottler(
84 6
                $this->cacheAdapter,
85 6
                $this->timeAdapter,
86 6
                $data->getKey(),
87 6
                $settings->getTokenLimit(),
88 6
                $settings->getTimeLimit(),
89 6
                $settings->getThreshold(),
90 6
                $settings->getCacheTtl()
91 6
            );
92
        }
93
94 22
        if ($settings instanceof MovingWindowSettings) {
95 8
            return new MovingWindowThrottler(
96 8
                $this->cacheAdapter,
97 8
                $this->timeAdapter,
98 8
                $data->getKey(),
99 8
                $settings->getHitLimit(),
100 8
                $settings->getTimeLimit(),
101 8
                $settings->getCacheTtl()
102 8
            );
103
        }
104
105 14
        if ($settings instanceof FixedWindowSettings) {
106 12
            return new FixedWindowThrottler(
107 12
                $this->cacheAdapter,
108 12
                $this->timeAdapter,
109 12
                $data->getKey(),
110 12
                $settings->getHitLimit(),
111 12
                $settings->getTimeLimit(),
112 12
                $settings->getCacheTtl()
113 12
            );
114
        }
115
116 2
        return parent::createThrottler($data, $settings);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (createThrottler() instead of createNestableController()). Are you sure this is correct? If so, you might want to change this to $this->createThrottler().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
117
    }
118
}
119