Completed
Push — master ( 60ab0f...029e93 )
by Kanto
28s queued 11s
created

Command::lockTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * Kore : Simple And Minimal Framework
4
 *
5
 */
6
7
namespace Kore;
8
9
use Kore\Log;
10
11
/**
12
 * Command class
13
 *
14
 */
15
abstract class Command
16
{
17
    /**
18
     * command namespace
19
     *
20
     * @var string
21
     */
22
    protected $command;
23
    /**
24
     * command arguments
25
     *
26
     * @var array<string>
27
     */
28
    protected $args = array();
29
    /**
30
     * command options
31
     *
32
     * @var array<string>
33
     */
34
    protected $opts = array();
35
36
    /**
37
     * Processing Execution
38
     *
39
     * The process is implemented in subclasses.
40
     * @return void
41
     */
42
    abstract protected function exec();
43
    
44
    /**
45
     * Main Processing
46
     *
47
     * @param string $command command namespace
48
     * @param array<string> $args command arguments
49
     * @param array<string> $opts command options
50
     * @return void
51
     */
52
    public function main($command, $args, $opts)
53
    {
54
        $this->command = $command;
55
        $this->args = $args;
56
        $this->opts = $opts;
57
        Log::init($this->commandName(), $this->logLevel());
58
59
        $lockfile = TMP_DIR.'/'.$this->commandName().'.lock';
60
        if (file_exists($lockfile) && filemtime($lockfile) + $this->lockTime() >= time()) {
61
            Log::warn('Process is running!');
62
            return;
63
        }
64
        touch($lockfile);
65
66
        Log::debug(sprintf('[START]%s', $this->command));
67
        try {
68
            $this->exec();
69
        } catch (\Exception $e) {
70
            $this->handleError($e);
71
        }
72
        Log::debug(sprintf('[END]%s', $this->command));
73
74
        unlink($lockfile);
75
    }
76
77
    /**
78
     * Get the the command name
79
     *
80
     * The default is the end of the command namespace.
81
     * If you need to customize, please override it with subclasses.
82
     * @return string command name
83
     */
84
    protected function commandName()
85
    {
86
        $namespace = explode('\\', $this->command);
87
        return $namespace[count($namespace) - 1];
88
    }
89
90
    /**
91
     * Get the the log level
92
     *
93
     * The default is Log::LEVEL_DEBUG.
94
     * If you need to customize, please override it with subclasses.
95
     * @return int log level
96
     * @see \Kore\Log
97
     */
98
    protected function logLevel()
99
    {
100
        return Log::LEVEL_DEBUG;
101
    }
102
103
    /**
104
     * Get the lock time
105
     *
106
     * The default is 24 hours.
107
     * If you need to customize, please override it with subclasses.
108
     * @return int lock time
109
     */
110
    protected function lockTime()
111
    {
112
        return 60 * 60 * 24;
113
    }
114
115
    /**
116
     * Handling Errors
117
     *
118
     * If you need to customize the handling of errors, please override it with subclasses.
119
     * @param \Exception $e errors
120
     * @return void
121
     */
122
    protected function handleError($e)
123
    {
124
        Log::error($e->getMessage());
125
    }
126
127
    /**
128
     * Get the the command arguments
129
     *
130
     * If no key is specified, all command arguments are returned.
131
     * @param string|null $key command arguments key
132
     * @param mixed $default default value if there is no value specified in the key
133
     * @return string|array<string>|null command arguments
134
     */
135
    protected function getArg($key = null, $default = null)
136
    {
137
        if ($key === null) {
138
            return $this->args;
139
        }
140
        if (!isset($this->args[$key])) {
141
            return $default;
142
        }
143
        return $this->args[$key];
144
    }
145
146
    /**
147
     * Get the the command options
148
     *
149
     * If no key is specified, all command options are returned.
150
     * @param string|null $key command options key
151
     * @param mixed $default default value if there is no value specified in the key
152
     * @return string|array<string>|null command options
153
     */
154
    protected function getOpt($key = null, $default = null)
155
    {
156
        if ($key === null) {
157
            return $this->opts;
158
        }
159
        if (!isset($this->opts[$key])) {
160
            return $default;
161
        }
162
        return $this->opts[$key];
163
    }
164
}
165