Completed
Push — master ( 30f5dc...922897 )
by Ryota
04:06
created

ContainerAwareJob::getPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result; (boolean) is incompatible with the return type declared by the interface Tavii\SQSJobQueue\Job\JobInterface::execute of type Tavii\SQSJobQueue\Job\booelan.

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:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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
}