Passed
Push — MODEL_LIB_240928 ( d6fbb6 )
by Rafael
48:22
created

Mailing::setNoEmail()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 18
rs 9.9
1
<?php
2
3
/* Copyright (C) 2024      Rafael San José      <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace Dolibarr\Code\Contact\Trait;
20
21
use Dolibarr\Code\Contact\Classes\MailingUnsubscribe;
22
use Exception;
23
24
trait Mailing
25
{
26
    /**
27
     *  Set "blacklist" mailing status
28
     *
29
     * @param bool $no_email 1=Do not send mailing, 0=Ok to receive mailing
30
     *
31
     * @return int                 Return integer <0 if KO, >0 if OK
32
     * @throws Exception
33
     */
34
    public function setNoEmail(bool $no_email): int
35
    {
36
        if (!$this->email) {
37
            dol_syslog('Called to Mailing::setNoEmail with no email');
38
            return 0;
39
        }
40
41
        $entities = [getEntity('mailing', 0)];
42
        $this->no_email = $no_email;
43
44
        if ($no_email) {
45
            dol_syslog('setNoEmail: unsubscribeEmails(' . $this->email . ')');
46
            return MailingUnsubscribe::unsubscribeEmail($this->email, $entities);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Dolibarr\Code\Con...this->email, $entities) returns the type boolean which is incompatible with the type-hinted return integer.
Loading history...
47
        }
48
49
        dol_syslog('setNoEmail: subscribeEmail(' . $this->email . ')');
50
        MailingUnsubscribe::removeUnsubscriptionEmail($this->email, $entities);
51
        return 1;
52
    }
53
54
    /**
55
     *  get "blacklist" mailing status
56
     *  set no_email attribute to 1 or 0
57
     *
58
     * @return bool                 Return integer <0 if KO, >0 if OK
59
     * @throws Exception
60
     */
61
    public function getNoEmail(): bool
62
    {
63
        if (!$this->email) {
64
            dol_syslog('Called to Mailing::getNoEmail with no email');
65
            return false;
66
        }
67
        $found = MailingUnsubscribe::unsubscribedEmail($this->email, [getEntity('mailing', 0)]);
68
        dol_syslog('getNoEmail ' . $this->email . ': ' . ($found ? '' : 'NOT ') . 'subscribed!');
69
        if ($found) {
70
            $this->no_email = true;
71
            return true;
72
        }
73
74
        $this->no_email = false;
75
        return false;
76
    }
77
}
78