Completed
Push — master ( d118af...3d5195 )
by Tomas
07:29 queued 04:09
created

RestartTrait::setRestart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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