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

ShutdownTrait::shouldShutdown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Tomaj\Hermes\Driver;
5
6
use DateTime;
7
use Tomaj\Hermes\Shutdown\ShutdownException;
8
use Tomaj\Hermes\Shutdown\ShutdownInterface;
9
10
trait ShutdownTrait
11
{
12
    /** @var ShutdownInterface */
13
    private $shutdown;
14
15
    /** @var DateTime */
16
    private $startTime;
17
18
    public function setShutdown(ShutdownInterface $shutdown)
19
    {
20
        $this->shutdown = $shutdown;
21
        $this->startTime = new DateTime();
22
    }
23
24
    private function shouldShutdown(): bool
25
    {
26
        return $this->shutdown !== null && $this->shutdown->shouldShutdown($this->startTime);
27
    }
28
29
    /**
30
     * @throws ShutdownException
31
     */
32
    private function checkShutdown(): void
33
    {
34
        if ($this->shouldShutdown()) {
35
            throw new ShutdownException();
36
        }
37
    }
38
}
39