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

ShutdownTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 29
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setShutdown() 0 5 1
A shouldShutdown() 0 4 2
A checkShutdown() 0 6 2
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