Completed
Push — develop ( 4bd203...a628f2 )
by
unknown
08:09
created

SendRegistrationNotifications   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 49
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B __invoke() 0 25 3
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2017 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Auth\Listener;
12
13
use Auth\Listener\Events\AuthEvent;
14
use Auth\Options\ModuleOptions;
15
use Core\Mail\MailService;
16
17
/**
18
 * Listener to send registration notifications.
19
 * 
20
 * @author Mathias Gelhausen <[email protected]>
21
 * @todo write test
22
 * @since 0.30
23
 */
24
class SendRegistrationNotifications 
25
{
26
27
    /**
28
     *
29
     *
30
     * @var \Core\Mail\MailService
31
     */
32
    private $mailer;
33
34
    /**
35
     *
36
     *
37
     * @var \Auth\Options\ModuleOptions
38
     */
39
    private $options;
40
41
    public function __construct(MailService $mailer, ModuleOptions $options)
42
    {
43
        $this->mailer = $mailer;
44
        $this->options = $options;
45
    }
46
47
    public function __invoke(AuthEvent $e)
48
    {
49
        /* @var \Core\Mail\HTMLTemplateMessage $mail */
50
        $tmpl = sprintf(
51
            'auth/mail/%s.%s',
52
            $e->getName() == AuthEvent::EVENT_USER_REGISTERED
0 ignored issues
show
Bug introduced by
The method getName does only exist in Auth\Listener\Events\AuthEvent, but not in Exception.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
53
                ? 'new-registration'
54
                : 'user-confirmed',
55
            $this->options->getNotificationLanguage()
56
        );
57
58
        $mail = $this->mailer->get('htmltemplate');
59
        $mail->setTemplate($tmpl);
60
        $mail->setVariable('user', $e->getUser());
0 ignored issues
show
Bug introduced by
The method getUser does only exist in Auth\Listener\Events\AuthEvent, but not in Exception.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
61
        $mail->setTo($this->options->getNotificationEmail());
62
        $mail->renderBodyText(true);
63
64
        try {
65
            $this->mailer->send($mail);
0 ignored issues
show
Documentation introduced by
$mail is of type object|array, but the function expects a string|object<Zend\Mail\Message>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
67
            /* Silently ignore all exceptions.
68
             * @todo Logging
69
             */
70
        }
71
    }
72
}