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

Timeout::getDateInterval()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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