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

MailingUnsubscribe::removeUnsubscriptionEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
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\Classes;
20
21
use Dolibarr\Core\Base\Model;
22
23
class MailingUnsubscribe extends Model
24
{
25
    /**
26
     * Indicates whether the automatic record creation and update fields are to be used.
27
     *
28
     * @var bool
29
     */
30
    public $timestamps = true;
31
32
    /**
33
     * Name of the table associated with the model. By default, it is the plural model name in
34
     * snakecase format: 'mailing_unsubscribes'.
35
     *
36
     * @var string
37
     */
38
    protected $table = 'mailing_unsubscribe';
39
40
    /**
41
     * List of fields that will be autocompleted with 'null' during the registration of a new record.
42
     *
43
     * @var string[]
44
     */
45
    protected $fillable = ['unsubscribegroup', 'ip', 'entity', 'email'];
46
47
48
    /**
49
     * Register an email to unsubscribe from lists.
50
     * TODO: It would be necessary to see if one or more entities really have to be passed.
51
     *
52
     * @param $email
53
     * @param $entities
54
     *
55
     * @return bool
56
     */
57
    public static function unsubscribeEmail($email, $entities): bool
58
    {
59
        foreach ($entities as $entity) {
60
            static::firstOrCreate([
61
                'email' => $email,
62
                'entity' => $entity,
63
            ]);
64
        }
65
        return static::unsubscribedEmail($email, $entities);
66
    }
67
68
    /**
69
     * Check if the email is subscribed in some of the indicated entities.
70
     * TODO: It would be necessary to see if one or more entities really have to be passed.
71
     *
72
     * @param $email
73
     * @param $entities
74
     *
75
     * @return bool
76
     */
77
    public static function unsubscribedEmail($email, $entities): bool
78
    {
79
        $data = static::where('email', $email)->whereIn('entity', $entities)->get();
80
        return count($data) > 0;
81
    }
82
83
    /**
84
     * Remove the email from the unsubscribed list.
85
     * TODO: It would be necessary to see if one or more entities really have to be passed.
86
     *
87
     * @param $email
88
     * @param $entities
89
     *
90
     * @return bool
91
     */
92
    public static function removeUnsubscriptionEmail($email, $entities): bool
93
    {
94
        return static::where('email', $email)->whereIn('entity', $entities)->delete();
95
    }
96
}
97