PostponeRuntimeBackend::isCommandLineInterface()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\NotificationBundle\Backend;
15
16
use Laminas\Diagnostics\Result\Success;
17
use Sonata\NotificationBundle\Iterator\IteratorProxyMessageIterator;
18
use Sonata\NotificationBundle\Model\MessageInterface;
19
use Symfony\Component\EventDispatcher\Event;
20
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
21
22
/**
23
 * This backend postpones the handling of messages to a registered event.
24
 *
25
 * It's based on the asynchronous event dispatcher:
26
 *
27
 * @see https://gist.github.com/3852361
28
 *
29
 * @author Toni Uebernickel <[email protected]>
30
 */
31
class PostponeRuntimeBackend extends RuntimeBackend
32
{
33
    /**
34
     * @var MessageInterface[]
35
     */
36
    protected $messages = [];
37
38
    /**
39
     * If set to true, you have to fire an event the onEvent method is subscribed to manually!
40
     *
41
     * @var bool
42
     */
43
    protected $postponeOnCli = false;
44
45
    /**
46
     * @param bool $postponeOnCli Whether to postpone the messages on the CLI, too
47
     */
48
    public function __construct(EventDispatcherInterface $dispatcher, $postponeOnCli = false)
49
    {
50
        parent::__construct($dispatcher);
51
52
        $this->postponeOnCli = $postponeOnCli;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function publish(MessageInterface $message): void
59
    {
60
        // if the message is generated from the cli the message is handled
61
        // directly as there is no kernel.terminate in cli
62
        if (!$this->postponeOnCli && $this->isCommandLineInterface()) {
63
            $this->handle($message, $this->dispatcher);
64
65
            return;
66
        }
67
68
        $this->messages[] = $message;
69
    }
70
71
    /**
72
     * Listen on any event and handle the messages.
73
     *
74
     * Actually, an event is not necessary, you can call this method manually, to.
75
     * The event is not processed in any way.
76
     */
77
    public function onEvent(?Event $event = null): void
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...
78
    {
79
        while (!empty($this->messages)) {
80
            $message = array_shift($this->messages);
81
82
            $this->handle($message, $this->dispatcher);
83
        }
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function getIterator()
90
    {
91
        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...
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function getStatus()
98
    {
99
        return new Success('Postpone runtime backend', 'Ok (Postpone Runtime)');
100
    }
101
102
    /**
103
     * Check whether this Backend is run on the CLI.
104
     *
105
     * @return bool
106
     */
107
    protected function isCommandLineInterface()
108
    {
109
        return 'cli' === \PHP_SAPI;
110
    }
111
}
112