Mail::createMail()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace FMUP\ErrorHandler\Plugin;
3
4
use FMUP\Sapi;
5
6
class Mail extends Abstraction
7
{
8 1
    public function canHandle()
9
    {
10 1
        $config = $this->getBootstrap()->getConfig();
11
        return (
12 1
            (!$this->getException() instanceof \FMUP\Exception\Status)
13 1
            && !$config->get('use_daily_alert') && !$this->iniGet('display_errors')
14
        );
15
    }
16
17
    /**
18
     * @param string $key
19
     * @return string
20
     * @codeCoverageIgnore
21
     */
22
    protected function iniGet($key)
23
    {
24
        return ini_get($key);
25
    }
26
27
    /**
28
     * @return $this
29
     */
30 1
    public function handle()
31
    {
32 1
        $this->sendMail($this->getBody());
33 1
        return $this;
34
    }
35
36
    /**
37
     * @param string $body
38
     * @return bool
39
     * @throws \Exception
40
     * @throws \FMUP\Config\Exception
41
     * @throws \FMUP\Exception
42
     * @throws \phpmailerException
43
     */
44 1
    protected function sendMail($body)
45
    {
46 1
        $config = $this->getBootstrap()->getConfig();
47
        /** @var \FMUP\Request\Http $request */
48 1
        $request = $this->getRequest();
49 1
        $serverName = $this->getBootstrap()->getSapi()->get() != Sapi::CLI
50 1
            ? $request->getServer(\FMUP\Request\Http::SERVER_NAME)
51 1
            : $config->get('erreur_mail_sujet');
52 1
        $mail = $this->createMail($config);
53 1
        $mail->From = $config->get('mail_robot');
0 ignored issues
show
Documentation Bug introduced by
It seems like $config->get('mail_robot') can also be of type array. However, the property $From is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
54 1
        $mail->FromName = $config->get('mail_robot_name');
0 ignored issues
show
Documentation Bug introduced by
It seems like $config->get('mail_robot_name') can also be of type array. However, the property $FromName is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
55 1
        $mail->Subject = '[Erreur] ' . $serverName;
56 1
        $mail->AltBody = (string)$body;
57 1
        $mail->WordWrap = 50; // set word wrap
58
59 1
        $mail->Body = (string)$body;
60
61 1
        $recipients = $config->get('mail_support');
62 1
        if (strpos($recipients, ',') === false) {
63 1
            $mail->AddAddress($recipients, "Support");
0 ignored issues
show
Bug introduced by
It seems like $recipients defined by $config->get('mail_support') on line 61 can also be of type array or null; however, PHPMailer::addAddress() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
64
        } else {
65 1
            $tab_recipients = explode(',', $recipients);
66 1
            foreach ($tab_recipients as $recipient) {
67 1
                $mail->AddAddress($recipient);
68
            }
69
        }
70 1
        return $mail->Send();
71
    }
72
73
    /**
74
     * Creates a new mail for config
75
     * @param \FMUP\Config\ConfigInterface $config
76
     * @return \FMUP\Mail
77
     * @codeCoverageIgnore
78
     */
79
    protected function createMail(\FMUP\Config\ConfigInterface $config)
80
    {
81
        return new \FMUP\Mail($config);
82
    }
83
84
    /**
85
     * @todo clean this
86
     * @return string
87
     */
88 1
    protected function getBody()
89
    {
90 1
        $view = new \FMUP\View(array('exception' => $this->getException()));
91 1
        return $view->setViewPath($this->getViewPath())->render();
92
    }
93
94 1
    protected function getViewPath()
95
    {
96 1
        return __DIR__ . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, array('Mail', 'render.phtml'));
97
    }
98
}
99