Test Failed
Push — master ( 67b70b...c2443c )
by Mike
02:06
created

FileWriter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
eloc 25
dl 0
loc 72
ccs 0
cts 38
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getLogFile() 0 3 1
A writeLog() 0 8 1
A __construct() 0 4 1
A getLogContent() 0 14 1
A createFileIfNotExist() 0 4 2
1
<?php
2
declare(strict_types=1);
3
4
5
namespace Xervice\Logger\Business\Writer;
6
7
8
use DataProvider\LogMessageDataProvider;
0 ignored issues
show
Bug introduced by
The type DataProvider\LogMessageDataProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
    public function __construct(string $path, string $filename)
29
    {
30
        $this->path = $path;
31
        $this->filename = $filename;
32
    }
33
34
    /**
35
     * @param \DataProvider\LogMessageDataProvider $messageDataProvider
36
     */
37
    public function writeLog(LogMessageDataProvider $messageDataProvider): void
38
    {
39
        $this->createFileIfNotExist();
40
41
        file_put_contents(
42
            $this->getLogFile(),
43
            $this->getLogContent($messageDataProvider),
44
            FILE_APPEND
45
        );
46
    }
47
48
    /**
49
     * @param \DataProvider\LogMessageDataProvider $messageDataProvider
50
     *
51
     * @return string
52
     */
53
    private function getLogContent(LogMessageDataProvider $messageDataProvider): string
54
    {
55
        return sprintf(
56
            '%s - %s%s%s%s%s%s%s%s%s',
57
            date('Y-m-d H:i:s'),
58
            $messageDataProvider->getTitle(),
59
            PHP_EOL,
60
            $messageDataProvider->getMessage(),
61
            PHP_EOL,
62
            PHP_EOL,
63
            $messageDataProvider->getContext(),
64
            PHP_EOL,
65
            PHP_EOL,
66
            PHP_EOL
67
        );
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    private function getLogFile(): string
74
    {
75
        return $this->path . '/' . $this->filename;
76
    }
77
78
    private function createFileIfNotExist(): void
79
    {
80
        if (!is_file($this->getLogFile())) {
81
            touch($this->getLogFile());
82
        }
83
    }
84
}