Completed
Push — 3.x-dev-kit ( cc4dbe )
by
unknown
03:15
created

RuntimeBackend   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 10
c 2
b 1
f 0
lcom 1
cbo 6
dl 0
loc 96
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getStatus() 0 4 1
A __construct() 0 4 1
A publish() 0 6 1
A create() 0 9 1
A createAndPublish() 0 4 1
A getIterator() 0 4 1
A initialize() 0 3 1
A cleanup() 0 3 1
A handle() 0 16 2
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\NotificationBundle\Backend;
13
14
use Sonata\NotificationBundle\Consumer\ConsumerEvent;
15
use Sonata\NotificationBundle\Exception\HandlingException;
16
use Sonata\NotificationBundle\Model\Message;
17
use Sonata\NotificationBundle\Model\MessageInterface;
18
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19
use ZendDiagnostics\Result\Success;
20
21
class RuntimeBackend implements BackendInterface
22
{
23
    /**
24
     * @var EventDispatcherInterface
25
     */
26
    protected $dispatcher;
27
28
    /**
29
     * @param EventDispatcherInterface $dispatcher
30
     */
31
    public function __construct(EventDispatcherInterface $dispatcher)
32
    {
33
        $this->dispatcher = $dispatcher;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function publish(MessageInterface $message)
40
    {
41
        $this->handle($message, $this->dispatcher);
42
43
        return $message;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function create($type, array $body)
50
    {
51
        $message = new Message();
52
        $message->setType($type);
53
        $message->setBody($body);
54
        $message->setState(MessageInterface::STATE_OPEN);
55
56
        return $message;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function createAndPublish($type, array $body)
63
    {
64
        return $this->publish($this->create($type, $body));
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getIterator()
71
    {
72
        return new \EmptyIterator();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \EmptyIterator(); (EmptyIterator) is incompatible with the return type declared by the interface Sonata\NotificationBundl...dInterface::getIterator of type Sonata\NotificationBundl...essageIteratorInterface.

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...
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function initialize()
79
    {
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function cleanup()
86
    {
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function handle(MessageInterface $message, EventDispatcherInterface $dispatcher)
93
    {
94
        $event = new ConsumerEvent($message);
95
96
        try {
97
            $dispatcher->dispatch($message->getType(), $event);
98
99
            $message->setCompletedAt(new \DateTime());
100
            $message->setState(MessageInterface::STATE_DONE);
101
        } catch (\Exception $e) {
102
            $message->setCompletedAt(new \DateTime());
103
            $message->setState(MessageInterface::STATE_ERROR);
104
105
            throw new HandlingException('Error while handling a message: '.$e->getMessage(), 0, $e);
106
        }
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function getStatus()
113
    {
114
        return new Success('Runtime backend health check', 'Ok  (Runtime)');
115
    }
116
}
117