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

PostponeRuntimeBackend   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 9
c 3
b 2
f 0
lcom 1
cbo 3
dl 0
loc 88
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 0 4 1
A getStatus() 0 4 1
A publish() 0 12 3
A onEvent() 0 10 2
A __construct() 0 6 1
A isCommandLineInterface() 0 4 1
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\Iterator\IteratorProxyMessageIterator;
15
use Sonata\NotificationBundle\Model\MessageInterface;
16
use Symfony\Component\EventDispatcher\Event;
17
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
18
use ZendDiagnostics\Result\Success;
19
20
/**
21
 * This backend postpones the handling of messages to a registered event.
22
 *
23
 * It's based on the asynchronous event dispatcher:
24
 *
25
 * @link https://gist.github.com/3852361
26
 *
27
 * @author Toni Uebernickel <[email protected]>
28
 */
29
class PostponeRuntimeBackend extends RuntimeBackend
30
{
31
    /**
32
     * @var MessageInterface[]
33
     */
34
    protected $messages = array();
35
36
    /**
37
     * If set to true, you have to fire an event the onEvent method is subscribed to manually!
38
     *
39
     * @var bool
40
     */
41
    protected $postponeOnCli = false;
42
43
    /**
44
     * Constructor.
45
     *
46
     * @param EventDispatcherInterface $dispatcher
47
     * @param bool                     $postponeOnCli Whether to postpone the messages on the CLI, too.
48
     */
49
    public function __construct(EventDispatcherInterface $dispatcher, $postponeOnCli = false)
50
    {
51
        parent::__construct($dispatcher);
52
53
        $this->postponeOnCli = $postponeOnCli;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function publish(MessageInterface $message)
60
    {
61
        // if the message is generated from the cli the message is handled
62
        // directly as there is no kernel.terminate in cli
63
        if (!$this->postponeOnCli && $this->isCommandLineInterface()) {
64
            $this->handle($message, $this->dispatcher);
65
66
            return;
67
        }
68
69
        $this->messages[] = $message;
70
    }
71
72
    /**
73
     * Listen on any event and handle the messages.
74
     *
75
     * Actually, an event is not necessary, you can call this method manually, to.
76
     * The event is not processed in any way.
77
     *
78
     * @param Event|null $event
79
     */
80
    public function onEvent(Event $event = null)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
    {
82
        reset($this->messages);
83
84
        while (list($key, $message) = each($this->messages)) {
85
            $this->handle($message, $this->dispatcher);
86
87
            unset($this->messages[$key]);
88
        }
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getIterator()
95
    {
96
        return new IteratorProxyMessageIterator(new \ArrayIterator($this->messages));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Sonata\Notif...ator($this->messages)); (Sonata\NotificationBundl...torProxyMessageIterator) 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...
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function getStatus()
103
    {
104
        return new Success('Postpone runtime backend', 'Ok (Postpone Runtime)');
105
    }
106
107
    /**
108
     * Check whether this Backend is run on the CLI.
109
     *
110
     * @return bool
111
     */
112
    protected function isCommandLineInterface()
113
    {
114
        return 'cli' === PHP_SAPI;
115
    }
116
}
117