Completed
Push — master ( 00b680...38e9d3 )
by Tomas
25:27 queued 10:32
created

SharedFileShutdown   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 40
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A shouldShutdown() 0 13 3
A shutdown() 0 8 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Tomaj\Hermes\Shutdown;
5
6
use DateTime;
7
8
class SharedFileShutdown implements ShutdownInterface
9
{
10
    private $filePath;
11
12
    public function __construct(string $filePath)
13
    {
14
        $this->filePath = $filePath;
15
    }
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function shouldShutdown(DateTime $startTime): bool
21
    {
22
        if (!file_exists($this->filePath)) {
23
            return false;
24
        }
25
26
        $time = filemtime($this->filePath);
27
        if ($time >= $startTime->getTimestamp()) {
28
            return true;
29
        }
30
31
        return false;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     *
37
     * Creates file defined in constructor with modification time `$shutdownTime` (or current DateTime).
38
     */
39
    public function shutdown(DateTime $shutdownTime = null): bool
40
    {
41
        if ($shutdownTime === null) {
42
            $shutdownTime = new DateTime();
43
        }
44
45
        return touch($this->filePath, (int) $shutdownTime->format('U'));
46
    }
47
}
48