Passed
Push — master ( 16bfb9...b081f7 )
by Mike
01:57
created

FileWriter::getLogFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
5
namespace Xervice\Logger\Business\Writer;
6
7
8
use DataProvider\LogMessageDataProvider;
9
10
class FileWriter implements FileWriterInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    private $path;
16
17
    /**
18
     * @var string
19
     */
20
    private $filename;
21
22
    /**
23
     * FileWriter constructor.
24
     *
25
     * @param string $path
26
     * @param string $filename
27
     */
28 1
    public function __construct(string $path, string $filename)
29
    {
30 1
        $this->path = $path;
31 1
        $this->filename = $filename;
32 1
    }
33
34
    /**
35
     * @param \DataProvider\LogMessageDataProvider $messageDataProvider
36
     */
37 1
    public function writeLog(LogMessageDataProvider $messageDataProvider): void
38
    {
39 1
        $this->createFileIfNotExist();
40
41 1
        file_put_contents(
42 1
            $this->getLogFile(),
43 1
            $this->getLogContent($messageDataProvider),
44 1
            FILE_APPEND
45
        );
46 1
    }
47
48
    /**
49
     * @param \DataProvider\LogMessageDataProvider $messageDataProvider
50
     *
51
     * @return string
52
     */
53 1
    private function getLogContent(LogMessageDataProvider $messageDataProvider): string
54
    {
55 1
        return sprintf(
56 1
            '%s - %s%s%s%s%s%s%s%s%s',
57 1
            date('Y-m-d H:i:s'),
58 1
            $messageDataProvider->getTitle(),
59 1
            PHP_EOL,
60 1
            $messageDataProvider->getMessage(),
61 1
            PHP_EOL,
62 1
            PHP_EOL,
63 1
            $messageDataProvider->getContext(),
64 1
            PHP_EOL,
65 1
            PHP_EOL,
66 1
            PHP_EOL
67
        );
68
    }
69
70
    /**
71
     * @return string
72
     */
73 1
    private function getLogFile(): string
74
    {
75 1
        return $this->path . '/' . $this->filename;
76
    }
77
78 1
    private function createFileIfNotExist(): void
79
    {
80 1
        if (!is_file($this->getLogFile())) {
81 1
            touch($this->getLogFile());
82
        }
83
    }
84
}