Timeout::getDateInterval()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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 3
    public function __construct($timeout)
22
    {
23 3
        $this->timeout = $timeout;
24 3
    }
25
26
    /**
27
     * @return string
28
     */
29 1
    public function getName()
30
    {
31 1
        return 'Timeout: ' . $this->timeout;
32
    }
33
34
    /**
35
     * @return \DateInterval
36
     */
37 2
    public function getDateInterval()
38
    {
39 2
        return \DateInterval::createFromDateString($this->timeout);
40
    }
41
42
    /**
43
     * @param object       $subject
44
     * @param \ArrayAccess $context
45
     *
46
     * @return \DateTime
47
     */
48 2
    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 2
        if ($subject instanceof LastStateHasChangedDateInterface) {
51 2
            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 2
    public function checkCondition($subject, \ArrayAccess $context)
61
    {
62
        // clone date to not change original object
63 2
        $date = clone $this->getLastStateHasChangedDate($subject, $context);
64 2
        $date->add($this->getDateInterval());
65
66 2
        return ($date <= new \DateTime());
67
    }
68
}
69