Passed
Branch master (ae28f9)
by Alexey
03:06
created

EventDispatcherAdapter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 19.05%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 44
ccs 4
cts 21
cp 0.1905
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A dispatchEvent() 0 9 2
A normalizeEventName() 0 7 1
1
<?php declare(strict_types = 1);
2
3
namespace Venta\Mail;
4
5
use Swift_Events_EventObject;
6
use Venta\Contracts\Event\EventDispatcher;
7
8
/**
9
 * Class EventDispatcher
10
 *
11
 * @package Venta\Mail\Events
12
 */
13
class EventDispatcherAdapter extends \Swift_Events_SimpleEventDispatcher
14
{
15
    /**
16
     * @var EventDispatcher
17
     */
18
    private $eventDispatcher;
19
20
    /**
21
     * EventDispatcherAdapter constructor.
22
     *
23
     * @param EventDispatcher $eventDispatcher
24
     */
25 17
    public function __construct(EventDispatcher $eventDispatcher)
26
    {
27 17
        parent::__construct();
28 17
        $this->eventDispatcher = $eventDispatcher;
29 17
    }
30
31
    /**
32
     * @inheritdoc
33
     */
34
    public function dispatchEvent(Swift_Events_EventObject $evt, $target)
35
    {
36
        parent::dispatchEvent($evt, $target);
37
        if ($evt->bubbleCancelled()) {
38
            return;
39
        }
40
        $eventName = $this->normalizeEventName($evt, $target);
41
        $this->eventDispatcher->trigger($eventName, ['swiftEventObject' => $evt]);
0 ignored issues
show
Bug introduced by
The method trigger() does not seem to exist on object<Venta\Contracts\Event\EventDispatcher>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
    }
43
44
    /**
45
     * @param \Swift_Events_EventObject $evt
46
     * @param $target
47
     * @return string
48
     */
49
    protected function normalizeEventName(Swift_Events_EventObject $evt, $target)
50
    {
51
        return strtolower(
52
            str_replace('_', '.', get_class($evt)) .
53
            sprintf(".%s", $target)
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal .%s does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
54
        );
55
    }
56
}