|
1
|
|
|
<?php |
|
2
|
|
|
namespace Tavii\SQSJobQueueBundle; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
5
|
|
|
use Symfony\Component\Finder\Finder; |
|
6
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
|
7
|
|
|
use Tavii\SQSJobQueue\Job\Job; |
|
8
|
|
|
use Tavii\SQSJobQueueBundle\Event\JobEvent; |
|
9
|
|
|
|
|
10
|
|
|
abstract class ContainerAwareJob extends Job |
|
11
|
|
|
{ |
|
12
|
|
|
private $kernel; |
|
13
|
|
|
|
|
14
|
|
|
protected $name; |
|
15
|
|
|
|
|
16
|
|
|
public function __destruct() |
|
17
|
|
|
{ |
|
18
|
|
|
if ($this->kernel instanceof KernelInterface) { |
|
19
|
|
|
$this->kernel->shutdown(); |
|
20
|
|
|
} |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @return bool |
|
25
|
|
|
*/ |
|
26
|
|
|
public function execute() |
|
27
|
|
|
{ |
|
28
|
|
|
$event = new JobEvent($this); |
|
29
|
|
|
$this->getEventDispatcher()->dispatch(SQSJobQueueEvents::JOB_EXECUTE, $event); |
|
30
|
|
|
$result = $this->run(); |
|
31
|
|
|
$event->setExecutedStatus($result); |
|
32
|
|
|
$this->getEventDispatcher()->dispatch(SQSJobQueueEvents::JOB_RAN, $event); |
|
33
|
|
|
return $result; |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param array $options |
|
38
|
|
|
*/ |
|
39
|
|
|
public function setKernelOptions(array $options = array()) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->args = array_merge($this->args, $options); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return ContainerInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function getContainer() |
|
48
|
|
|
{ |
|
49
|
|
|
if (! $this->kernel instanceof KernelInterface) { |
|
50
|
|
|
$this->kernel = $this->createKernel(); |
|
51
|
|
|
$this->kernel->boot(); |
|
52
|
|
|
} |
|
53
|
|
|
return $this->kernel->getContainer(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return KernelInterface |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function createKernel() |
|
60
|
|
|
{ |
|
61
|
|
|
$finder = new Finder(); |
|
62
|
|
|
$finder->name('*Kernel.php')->depth(0)->in($this->args['kernel.root_dir']); |
|
63
|
|
|
$results = iterator_to_array($finder); |
|
64
|
|
|
$file = current($results); |
|
65
|
|
|
$class = $file->getBasename('.php'); |
|
66
|
|
|
require_once $file; |
|
67
|
|
|
return new $class( |
|
68
|
|
|
isset($this->args['kernel.environment']) ? $this->args['kernel.environment'] : 'dev', |
|
69
|
|
|
isset($this->args['kernel.debug']) ? $this->args['kernel.debug'] : true |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
|
|
final public function getName() |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->name; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
|
|
final public function getPrefix() |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->getContainer()->getParameter('sqs_job_queue.prefix'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return \Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher |
|
92
|
|
|
*/ |
|
93
|
|
|
final protected function getEventDispatcher() |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->getContainer()->get('event_dispatcher'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
} |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.