1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JobQueue\Domain\Job; |
4
|
|
|
|
5
|
|
|
use JobQueue\Domain\Task\Task; |
6
|
|
|
use Psr\Log\LoggerAwareTrait; |
7
|
|
|
use Psr\Log\LogLevel; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* |
11
|
|
|
* @method void emergency($message, array $context = []) |
12
|
|
|
* @method void alert($message, array $context = []) |
13
|
|
|
* @method void critical($message, array $context = []) |
14
|
|
|
* @method void error($message, array $context = []) |
15
|
|
|
* @method void warning($message, array $context = []) |
16
|
|
|
* @method void notice($message, array $context = []) |
17
|
|
|
* @method void info($message, array $context = []) |
18
|
|
|
* @method void debug($message, array $context = []) |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractJob implements ExecutableJob |
21
|
|
|
{ |
22
|
|
|
use LoggerAwareTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* |
26
|
|
|
* @param Task $task |
27
|
|
|
*/ |
28
|
|
|
public function setUp(Task $task) {} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @param Task $task |
33
|
|
|
*/ |
34
|
|
|
abstract public function perform(Task $task); |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* @param Task $task |
39
|
|
|
*/ |
40
|
|
|
public function tearDown(Task $task) {} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* |
44
|
|
|
* @param string $message |
45
|
|
|
* @param array $context |
46
|
|
|
* @param string $level |
47
|
|
|
*/ |
48
|
4 |
|
protected function log(string $message, array $context = [], string $level = LogLevel::INFO) |
49
|
|
|
{ |
50
|
4 |
|
if (!$this->logger) { |
51
|
4 |
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->logger->log($level, $message, $context); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Magic method for "log-level methods" |
59
|
|
|
* Eg. `this->alert($message)` |
60
|
|
|
* |
61
|
|
|
* @param $name |
62
|
|
|
* @param $arguments |
63
|
|
|
*/ |
64
|
|
|
public function __call($name, $arguments) |
65
|
|
|
{ |
66
|
|
|
$reflection = new \ReflectionClass(LogLevel::class); |
67
|
|
|
|
68
|
|
|
if (!in_array($name, $reflection->getConstants())) { |
69
|
|
|
throw new \RuntimeException(sprintf('Method "%s" can\'t be called on the job logger')); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$message = array_shift($arguments); |
73
|
|
|
$context = empty($arguments) ? [] : array_shift($arguments); |
74
|
|
|
|
75
|
|
|
$this->logger->$name($message, $context); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|