Completed
Pull Request — master (#6)
by
unknown
03:54
created

BuiltInFunctionsExtension::currentUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
use Symfony\Component\Security\Core\SecurityContextInterface;
9
10
class BuiltInFunctionsExtension extends AbstractTableBundleExtension
11
{
12
    /**
13
     * @var TokenStorageInterface
14
     */
15
    private $tokenStorage;
16
17
    /**
18
     * @var CurrentDateTimeProvider
19
     */
20
    private $dateTimeProvider;
21
22
    /**
23
     * @param TokenStorageInterface   $tokenStorage
24
     * @param CurrentDateTimeProvider $dateTimeProvider
25
     */
26
    public function __construct(TokenStorageInterface $tokenStorage, CurrentDateTimeProvider $dateTimeProvider)
27
    {
28
        $this->tokenStorage = $tokenStorage;
29
        $this->dateTimeProvider = $dateTimeProvider;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function getName()
36
    {
37
        return 'table_bundle_extension_built_in';
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getFunctions()
44
    {
45
        return [
46
            new TableBundleFunctionExtension('now', $this, 'now'),
47
            new TableBundleFunctionExtension('startOfDay', $this, 'startOfDay'),
48
            new TableBundleFunctionExtension('currentUser', $this, 'currentUser'),
49
            new TableBundleFunctionExtension('random', $this, 'random')
50
        ];
51
    }
52
53
    /**
54
     * Gets the current timestamp, with an offset string
55
     *
56
     * @param string $offset
57
     *
58
     * @return string
59
     * @throws \Exception
60
     */
61
    public function now($offset = null)
62
    {
63
        $now = clone $this->dateTimeProvider->get();
64
65
        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...
66
            $interval = \DateInterval::createFromDateString($offset);
67
            $now->add($interval);
68
69
            if ($now == $this->dateTimeProvider->get()) {
70
                // The date didn't change therefore we assume the given offset is not valid
71
                throw new \Exception($offset . ' is not a valid date/time interval.');
72
            }
73
        }
74
75
        return $now->format(\DateTime::ISO8601);
76
    }
77
78
    /**
79
     * Gets a date with the hour 00:00:00
80
     *
81
     * @param string $date
82
     *
83
     * @return string
84
     * @throws \Exception
85
     */
86
    public function startOfDay($date = null)
87
    {
88
        $now = clone $this->dateTimeProvider->get();
89
90
        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...
91
            $now = $this->modifyDate($now, $date);
92
        }
93
94
        $now->setTime(0, 0, 0);
95
96
        return $now->format(\DateTime::ISO8601);
97
    }
98
99
    /**
100
     * Gets the current users' username
101
     *
102
     * @return string
103
     */
104
    public function currentUser()
105
    {
106
        return $this->tokenStorage->getToken()->getUsername();
107
    }
108
109
    /**
110
     * Gets a random value between $min and $max
111
     *
112
     * @param int $min
113
     * @param int $max
114
     *
115
     * @return int
116
     */
117
    public function random($min = 0, $max = 10)
118
    {
119
        return rand($min, $max);
120
    }
121
122
    /**
123
     * @param \DateTime $date
124
     * @param string    $change
125
     *
126
     * @return \DateTime
127
     * @throws \Exception
128
     */
129
    private function modifyDate(\DateTime $date, $change)
130
    {
131
        try {
132
            $date->modify($change);
133
        } catch (\Exception $e) {
134
            throw new \Exception($change . ' is not a valid date or date offset.');
135
        }
136
137
        return ($date);
138
    }
139
}
140