Completed
Push — master ( 0aa8ac...a01137 )
by Bret R.
01:24
created

LogfileContentCheck   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 13.16 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setCheckfor() 0 4 1
A checkStatus() 5 20 4

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
namespace BretRZaun\StatusPage\Check;
3
4
use BretRZaun\StatusPage\Result;
5
6
class LogfileContentCheck extends AbstractCheck
7
{
8
9
    protected $filename;
10
    protected $content;
11
12
    public function __construct(string $label, string $filename)
13
    {
14
        parent::__construct($label);
15
        $this->filename = $filename;
16
    }
17
18
    public function setCheckfor($content): void
19
    {
20
        $this->content = $content;
21
    }
22
23
    public function checkStatus(): Result
24
    {
25
        $result = new Result($this->label);
26
27 View Code Duplication
        if (!file_exists($this->filename)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
28
            $result->setSuccess(false);
29
            $result->setError("Log file $this->filename does not exist!");
30
            return $result;
31
        }
32
33
        if (!empty($this->content)) {
34
            $fileContent = file_get_contents($this->filename);
35
            if (false === strpos($fileContent, $this->content)) {
36
                $result->setSuccess(false);
37
                $result->setError('Log file failure');
38
                $result->setDetails('Timestamp: '.date('d.m.Y H:i', filemtime($this->filename)));
39
            }
40
        }
41
        return $result;
42
    }
43
}
44