1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Task\TaskBundle\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
6
|
|
|
use Symfony\Component\Console\Helper\Table; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Task\Storage\StorageInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Run pending tasks. |
13
|
|
|
* |
14
|
|
|
* @author @wachterjohannes <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class DebugTasksCommand extends Command |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var StorageInterface |
20
|
|
|
*/ |
21
|
|
|
private $storage; |
22
|
|
|
|
23
|
|
|
public function __construct($name, StorageInterface $storage) |
24
|
|
|
{ |
25
|
|
|
parent::__construct($name); |
26
|
|
|
|
27
|
|
|
$this->storage = $storage; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
protected function configure() |
34
|
|
|
{ |
35
|
|
|
$this->setDescription('Debug tasks') |
36
|
|
|
->addOption('limit', 'l') |
37
|
|
|
->addOption('key'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
44
|
|
|
{ |
45
|
|
|
$key = $input->getOption('key'); |
46
|
|
|
$limit = $input->getOption('limit'); |
47
|
|
|
|
48
|
|
|
if (null !== $key) { |
49
|
|
|
$tasks = $this->storage->findByKey($key, $limit); |
|
|
|
|
50
|
|
|
} else { |
51
|
|
|
$tasks = $this->storage->findAll($limit); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$table = new Table($output); |
55
|
|
|
$table->setHeaders(array('uuid', 'key', 'task-name', 'execution-date', 'completed', 'start', 'duration')); |
56
|
|
|
|
57
|
|
|
foreach ($tasks as $task) { |
58
|
|
|
$start = null; |
59
|
|
|
$duration = null; |
60
|
|
|
if (null !== $task->getLastExecution()) { |
61
|
|
|
$start = $task->getLastExecution()->getFinishedAtAsDateTime()->format(\DateTime::RFC3339); |
62
|
|
|
$duration = $task->getLastExecution()->getExecutionDuration(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$table->addRow( |
66
|
|
|
[ |
67
|
|
|
$task->getUuid(), |
68
|
|
|
$task->getKey(), |
69
|
|
|
$task->getTaskName(), |
70
|
|
|
$task->getExecutionDate()->format(\DateTime::RFC3339), |
71
|
|
|
$task->isCompleted(), |
72
|
|
|
$start, |
73
|
|
|
$duration, |
74
|
|
|
] |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$table->render(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: