Completed
Push — master ( 3c1969...5b5792 )
by Schlaefer
03:16 queued 10s
created

InstallerState   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 9 2
A reset() 0 4 1
A set() 0 4 1
A getFile() 0 8 2
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace Installer\Lib;
14
15
use Cake\Filesystem\File;
16
17
/**
18
 * Storage for installer state
19
 */
20
class InstallerState
21
{
22
    /**
23
     * Checks the installer state
24
     *
25
     * @param string $state state to check agains
26
     * @return bool true if installer is in state $state
27
     */
28
    public static function check(string $state): bool
29
    {
30
        $file = self::getFile();
31
        if (!$file->exists()) {
32
            return false;
33
        }
34
35
        return $file->read() === $state;
36
    }
37
38
    /**
39
     * Resets the installer state
40
     *
41
     * @return void
42
     */
43
    public static function reset(): void
44
    {
45
        self::getFile()->delete();
46
    }
47
48
    /**
49
     * Sets the installer state
50
     *
51
     * @param string $state the state
52
     * @return void
53
     */
54
    public static function set(string $state): void
55
    {
56
        self::getFile()->write($state);
57
    }
58
59
    /**
60
     * Gets handle to state storage file
61
     *
62
     * The file is stored as file in writable directory. Cache isn't available
63
     * during the installation.
64
     *
65
     * @return File file handle
66
     * @throws \RuntimeException
67
     */
68
    private static function getFile(): File
69
    {
70
        if (empty(TMP)) {
71
            throw new \RuntimeException('TMP directory not available.', 1560524787);
72
        }
73
74
        return (new File(TMP . 'installer.state'));
75
    }
76
}
77