GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

MailManager::getMailPreferencesByEmail()   C
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 24
rs 6.7272
cc 7
eloc 19
nc 5
nop 1
1
<?php
2
/**
3
 * Copyright (c) Enalean, 2011. All Rights Reserved.
4
 *
5
 * This file is a part of Tuleap.
6
 *
7
 * Tuleap is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * Tuleap is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with Tuleap. If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
require_once 'common/mail/Codendi_Mail_Interface.class.php';
22
require_once 'common/mail/Mail.class.php';
23
require_once 'common/mail/Codendi_Mail.class.php';
24
require_once 'common/include/Tuleap_Template.class.php';
25
26
require_once 'common/user/UserManager.class.php';
27
28
/**
29
 * Mail manager is the key interface to send emails in the platform
30
 *
31
 */
32
class MailManager {
33
    
34
    /**
35
     * Prepare the mail according to user preferences
36
     * 
37
     * @param PFUser $user The user to whom send the mail
38
     * 
39
     * @return Mail 
40
     */
41
    public function getMailForUser(PFUser $user) {
42
        $mail = $this->getMailByType($this->getMailPreferencesByUser($user));
43
        $mail->setToUser(array($user));
44
        return $mail;
45
    }
46
    
47
    /**
48
     * Return a mail object depending of the requested format
49
     * 
50
     * @param String $type Type of mail (text or html)
51
     * 
52
     * @return Mail
53
     */
54
    public function getMailByType($type = null) {
55
        $mail = new Codendi_Mail();
0 ignored issues
show
Bug Compatibility introduced by
The expression new \Codendi_Mail(); of type Codendi_Mail adds the type Codendi_Mail to the return on line 60 which is incompatible with the return type documented by MailManager::getMailByType of type Mail.
Loading history...
56
        if ($type == Codendi_Mail_Interface::FORMAT_TEXT) {
57
            $mail = new Mail();
58
        }
59
        $mail->setFrom($this->getConfig('sys_noreply'));
60
        return $mail;
61
    }
62
    
63
    /**
64
     * Return users corresponding to email addresses mapped according to their
65
     * preferences.
66
     * 
67
     * @deprecated
68
     * 
69
     * @param Array $addresses A set of addresses
70
     * 
71
     * @return Array of Array of User
72
     */
73
    public function getMailPreferencesByEmail($addresses) {
74
        $default = Codendi_Mail_Interface::FORMAT_HTML;
75
        $res     = array('html' => array(), 'text' => array());
76
        $um      = $this->getUserManager();
77
        foreach ($addresses as $address) {
78
            $users = $um->getAllUsersByEmail($address);
79
            $pref  = $default;
80
            if (count($users) > 0) {
81
                foreach ($users as $user) {
82
                    $pref_user   = $this->getMailPreferencesByUser($user);
83
                    $user_status = $user->getStatus();
84
                    if ($pref_user != $default && ($user_status == 'A' || $user_status == 'R')) {
85
                        $pref = $pref_user;
86
                        break;
87
                    }
88
                }
89
            } else {
90
                $user = new PFUser(array('user_id' => 0, 'language_id' => $this->getConfig('sys_lang')));
91
                $user->setEmail($address);
92
            }
93
            $res[$pref][] = $user;
94
        }
95
        return $res;
96
    }
97
    
98
    /**
99
     * Returns whether the user wants an HTML or a Text notification
100
     * 
101
     * @param PFUser $user
102
     * 
103
     * @return String
104
     */
105
    public function getMailPreferencesByUser(PFUser $user) {
106
        if ($user->getPreference(Codendi_Mail_Interface::PREF_FORMAT) == Codendi_Mail_Interface::FORMAT_TEXT) {
107
            return Codendi_Mail_Interface::FORMAT_TEXT;
108
        }
109
        return Codendi_Mail_Interface::FORMAT_HTML;
110
    }
111
    
112
    /**
113
     * Returns all possible mail formats
114
     * 
115
     * @return Array
116
     */
117
    public function getAllMailFormats() {
118
        return array(Codendi_Mail_Interface::FORMAT_TEXT, Codendi_Mail_Interface::FORMAT_HTML);
119
    }
120
121
    /**
122
     * Wrapper for configuration access
123
     * 
124
     * @param String $var
125
     * 
126
     * @return String 
127
     */
128
    protected function getConfig($var) {
129
        return ForgeConfig::get($var);
130
    }
131
    
132
    /**
133
     * Wrapper for UserManager
134
     * 
135
     * @return UserManager 
136
     */
137
    protected function getUserManager() {
138
        return UserManager::instance();
139
    }
140
}
141
142
?>
143