AbstractJob   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 25%

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 3
cts 12
cp 0.25
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A log() 0 7 2
A setUp() 0 1 1
A __call() 0 12 3
A tearDown() 0 1 1
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