Completed
Push — master ( 2e271d...4ff4ed )
by
unknown
19s queued 16s
created

SmallWorldMail::sendMails()   C

Complexity

Conditions 8
Paths 14

Size

Total Lines 140

Duplication

Lines 4
Ratio 2.86 %

Importance

Changes 0
Metric Value
cc 8
nc 14
nop 5
dl 4
loc 140
rs 6.7555
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace Xoopsmodules\Smallworld;
2
/**
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * SmallWorld
14
 *
15
 * @copyright    The XOOPS Project (https://xoops.org)
16
 * @copyright    2011 Culex
17
 * @license      GNU GPL (http://www.gnu.org/licenses/gpl-2.0.html/)
18
 * @package      SmallWorld
19
 * @since        1.0
20
 * @author       Michael Albertsen (http://culex.dk) <[email protected]>
21
 */
22
23
require_once XOOPS_ROOT_PATH . '/class/mail/xoopsmultimailer.php';
24
require_once XOOPS_ROOT_PATH . '/class/template.php';
25
26
/**
27
 * Class SmallWorldMail
28
 */
29
class SmallWorldMail
30
{
31
    /* Function to send mails to users based on certain events
32
         * $fromUserID = uid, $toUserID = uid
33
         * $event : 'register' = New user registration,
34
         * 	'complatint' = Complaint agains a wall message,
35
         *  'newavatar' = User has opload new avatar, 'commentToWM' = New comment to your update
36
         * Register, complaint, newavatar is sent only to site admin, commentToWM to owner user of wall update
37
         * Link is optional, defaul null. Could be a link to Userprofile, or singlepage wall update.
38
         * Itemtext is text from comments or complaints to be sent by mail..
39
         * Result: send mail, return true or false
40
         */
41
42
    /**
43
     * @param             $fromUserID
44
     * @param             $toUserID
45
     * @param             $event
46
     * @param null|string $link
47
     * @param array       $data
48
     * @throws \phpmailerException
49
     */
50
    public function sendMails($fromUserID, $toUserID, $event, $link = null, array $data)
51
    {
52
        global $xoopsConfig, $xoopsUser;
53
        $date    = date('m-d-Y H:i:s', time());
54
        $mail    = new \XoopsMultiMailer;
55
        $wall    = new WallUpdates;
56
        $tpl     = new \XoopsTpl();
0 ignored issues
show
Unused Code introduced by
$tpl is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
57
        $message = '';
0 ignored issues
show
Unused Code introduced by
$message is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
58
59
        // From and To user ids
60
        $FromUser        = new \XoopsUser($fromUserID);
61
        $from_avatar     = $wall->Gravatar($fromUserID);
0 ignored issues
show
Deprecated Code introduced by
The method XoopsModules\Smallworld\WallUpdates::Gravatar() has been deprecated.

This method has been deprecated.

Loading history...
62
        $from_avatarlink = "<img class='left' src='" . smallworld_getAvatarLink($fromUserID, $from_avatar) . "' height='90px' width='90px'>";
0 ignored issues
show
Deprecated Code introduced by
The function smallworld_getAvatarLink() has been deprecated.

This function has been deprecated.

Loading history...
63
64
        $ToUser        = new \XoopsUser($toUserID);
65
        $To_avatar     = $wall->Gravatar($toUserID);
0 ignored issues
show
Deprecated Code introduced by
The method XoopsModules\Smallworld\WallUpdates::Gravatar() has been deprecated.

This method has been deprecated.

Loading history...
66
        $To_avatarlink = "<img class='left' src='" . smallworld_getAvatarLink($toUserID, $To_avatar) . "' height='90px' width='90px'>";
0 ignored issues
show
Unused Code introduced by
$To_avatarlink is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Deprecated Code introduced by
The function smallworld_getAvatarLink() has been deprecated.

This function has been deprecated.

Loading history...
67
        // Senders username
68
        $SendName    = $FromUser->getVar('uname');
69
        $SendNameUrl = "<a href='" . XOOPS_URL . '/modules/smallworld/userprofile.php?username=' . $SendName . "'>" . $SendName . '</a>';
70
71
        // Recievers username and email
72
        $RecieveName    = $ToUser->getVar('uname');
73
        $RecieveNameUrl = "<a href='" . XOOPS_URL . '/modules/smallworld/userprofile.php?username=' . $RecieveName . "'>" . $RecieveName . '</a>';
0 ignored issues
show
Unused Code introduced by
$RecieveNameUrl is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
74
75
        // Checking content of 'event' to send right message
76
        if ('register' === $event) {
77
            $subject = _SMALLWORLD_MAIL_REGISTERSUBJECT . $xoopsConfig['sitename'];
78
79
            $registername  = $SendName;
80
            $To_avatarlink = "<img class='left' src='" . smallworld_getAvatarLink($fromUserID, $To_avatar) . "' height='90px' width='90px'>";
0 ignored issues
show
Deprecated Code introduced by
The function smallworld_getAvatarLink() has been deprecated.

This function has been deprecated.

Loading history...
81
82
            $tpl = new \XoopsTpl();
83
            $tpl->assign('registername', $registername);
84
            $tpl->assign('sitename', $xoopsConfig['sitename']);
85
            $tpl->assign('registerurl', $SendNameUrl);
86
            $tpl->assign('registerlink', $To_avatarlink);
87
88
            $lnk        = XOOPS_ROOT_PATH . '/modules/smallworld/language/' . $xoopsConfig['language'] . '/mailTpl/mail_register.tpl';
89
            $message    = $tpl->fetch($lnk);
90
            $mail->Body = $message;
91
            $toMail     = $xoopsConfig['adminmail'];
92
93
            // Send email to admin if red/yellow card has been pressed indicating a "bad" thread has been found.
94
        } elseif ('complaint' === $event) {
95
            $subject = _SMALLWORLD_MAIL_COMPLAINT . $xoopsConfig['sitename'];
96
97
            $senders_id   = $fromUserID;
0 ignored issues
show
Unused Code introduced by
$senders_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
98
            $senders_name = stripslashes($data['byuser']);
99
            $against_user = stripslashes($data['a_user']);
100
            $time         = date('d-m-Y H:i:s', $data['time']);
101
            $link         = stripslashes($data['link']);
102
103
            $tpl = new \XoopsTpl();
104
            $tpl->assign('sendername', $senders_name);
105
            $tpl->assign('against', $against_user);
106
            $tpl->assign('time', $time);
107
            $tpl->assign('link', $link);
108
            $tpl->assign('sitename', $xoopsConfig['sitename']);
109
110
            $lnk        = XOOPS_ROOT_PATH . '/modules/smallworld/language/' . $xoopsConfig['language'] . '/mailTpl/mail_complaint.tpl';
111
            $message    = $tpl->fetch($lnk);
112
            $mail->Body = $message;
113
            $toMail     = $xoopsConfig['adminmail'];
114
        } elseif ('commentToWM' === $event) {
115
            $subject = _SMALLWORLD_MAIL_NEWCOMMENT . $xoopsConfig['sitename'];
116
117
            $ownermessage = stripslashes($this->getOwnerUpdateFromMsgID($data['msg_id_fk']));
118 View Code Duplication
            if (preg_match('/UPLIMAGE/', $ownermessage)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
                $ownmsg       = str_replace('UPLIMAGE ', '', $ownermessage);
120
                $ownermessage = "<img width='300px' src='" . $ownmsg . "' style='margin: 5px 0px;' >";
121
            }
122
123
            $owner            = Smallworld_getOwnerFromComment($data['msg_id_fk']);
124
            $OwnerUser        = new \XoopsUser($owner);
125
            $Owner_avatar     = $wall->Gravatar($owner);
0 ignored issues
show
Bug introduced by
It seems like $owner defined by Smallworld_getOwnerFromC...ent($data['msg_id_fk']) on line 123 can also be of type boolean; however, XoopsModules\Smallworld\WallUpdates::Gravatar() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Deprecated Code introduced by
The method XoopsModules\Smallworld\WallUpdates::Gravatar() has been deprecated.

This method has been deprecated.

Loading history...
126
            $Owner_avatarlink = "<img class='left' src='" . smallworld_getAvatarLink($owner, $Owner_avatar) . "' height='90px' width='90px'>";
0 ignored issues
show
Bug introduced by
It seems like $owner defined by Smallworld_getOwnerFromC...ent($data['msg_id_fk']) on line 123 can also be of type boolean; however, smallworld_getAvatarLink() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Deprecated Code introduced by
The function smallworld_getAvatarLink() has been deprecated.

This function has been deprecated.

Loading history...
127
            $OwnerName        = $OwnerUser->getVar('uname');
128
            $OwnerNameUrl     = "<a href='" . XOOPS_URL . '/modules/smallworld/userprofile.php?username=' . $OwnerName . "'>" . $OwnerName . '</a>';
129
130
            $replylink = "<a href='" . XOOPS_URL . '/modules/smallworld/permalink.php?ownerid=' . $owner . '&updid=' . $data['msg_id_fk'] . "'>";
131
            $replylink .= _SMALLWORLD_SEEANDREPLYHERE . '</a>';
132
133
            $tpl = new \XoopsTpl();
134
            $tpl->assign('recievename', $RecieveName);
135
            $tpl->assign('ownername', $OwnerName);
136
            $tpl->assign('ownernameurl', $OwnerNameUrl);
137
            $tpl->assign('sendname', $SendName);
138
            $tpl->assign('sendnameurl', $SendNameUrl);
139
            $tpl->assign('sitename', $xoopsConfig['sitename']);
140
            $tpl->assign('ownermessage', $ownermessage);
141
            $tpl->assign('from_avatarlink', $from_avatarlink);
142
            $tpl->assign('to_avatarlink', $Owner_avatarlink);
143
            $tpl->assign('itemtext', stripslashes($data['comment']));
144
            $tpl->assign('itemtextdate', $date);
145
            $tpl->assign('replylink', $replylink);
146
            $lnk        = XOOPS_ROOT_PATH . '/modules/smallworld/language/' . $xoopsConfig['language'] . '/mailTpl/mail_newcomment.tpl';
147
            $message    = $tpl->fetch($lnk);
148
            $mail->Body = $message;
149
150
            $toMail = $ToUser->getVar('email');
151
        } elseif ('friendshipfollow' === $event) {
152
            $subject = _SMALLWORLD_MAIL_NEWFRIENDFOLLOWER . $xoopsConfig['sitename'];
153
            $link    = "<a href='" . XOOPS_URL . "/modules/smallworld/index.php'>";
154
            $link    .= _SMALLWORLD_GOTOSMALLWORLDHERE . '</a>';
155
156
            $tpl = new \XoopsTpl();
157
            $tpl->assign('toUser', $RecieveName);
158
            $tpl->assign('date', $date);
159
            $tpl->assign('link', $link);
160
            $tpl->assign('sitename', $xoopsConfig['sitename']);
161
162
            $lnk        = XOOPS_ROOT_PATH . '/modules/smallworld/language/' . $xoopsConfig['language'] . '/mailTpl/mail_attencionneeded.tpl';
163
            $message    = $tpl->fetch($lnk);
164
            $mail->Body = $message;
165
            $toMail     = $ToUser->getVar('email');
166
        } elseif ('tag' === $event) {
167
            $subject = _SMALLWORLD_MAIL_FRIENDTAGGEDYOU . $xoopsConfig['sitename'];
168
            $tpl     = new \XoopsTpl();
169
            $tpl->assign('toUser', $RecieveName);
170
            $tpl->assign('fromUser', $SendName);
171
            $tpl->assign('date', $date);
172
            $tpl->assign('link', $link);
173
            $tpl->assign('sitename', $xoopsConfig['sitename']);
174
175
            $lnk        = XOOPS_ROOT_PATH . '/modules/smallworld/language/' . $xoopsConfig['language'] . '/mailTpl/mail_tag.tpl';
176
            $message    = $tpl->fetch($lnk);
177
            $mail->Body = $message;
178
            $toMail     = $ToUser->getVar('email');
179
        }
180
181
        $mail->isMail();
182
        $mail->isHTML(true);
183
        $mail->addAddress($toMail);
0 ignored issues
show
Bug introduced by
The variable $toMail does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
184
        $mail->Subject = $subject;
0 ignored issues
show
Bug introduced by
The variable $subject does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
185
186
        if (!$mail->send()) {
187
        } else {
188
        }
189
    }
190
191
    /*
192
     From msg_id_fk get userids in the thread and return unique array
193
    */
194
    /**
195
     * @param $msg_id_fk
196
     * @return array
197
     */
198
    public function getPartsFromComment($msg_id_fk)
199
    {
200
        global $xoopsDB;
201
        $parts  = [];
202
        $sql    = 'SELECT uid_fk FROM ' . $xoopsDB->prefix('smallworld_comments') . " WHERE msg_id_fk = '" . $msg_id_fk . "'";
203
        $result = $xoopsDB->queryF($sql);
204
        while ($r = $xoopsDB->fetchArray($result)) {
205
            $parts[] = $r['uid_fk'];
206
        }
207
        return array_unique($parts);
208
    }
209
210
    /**
211
     * @param $msgid
212
     * @return mixed
213
     */
214
    public function getOwnerUpdateFromMsgID($msgid)
215
    {
216
        global $xoopsDB;
217
        $sql    = 'SELECT message FROM ' . $xoopsDB->prefix('smallworld_messages') . " WHERE msg_id = '" . $msgid . "'";
218
        $result = $xoopsDB->queryF($sql);
219
        while ($r = $xoopsDB->fetchArray($result)) {
220
            $message = $r['message'];
221
        }
222
        return $message;
0 ignored issues
show
Bug introduced by
The variable $message does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
223
    }
224
}
225