Completed
Pull Request — master (#327)
by
unknown
02:16
created

DateTimeDeltaToken   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 49
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A scoreArgument() 0 8 2
A isLast() 0 4 1
A __toString() 0 4 1
1
<?php
2
3
namespace Prophecy\Argument\Token;
4
5
6
class DateTimeDeltaToken implements TokenInterface
7
{
8
    /** @var \DateTime */
9
    private $value;
10
    /** @var integer */
11
    private $delta;
12
13
    public function __construct(\DateTime $value, $delta)
14
    {
15
        $this->value = $value;
16
        $this->delta = $delta;
17
    }
18
19
    /**
20
     * Calculates token match score for provided argument.
21
     *
22
     * @param $argument
23
     *
24
     * @return bool|int
25
     */
26
    public function scoreArgument($argument)
27
    {
28
        if (!$argument instanceof \DateTime) {
29
            return false;
30
        }
31
32
        return abs($argument->getTimestamp() - $this->value->getTimestamp()) < $this->delta;
33
    }
34
35
    /**
36
     * Returns true if this token prevents check of other tokens (is last one).
37
     *
38
     * @return bool|int
39
     */
40
    public function isLast()
41
    {
42
        return false;
43
    }
44
45
    /**
46
     * Returns string representation for token.
47
     *
48
     * @return string
49
     */
50
    public function __toString()
51
    {
52
        return sprintf('Date{%s}~%d', $this->value->format('Y-m-d H:i:s'), $this->delta);
53
    }
54
}