Completed
Push — master ( 8acc04...458fbe )
by Laurent
02:26
created

InterfaceMailOnIncident::runTrigger()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 7
nop 5
dl 0
loc 58
rs 7.3608
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
2
/* Copyright (C) 2017 laurent De Coninck <[email protected]>
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
19
require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
20
dol_include_once('/flightlog/class/bbcvols.class.php');
21
dol_include_once('/core/class/CMailFile.class.php');
22
23
24
/**
25
 *  Class of triggers for hello module
26
 */
27
class InterfaceMailOnIncident extends DolibarrTriggers
28
{
29
	/**
30
	 * @var DoliDB Database handler
31
	 */
32
	protected $db;
33
34
	/**
35
	 * Constructor
36
	 *
37
	 * @param DoliDB $db Database handler
38
	 */
39
	public function __construct($db)
40
	{
41
		$this->db = $db;
42
43
		$this->name = preg_replace('/^Interface/i', '', get_class($this));
44
		$this->family = "Belgian balloon club";
45
		$this->description = "Trigger that send an e-mail on flight incident.";
46
		$this->version = '1.0';
47
		$this->picto = 'flightlog@flightlog';
48
	}
49
50
	/**
51
	 * Trigger name
52
	 *
53
	 * @return string Name of trigger file
54
	 */
55
	public function getName()
56
	{
57
		return $this->name;
58
	}
59
60
	/**
61
	 * Trigger description
62
	 *
63
	 * @return string Description of trigger file
64
	 */
65
	public function getDesc()
66
	{
67
		return $this->description;
68
	}
69
70
71
	/**
72
	 * @param string 		$action 	Event action code
73
	 * @param Bbcvols 	    $object 	Object
74
	 * @param User 			$user 		Object user
75
	 * @param Translate 	$langs 		Object langs
76
	 * @param Conf 			$conf 		Object conf
77
	 * @return int              		<0 if KO, 0 if no triggered ran, >0 if OK
78
	 */
79
	public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
80
	{
81
        if (empty($conf->flightlog->enabled) || empty($conf->workflow->enabled)){
82
            return 0;
83
        }
84
85
        if(empty($conf->global->WORKFLOW_BBC_FLIGHTLOG_SEND_MAIL_ON_INCIDENT)){
86
            return 0;
87
        }
88
89
        if($action !== 'BBC_FLIGHT_LOG_ADD_FLIGHT'){
90
            return 0;
91
        }
92
93
        if(empty(trim($object->incidents)) && empty(trim($object->remarque))){
94
            return 0;
95
        }
96
97
        if(!empty($conf->global->MAIN_DISABLE_ALL_MAILS)){
98
            return 0;
99
        }
100
101
        $message = "<p>Bonjour,</p><br/>";
102
        $message .= sprintf("<p>Vous recevez cet e-mail car un vol a été encodé avec un incident/ une remarque sur le ballon : %s.</p>", $object->getBalloon()->immat);
103
        $message .= sprintf("<p>Vol id : %d</p>", $object->getId());
104
        $message .= sprintf("<p>Réalisé par : %s </p>", $object->getPilot()->getFullName($langs));
105
        $message .= "<br/>";
106
        $message .= sprintf("<p>Commentaire : %s </p>", $object->getComment());
107
        $message .= sprintf("<p>Incident : %s </p>", $object->getIncident());
108
        $message .= "<p>Ce mail est un mail informatif automatique, il ne faut pas y répondre.</p>";
109
        $message .= "<p>Le Belgian balloon club.</p>";
110
        
111
        $responsable = new User($user->db);
112
        $responsable->fetch($object->getBalloon()->fk_responsable);
113
114
        $mailfile = new CMailFile(
115
            sprintf("Un vol a été encodé avec un incident / un commentaire sur le ballon %s", $object->getBalloon()->immat),
116
            $responsable->email,
117
            $conf->global->MAIN_MAIL_EMAIL_FROM,
118
            $message,
119
            array(),
120
            array(),
121
            array(),
122
            '',
123
            '',
124
            0,
125
            -1,
126
            $conf->global->MAIN_MAIL_ERRORS_TO
127
        );
128
129
130
        if (! $mailfile->sendfile())
131
        {
132
            dol_syslog("Error while sending mail in flight log module : incident", LOG_ERR);
133
        }
134
135
		return 0;
136
	}
137
}
138