Completed
Push — master ( b268cf...c6aad7 )
by Oliver
07:45
created

Timeout   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 58
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getName() 0 4 1
A getDateInterval() 0 4 1
A getLastStateHasChangedDate() 0 8 2
A checkCondition() 0 8 1
1
<?php
2
3
namespace Metabor\Statemachine\Condition;
4
5
use MetaborStd\Statemachine\ConditionInterface;
6
use MetaborStd\Statemachine\LastStateHasChangedDateInterface;
7
8
/**
9
 * @author otischlinger
10
 */
11
class Timeout implements ConditionInterface
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $timeout;
17
18
    /**
19
     * @param string $timeout
20
     */
21
    public function __construct($timeout)
22
    {
23
        $this->timeout = $timeout;
24
    }
25
26
    /**
27
     * @return string
28
     */
29
    public function getName()
30
    {
31
        return 'Timeout: ' . $this->timeout;
32
    }
33
34
    /**
35
     * @return \DateInterval
36
     */
37
    public function getDateInterval()
38
    {
39
        return \DateInterval::createFromDateString($this->timeout);
40
    }
41
42
    /**
43
     * @param object       $subject
44
     * @param \ArrayAccess $context
45
     *
46
     * @return \DateTime
47
     */
48
    protected function getLastStateHasChangedDate($subject, \ArrayAccess $context)
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
    {
50
        if ($subject instanceof LastStateHasChangedDateInterface) {
51
            return $subject->getLastStateHasChangedDate();
52
        } else {
53
            throw new \InvalidArgumentException('Overwrite this method to implement a different type!');
54
        }
55
    }
56
57
    /**
58
     * @see MetaborStd\Statemachine.ConditionInterface::checkCondition()
59
     */
60
    public function checkCondition($subject, \ArrayAccess $context)
61
    {
62
        // clone date to not change original object
63
        $date = clone $this->getLastStateHasChangedDate($subject, $context);
64
        $date->add($this->getDateInterval());
65
66
        return ($date <= new \DateTime());
67
    }
68
}
69