FileCreatedAfterCheck   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 15
c 2
b 0
f 0
dl 0
loc 43
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 13 4
A init() 0 5 1
A getIdentifier() 0 3 1
1
<?php
2
3
namespace Leankoala\HealthFoundation\Check\Files;
4
5
use Leankoala\HealthFoundation\Check\Check;
6
use Leankoala\HealthFoundation\Check\Result;
7
8
9
/**
10
 * Class FileCreatedAfterCheck
11
 *
12
 * This class checks if there is a file in a given directory that was created after a defined date.
13
 *
14
 * Use case 1: Is there a new backup file for a given database
15
 *
16
 * @package Leankoala\HealthFoundation\Check\Files
17
 */
18
class FileCreatedAfterCheck implements Check
19
{
20
    const IDENTIFIER = 'base:files:fileCreatedAfter';
21
22
    private $directory;
23
24
    private $pattern;
25
26
    /**
27
     * @var \DateTime
28
     */
29
    private $date;
30
31
    public function run()
32
    {
33
        $files = glob(rtrim($this->directory, '/') . DIRECTORY_SEPARATOR . $this->pattern);
34
35
        foreach ($files as $file) {
36
            if (is_file($file)) {
37
                if (filemtime($file) >= $this->date->getTimestamp()) {
38
                    return new Result(Result::STATUS_PASS, 'At least one file (name: ' . basename($file) . ', created: ' . date('Y-d-m H:i', filemtime($file)) . ') was created after ' . $this->date->format('Y-m-d H:m') . '.');
39
                }
40
            }
41
        }
42
43
        return new Result(Result::STATUS_FAIL, 'No file was found newer than ' . $this->date->format('Y-m-d H:m:s') . '.');
44
    }
45
46
    /**
47
     * @param string $directory the directory where to look for the file
48
     * @param \DateTime $date the date the file must be newer than
49
     * @param string $pattern if not all files should be analyzed there can be set a filter
50
     */
51
    public function init($directory, \DateTime $date, $pattern = '*')
52
    {
53
        $this->directory = $directory;
54
        $this->date = $date;
55
        $this->pattern = $pattern;
56
    }
57
58
    public function getIdentifier()
59
    {
60
        return self::IDENTIFIER;
61
    }
62
}
63