RotatingFileHandler   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 12.31 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 8
loc 65
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A add() 8 8 1
A getRotatedFilename() 0 12 2
A getCurrentDateTime() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Moon\Logger\Handler;
6
7
use DateTimeImmutable;
8
use Moon\Logger\Formatter\FormatterInterface;
9
10
class RotatingFileHandler implements HandlerInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    private $filename;
16
    /**
17
     * @var \DateInterval
18
     */
19
    private $rotateEvery;
20
    /**
21
     * @var DateTimeImmutable
22
     */
23
    private $rotationStartedAt;
24
    /**
25
     * @var FormatterInterface
26
     */
27
    private $formatter;
28
29
    /**
30
     * @var string
31
     */
32
    private $defaultCurrentTime = 'now';
33
34
    public function __construct(FormatterInterface $formatter, string $filename, \DateInterval $rotateEvery = null)
35
    {
36
        $this->filename = $filename;
37
        $this->formatter = $formatter;
38
        $this->rotateEvery = $rotateEvery ?: new \DateInterval('P1D');
39
        $this->rotationStartedAt = new DateTimeImmutable('now');
40
    }
41 3
42 View Code Duplication
    public function add(string $name, string $level, $message, array $context = []): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43 3
    {
44 3
        // Format the message
45 3
        $data = $this->formatter->interpolate($name, $level, $message, $context);
46 3
47 3
        // Push it to file
48
        \file_put_contents($this->getRotatedFilename(), $data.PHP_EOL, FILE_APPEND);
49
    }
50
51
    /**
52 2
     * Return the file name appending the $this->rotationStartedAt.
53
     */
54
    private function getRotatedFilename(): string
55 2
    {
56
        // Check if a new file must be used
57
        $now = $this->getCurrentDateTime();
58 2
        if ($now > $this->rotationStartedAt->add($this->rotateEvery)) {
59 2
            $this->rotationStartedAt = $now;
60
        }
61
62
        // Get file name appending data by on $this->rotationEvery
63
        return \preg_replace('/(^.*\/[^.\/]+)(\.[^.\/]+)?$/', "$1_{$this->rotationStartedAt->format('Y-M-d H:m:s')}$2",
64
            $this->filename);
65
    }
66 3
67
    /**
68
     * Return a current DateTime. Extracted for be easily tested.
69 3
     */
70 3
    private function getCurrentDateTime(): DateTimeImmutable
71 1
    {
72
        return new DateTimeImmutable($this->defaultCurrentTime);
73
    }
74
}
75