FileNotificationType::notify()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
crap 2
1
<?php
2
3
namespace TonicHealthCheck\Incident\Siren\NotificationType;
4
5
use TonicHealthCheck\Incident\IncidentInterface;
6
use TonicHealthCheck\Incident\Siren\Subject\SubjectInterface;
7
8
/**
9
 * Class FileNotificationType.
10
 */
11
class FileNotificationType implements NotificationTypeInterface
12
{
13
    protected $pathMessage;
14
15
    /**
16
     * FileNotificationType constructor.
17
     *
18
     * @param string $pathMessage
19
     */
20 2
    public function __construct($pathMessage)
21
    {
22 2
        $this->setPathMessage($pathMessage);
23 2
    }
24
25
    /**
26
     * @param SubjectInterface  $subject
27
     * @param IncidentInterface $incident
28
     */
29 1
    public function notify(SubjectInterface $subject, IncidentInterface $incident)
30
    {
31 1
        if ($incident->getStatus() != IncidentInterface::STATUS_OK) {
32 1
            file_put_contents($this->getSubjectNotifyFilePath($subject), $incident->getMessage());
33
        }
34 1
    }
35
36
    /**
37
     * @return string
38
     */
39 1
    public function getPathMessage()
40
    {
41 1
        return $this->pathMessage;
42
    }
43
44
    /**
45
     * @param string $pathMessage
46
     */
47 2
    public function setPathMessage($pathMessage)
48
    {
49 2
        if (!is_writable($pathMessage)) {
50 1
            throw FileNotificationTypeException::dirForMessageDoesNotWritable($pathMessage);
51
        }
52 2
        $this->pathMessage = rtrim($pathMessage, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
53 2
    }
54
55
    /**
56
     * @param SubjectInterface $subject
57
     *
58
     * @return string
59
     */
60 1
    public function getSubjectNotifyFilePath(SubjectInterface $subject)
61
    {
62 1
        return $this->getPathMessage().$subject;
63
    }
64
}
65