Completed
Pull Request — master (#203)
by
unknown
40:36
created

ForumThreadSubscription   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 88
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A alreadySubscribed() 0 20 4
B notify() 0 37 4
1
<?php
2
namespace SilverStripe\Forum\Models;
3
4
use SilverStripe\Control\Controller;
5
use SilverStripe\Control\Director;
6
use SilverStripe\Control\Email\Email;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Core\Convert;
9
use SilverStripe\ORM\DataObject;
10
use SilverStripe\ORM\DB;
11
use SilverStripe\ORM\FieldType\DBDatetime;
12
use SilverStripe\Security\Member;
13
14
/**
15
 * Forum Thread Subscription: Allows members to subscribe to this thread
16
 * and receive email notifications when these topics are replied to.
17
 *
18
 * @package forum
19
 * @property DBDatetime LastSent
20
 * @method ForumThread Thread
21
 * @method Member Member
22
 */
23
class ForumThreadSubscription extends DataObject
24
{
25
    /** @var array */
26
    private static $db = array(
27
        "LastSent" => "Datetime"
28
    );
29
30
    /** @var array */
31
    private static $has_one = array(
32
        "Thread" => ForumThread::class,
33
        "Member" => Member::class
34
    );
35
36
    /**
37
     * Checks to see if a Member is already subscribed to this thread
38
     *
39
     * @param int $threadID The ID of the thread to check
40
     * @param int $memberID The ID of the currently logged in member (Defaults to Member::currentUserID())
41
     *
42
     * @return bool true if they are subscribed, false if they're not
43
     */
44
    public static function alreadySubscribed($threadID, $memberID = null)
45
    {
46
        if (!$memberID) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $memberID of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
47
            $memberID = Member::currentUserID();
48
        }
49
50
        $threadID = Convert::raw2sql($threadID);
51
        $memberID = Convert::raw2sql($memberID);
52
53
        if ($threadID == '' || $memberID == '') {
54
            return false;
55
        }
56
57
        return (bool)self::get()->filter(
58
            [
59
                'ThreadID' => $threadID,
60
                'MemberID' => $memberID
61
            ]
62
        )->count();
63
    }
64
65
    /**
66
     * Notifies everybody that has subscribed to this topic that a new post has been added.
67
     * To get emailed, people subscribed to this topic must have visited the forum
68
     * since the last time they received an email
69
     *
70
     * @param Post $post The post that has just been added
71
     */
72
    public static function notify(Post $post)
73
    {
74
        $list = self::get()->filter(
75
            [
76
                'ThreadID' => $post->ThreadID,
77
                'MemberID' => $post->AuthorID
78
            ]
79
        );
80
81
        if (!$list) {
82
            return;
83
        }
84
85
        foreach ($list as $obj) {
86
            $id = Convert::raw2sql((int)$obj->MemberID);
87
88
            // Get the members details
89
            $member     = Member::get()->byID($id);
90
            $adminEmail = Config::inst()->get(Email::class, 'admin_email');
91
92
            if ($member) {
93
                Email::create()
94
                    ->setFrom($adminEmail)
95
                    ->setTo($member->Email)
96
                    ->setSubject(_t('Post.NEWREPLY', 'New reply for {title}', array('title' => $post->Title)))
0 ignored issues
show
Documentation introduced by
array('title' => $post->Title) is of type array<string,?,{"title":"?"}>, but the function expects a string.

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...
97
                    ->setHTMLTemplate('ForumMember_TopicNotification')
98
                    ->setData($member)
99
                    ->setData($post)
100
                    ->setData(
101
                        [
102
                            'UnsubscribeLink' => Controller::join_links(Director::absoluteBaseURL(), $post->Thread()->Forum()->Link(), 'unsubscribe', $post->ID)
103
                        ]
104
                    )
105
                    ->send();
106
            }
107
        }
108
    }
109
110
}
111