SendIssueEmailNotificationHandler   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B handle() 0 33 6
1
<?php
2
3
/*
4
 * This file is part of Gitamin.
5
 *
6
 * Copyright (C) 2015-2016 The Gitamin Team
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 Gitamin\Handlers\Events\Issue;
13
14
use Gitamin\Events\Issue\IssueWasAddedEvent;
15
use Gitamin\Models\Subscriber;
16
use Illuminate\Contracts\Mail\MailQueue;
17
use Illuminate\Mail\Message;
18
use McCool\LaravelAutoPresenter\Facades\AutoPresenter;
19
20
class SendIssueEmailNotificationHandler
21
{
22
    /**
23
     * The mailer instance.
24
     *
25
     * @var \Illuminate\Contracts\Mail\Mailer
26
     */
27
    protected $mailer;
28
29
    /**
30
     * The subscriber instance.
31
     *
32
     * @var \Gitamin\Models\Subscriber
33
     */
34
    protected $subscriber;
35
36
    /**
37
     * Create a new send issue email notification handler.
38
     *
39
     * @param \Illuminate\Contracts\Mail\Mailer $mailer
40
     * @param \Gitamin\Models\Subscriber        $subscriber
41
     */
42
    public function __construct(MailQueue $mailer, Subscriber $subscriber)
43
    {
44
        $this->mailer = $mailer;
0 ignored issues
show
Documentation Bug introduced by
It seems like $mailer of type object<Illuminate\Contracts\Mail\MailQueue> is incompatible with the declared type object<Illuminate\Contracts\Mail\Mailer> of property $mailer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
45
        $this->subscriber = $subscriber;
46
    }
47
48
    /**
49
     * Handle the event.
50
     *
51
     * @param \Gitamin\Events\Issue\IssueHasAddedEvent $event
52
     */
53
    public function handle(IssueWasAddedEvent $event)
54
    {
55
        if (! $event->issue->notify) {
0 ignored issues
show
Documentation introduced by
The property notify does not exist on object<Gitamin\Models\Issue>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
56
            //return false;
57
        }
58
59
        $issue = AutoPresenter::decorate($event->issue);
60
        $project = AutoPresenter::decorate($event->issue->project);
0 ignored issues
show
Documentation introduced by
The property project does not exist on object<Gitamin\Models\Issue>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
61
62
        // Only send emails for public issues.
63
        if (1 === 1) {
64
            foreach ($this->subscriber->all() as $subscriber) {
65
                $mail = [
66
                    'email' => $subscriber->email,
67
                    'subject' => 'New issue reported.',
68
                    'has_project' => ($event->issue->project) ? true : false,
0 ignored issues
show
Documentation introduced by
The property project does not exist on object<Gitamin\Models\Issue>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
69
                    'project_name' => $project ? $project->name : null,
70
                    'status' => $issue->humanStatus,
71
                    'html_content' => $issue->formattedMessage,
72
                    'text_content' => $issue->message,
73
                    'token' => $subscriber->token,
74
                    'unsubscribe_link' => route('subscribe.unsubscribe', ['code' => $subscriber->verify_code]),
75
                ];
76
                error_log(var_export($mail, true), 3, '/tmp/mail.log');
77
                $this->mailer->queue([
78
                    'html' => 'emails.issues.new-html',
79
                    'text' => 'emails.issues.new-text',
80
                ], $mail, function (Message $message) use ($mail) {
81
                    $message->to($mail['email'])->subject($mail['subject']);
82
                });
83
            }
84
        }
85
    }
86
}
87