Completed
Push — master ( 741f1a...42bbb8 )
by Bernhard
03:50
created

SelfCheck   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 6
dl 0
loc 34
ccs 0
cts 15
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A autoCheck() 0 15 2
1
<?php
2
3
/**
4
 * \Wicked\Timely\Helper\SelfCheck
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    wick-ed
15
 * @copyright 2020 Bernhard Wick
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/wick-ed/timely
18
 */
19
20
namespace Wicked\Timely\Helper;
21
22
use Symfony\Component\Console\Application;
23
use Symfony\Component\Console\Input\ArrayInput;
24
use Symfony\Component\Console\Output\ConsoleOutput;
25
use \Wicked\Timely\Command\SelfCheck as SelfCheckCommand;
26
use Wicked\Timely\Storage\StorageFactory;
27
28
/**
29
 * SelfCheck helper
30
 *
31
 * @author    wick-ed
32
 * @copyright 2020 Bernhard Wick
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/wick-ed/timely
35
 */
36
class SelfCheck
37
{
38
39
    /**
40
     * Constants being needed
41
     */
42
    const AUTO_SELF_CHECK_TIMESTAMP_FILE = 'last_auto_selfcheck_timestamp';
43
    const AUTO_SELF_CHECK_DELAY = 24*60*60;
44
45
    /**
46
     * Makes an auto-self check but buffers for a certain amount of time
47
     *
48
     * @param Application $application
49
     *
50
     * @return void
51
     *
52
     * @throws \Exception
53
     */
54
    public static function autoCheck(Application $application)
55
    {
56
        $storage = StorageFactory::getStorage();
57
        $lastCheckTimestampFilepath = dirname($storage->getLogFilePath()) .
58
            DIRECTORY_SEPARATOR .
59
            static::AUTO_SELF_CHECK_TIMESTAMP_FILE;
60
        $lastCheckTimestamp = (int) file_get_contents($lastCheckTimestampFilepath);
61
        if ($lastCheckTimestamp < time() - static::AUTO_SELF_CHECK_DELAY) {
62
            $command = $application->find(SelfCheckCommand::COMMAND_NAME);
63
            $input = new ArrayInput([]);
64
            $output = new ConsoleOutput();
65
            $command->run($input, $output);
66
            file_put_contents($lastCheckTimestampFilepath, time());
67
        }
68
    }
69
}
70