Completed
Push — master ( f59019...093bae )
by Guillaume
04:06
created

Handler::isOpen()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the circuit-breaker package
5
 *
6
 * Copyright (c) 2016 Guillaume Cavana
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * Feel free to edit as you please, and have fun.
12
 *
13
 * @author Guillaume Cavana <[email protected]>
14
 */
15
16
namespace Eljam\CircuitBreaker\Handler;
17
18
use Eljam\CircuitBreaker\Circuit;
19
20
/**
21
 * Handler.
22
 */
23
class Handler
24
{
25
    /**
26
     * $config.
27
     *
28
     * @var array
29
     */
30
    protected $config;
31
32
    /**
33
     * Constructor.
34
     *
35
     * @param array $config
36
     */
37
    public function __construct($config)
38
    {
39
        $this->config = $config;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function isClosed($circuit)
46
    {
47
        return $circuit->getFailures() < $this->config['max_failure'];
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function isOpen($circuit)
54
    {
55
        return $circuit->getFailures() >= $this->config['max_failure'];
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function isHalfOpen($circuit)
62
    {
63
        return ($circuit->getFailures() >= $this->config['max_failure'])
64
            && (time() - $circuit->getLastFailure() > $this->config['reset_timeout']);
65
    }
66
}
67