Completed
Push — master ( 517664...b17efa )
by
unknown
02:24
created

BuiltInFunctionsExtension::startOfDay()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.4286
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Netdudes\DataSourceryBundle\Extension;
4
5
use Netdudes\DataSourceryBundle\Extension\Type\TableBundleFunctionExtension;
6
use Netdudes\DataSourceryBundle\Util\CurrentDateTimeProvider;
7
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
8
9
class BuiltInFunctionsExtension extends AbstractTableBundleExtension
10
{
11
    /**
12
     * @var TokenStorageInterface
13
     */
14
    private $tokenStorage;
15
16
    /**
17
     * @var CurrentDateTimeProvider
18
     */
19
    private $dateTimeProvider;
20
21
    /**
22
     * @param TokenStorageInterface   $tokenStorage
23
     * @param CurrentDateTimeProvider $dateTimeProvider
24
     */
25
    public function __construct(TokenStorageInterface $tokenStorage, CurrentDateTimeProvider $dateTimeProvider)
26
    {
27
        $this->tokenStorage = $tokenStorage;
28
        $this->dateTimeProvider = $dateTimeProvider;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function getName()
35
    {
36
        return 'table_bundle_extension_built_in';
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getFunctions()
43
    {
44
        return [
45
            new TableBundleFunctionExtension('now', $this, 'now'),
46
            new TableBundleFunctionExtension('startOfDay', $this, 'startOfDay'),
47
            new TableBundleFunctionExtension('currentUser', $this, 'currentUser'),
48
            new TableBundleFunctionExtension('random', $this, 'random')
49
        ];
50
    }
51
52
    /**
53
     * Gets the current timestamp, with an offset string
54
     *
55
     * @param string $offset
56
     *
57
     * @return string
58
     * @throws \Exception
59
     */
60
    public function now($offset = null)
61
    {
62
        $now = clone $this->dateTimeProvider->get();
63
64
        if ($offset) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $offset of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
65
            $interval = \DateInterval::createFromDateString($offset);
66
            $now->add($interval);
67
68
            if ($now == $this->dateTimeProvider->get()) {
69
                // The date didn't change therefore we assume the given offset is not valid
70
                throw new \Exception($offset . ' is not a valid date/time interval.');
71
            }
72
        }
73
74
        return $now->format(\DateTime::ISO8601);
75
    }
76
77
    /**
78
     * Gets a date with the hour 00:00:00
79
     *
80
     * @param string $date
81
     *
82
     * @return string
83
     * @throws \Exception
84
     */
85
    public function startOfDay($date = null)
86
    {
87
        $now = clone $this->dateTimeProvider->get();
88
89
        if ($date) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $date of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
90
            $now = $this->modifyDate($now, $date);
91
        }
92
93
        $now->setTime(0, 0, 0);
94
95
        return $now->format(\DateTime::ISO8601);
96
    }
97
98
    /**
99
     * Gets the current users' username
100
     *
101
     * @return string
102
     */
103
    public function currentUser()
104
    {
105
        return $this->tokenStorage->getToken()->getUsername();
106
    }
107
108
    /**
109
     * Gets a random value between $min and $max
110
     *
111
     * @param int $min
112
     * @param int $max
113
     *
114
     * @return int
115
     */
116
    public function random($min = 0, $max = 10)
117
    {
118
        return rand($min, $max);
119
    }
120
121
    /**
122
     * @param \DateTime $date
123
     * @param string    $change
124
     *
125
     * @return \DateTime
126
     * @throws \Exception
127
     */
128
    private function modifyDate(\DateTime $date, $change)
129
    {
130
        try {
131
            $date->modify($change);
132
        } catch (\Exception $e) {
133
            throw new \Exception($change . ' is not a valid date or date offset.');
134
        }
135
136
        return ($date);
137
    }
138
}
139