Test Failed
Push — main ( 2b2a12...672d0a )
by Michiel
06:14
created

TstampTask::createProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 5
crap 2
1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the LGPL. For more information please see
18
 * <http://phing.info>.
19
 */
20
21
namespace Phing\Task\System;
22
23
use DateTime;
24
use Exception;
25
use IntlDateFormatter;
26
use Phing\Exception\BuildException;
27
use Phing\Task;
28
29
/**
30
 * Sets properties to the current time, or offsets from the current time.
31
 * The default properties are TSTAMP, DSTAMP and TODAY;.
32
 *
33
 * Based on Ant's Tstamp task.
34
 *
35
 * @author  Michiel Rook <[email protected]>
36
 *
37
 * @since   2.2.0
38
 */
39
class TstampTask extends Task
40
{
41
    /** @var \Phing\Task\System\TstampCustomFormat[] */
42
    private $customFormats = [];
43
44
    /** @var string */
45
    private $prefix = '';
46
47
    /**
48
     * Set a prefix for the properties.
49
     */
50
    public function setPrefix(string $prefix): void
51
    {
52
        $this->prefix = $prefix;
53
54
        if (!empty($this->prefix)) {
55
            $this->prefix .= '.';
56
        }
57
    }
58
59
    /**
60
     * @param TstampCustomFormat $format object representing `<format/>` tag
61
     */
62
    public function addFormat(TstampCustomFormat $format): void
63
    {
64
        $this->customFormats[] = $format;
65
    }
66
67
    public function init(): void
68
    {
69
        // Testing class instead of extension to allow polyfills
70
        if (!class_exists(IntlDateFormatter::class)) {
71
            throw new BuildException('TstampTask requires Intl extension');
72
        }
73
    }
74
75
    /**
76
     * Create the timestamps. Custom ones are done before the standard ones.
77
     */
78
    public function main(): void
79
    {
80
        $unixTime = $this->getUnixTime();
81
82
        foreach ($this->customFormats as $format) {
83
            $format->validate($this);
84
            $this->createProperty($format->propertyName, $unixTime, $format->pattern, $format->locale, $format->timezone);
85
        }
86
87
        $this->createProperty('DSTAMP', $unixTime, 'yyyyMMdd');
88
        $this->createProperty('TSTAMP', $unixTime, 'HHmm');
89
        $this->createProperty('TODAY', $unixTime);
90
    }
91
92
    /**
93
     * @param string      $propertyName  name of the property to be created
94
     * @param int         $unixTimestamp unix timestamp to be converted
95
     * @param null|string $pattern       ICU pattern, when null locale-dependent date pattern is used
96
     * @param null|string $locale        locale to use with timestamp, when null PHP default locale is used
97
     * @param null|string $timezone      timezone to use with timestamp, when null PHP default timezone is used
98
     */
99
    protected function createProperty(string $propertyName, int $unixTimestamp, ?string $pattern = null, ?string $locale = null, ?string $timezone = null): void
100
    {
101
        $formatter = new IntlDateFormatter($locale, IntlDateFormatter::LONG, IntlDateFormatter::NONE, $timezone, IntlDateFormatter::GREGORIAN, $pattern);
102
        $value = $formatter->format($unixTimestamp);
103
        $this->getProject()->setNewProperty($this->prefix . $propertyName, $value);
104
    }
105
106
    protected function getUnixTime(): int
107
    {
108
        // phing.tstamp.now.iso
109
        $property = $this->getProject()->getProperty('phing.tstamp.now.iso');
110
        if (null !== $property && '' !== $property) {
111
            try {
112
                $dateTime = new DateTime($property);
113
114
                return $dateTime->getTimestamp();
115
            } catch (Exception $e) {
116
                $this->log('magic property phing.tstamp.now.iso ignored as ' . $property . ' is not a valid number');
117
            }
118
        }
119
120
        // phing.tstamp.now
121
        $property = $this->getProject()->getProperty('phing.tstamp.now');
122
        if (null !== $property && '' !== $property) {
123
            $dateTime = DateTime::createFromFormat('U', $property);
124
            if ($dateTime instanceof DateTime) {
0 ignored issues
show
introduced by
$dateTime is always a sub-type of DateTime.
Loading history...
125
                return $dateTime->getTimestamp();
126
            }
127
            $this->log('magic property phing.tstamp.now ignored as ' . $property . ' is not a valid number');
128
        }
129
130
        return time();
131
    }
132
}
133