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\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Task\Storage\StorageInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Run pending tasks. |
14
|
|
|
* |
15
|
|
|
* @author @wachterjohannes <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class DebugTasksCommand extends Command |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var StorageInterface |
21
|
|
|
*/ |
22
|
|
|
private $storage; |
23
|
|
|
|
24
|
|
|
public function __construct($name, StorageInterface $storage) |
25
|
|
|
{ |
26
|
|
|
parent::__construct($name); |
27
|
|
|
|
28
|
|
|
$this->storage = $storage; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
protected function configure() |
35
|
|
|
{ |
36
|
|
|
$this->setDescription('Debug tasks') |
37
|
|
|
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, '', null) |
38
|
|
|
->addOption('key', 'k', InputOption::VALUE_REQUIRED, '', null); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
45
|
|
|
{ |
46
|
|
|
$key = $input->getOption('key'); |
47
|
|
|
$limit = $input->getOption('limit'); |
48
|
|
|
|
49
|
|
|
if (null !== $key) { |
50
|
|
|
$tasks = $this->storage->findByKey($key, $limit); |
|
|
|
|
51
|
|
|
} else { |
52
|
|
|
$tasks = $this->storage->findAll($limit); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$table = new Table($output); |
56
|
|
|
$table->setHeaders(array('uuid', 'key', 'task-name', 'execution-date', 'completed', 'start', 'duration')); |
57
|
|
|
|
58
|
|
|
foreach ($tasks as $task) { |
59
|
|
|
$start = null; |
60
|
|
|
$duration = null; |
61
|
|
|
if ($task->getLastExecution()) { |
62
|
|
|
$start = $task->getLastExecution()->getFinishedAtAsDateTime()->format(\DateTime::RFC3339); |
63
|
|
|
$duration = $task->getLastExecution()->getExecutionDuration(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$table->addRow( |
67
|
|
|
[ |
68
|
|
|
$task->getUuid(), |
69
|
|
|
$task->getKey(), |
70
|
|
|
$task->getTaskName(), |
71
|
|
|
$task->getExecutionDate()->format(\DateTime::RFC3339), |
72
|
|
|
$task->isCompleted(), |
73
|
|
|
$start, |
74
|
|
|
$duration, |
75
|
|
|
] |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$table->render(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
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: