Passed
Push — master ( a63774...27c524 )
by Nicolaas
02:41
created

SendMailTest::run()   C

Complexity

Conditions 13
Paths 12

Size

Total Lines 88
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 13
Bugs 0 Features 0
Metric Value
cc 13
eloc 34
c 13
b 0
f 0
nc 12
nop 1
dl 0
loc 88
rs 6.6166

How to fix   Long Method    Complexity   

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
3
namespace Sunnysideup\EmailTest\Tasks;
4
5
use SilverStripe\Control\Director;
6
use SilverStripe\Control\Email\Email;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Core\Convert;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\Core\Kernel;
11
use SilverStripe\Dev\BuildTask;
12
13
/**
14
 * @internal
15
 * @coversNothing
16
 */
17
class SendMailTest extends BuildTask
18
{
19
    protected $title = 'Test if emails are working';
20
21
    private static $segment = 'testemail';
22
23
    public function run($request)
24
    {
25
// <<<<<<< HEAD
26
//         $from = $_GET['from'] ?? 'webmaster@' . Director::host();
27
//         $to = $_GET['to'] ?? 'support@' . Director::host();
28
//         $subject = $_GET['subject'] ?? 'testing email';
29
//         $message = $_GET['message'] ?? 'Message goes here';
30
//
31
//         echo '
32
//             <style>
33
//                 input {width: 80vw; max-width: 500px; padding: 5px;}
34
//             </style>
35
//             <form action="" method="get">
36
//                 from: <br/><input name="from" value="' . Convert::raw2att($from) . '" /><br/><br/>
37
//                 to: <br/><input name="to" value="' . Convert::raw2att($to) . '" /><br/><br/>
38
//                 subject: <br/><input name="subject" value="' . Convert::raw2att($subject) . '" /><br/><br/>
39
//                 message: <br/><input name="message" value="' . Convert::raw2att($message) . '" /><br/><br/>
40
//                 <input type="submit" />
41
//             </form>
42
//             <h1>Outcome</h1>
43
//
44
//         ';
45
//         $outcome = mail($to, $subject, $message);
46
//         echo 'PHP mail sent: ' . ($outcome ? 'YES' : 'NO') . $this->newLine();
47
//         $email = new Email($from, $to, $subject, $message);
48
//         $outcome = $email->sendPlain();
49
//         echo 'Silverstripe e-mail sent: ' . ($outcome ? 'YES' : 'NO') . $this->newLine();
50
// =======
51
        /** @var Kernel $kernel */
52
        $kernel = Injector::inst()->get(Kernel::class);
53
        $kernel->setEnvironment('dev');
54
55
        $from = $request->getVar('from') ?: Config::inst()->get(Email::class, 'admin_email');
0 ignored issues
show
Unused Code introduced by
The assignment to $from is dead and can be removed.
Loading history...
56
        $to = $request->getVar('to') ?: Config::inst()->get(Email::class, 'admin_email');
0 ignored issues
show
Unused Code introduced by
The assignment to $to is dead and can be removed.
Loading history...
57
        $adminEmail = Config::inst()->get(Email::class, 'admin_email');
58
        if (is_array($adminEmail)) {
59
            $keys = array_keys($adminEmail);
60
            $adminEmail = array_pop($keys);
61
        }
62
63
        $from = $request->getVar('from') ?: $adminEmail;
64
        $to = $request->getVar('to') ?: $adminEmail;
65
        $subject = $request->getVar('subject') ?: 'testing email';
66
        $message = $request->getVar('message') ?: 'Message goes here';
67
        if (Director::is_cli()) {
68
            echo '
69
70
from: ' . Convert::raw2att($from) . '
0 ignored issues
show
Bug introduced by
Are you sure SilverStripe\Core\Convert::raw2att($from) of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
from: ' . /** @scrutinizer ignore-type */ Convert::raw2att($from) . '
Loading history...
71
72
to: ' . Convert::raw2att($to) . '
0 ignored issues
show
Bug introduced by
Are you sure SilverStripe\Core\Convert::raw2att($to) of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
to: ' . /** @scrutinizer ignore-type */ Convert::raw2att($to) . '
Loading history...
73
74
subject:' . Convert::raw2att($subject) . '" /><br/><br/>
0 ignored issues
show
Bug introduced by
Are you sure SilverStripe\Core\Convert::raw2att($subject) of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
subject:' . /** @scrutinizer ignore-type */ Convert::raw2att($subject) . '" /><br/><br/>
Loading history...
75
76
message: ' . Convert::raw2att($message) . '
0 ignored issues
show
Bug introduced by
Are you sure SilverStripe\Core\Convert::raw2att($message) of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
message: ' . /** @scrutinizer ignore-type */ Convert::raw2att($message) . '
Loading history...
77
78
Change values like this: sake dev/tasks/testemail [email protected] [email protected] subject=test message=hello
79
            ';
80
        } else {
81
            echo '
82
                <style>
83
                    input {width: 80vw; max-width: 500px; padding: 5px;}
84
                </style>
85
                <form action="" method="get">
86
                    from: <br/><input name="from" value="' . Convert::raw2att($from) . '" /><br/><br/>
87
                    to: <br/><input name="to" value="' . Convert::raw2att($to) . '" /><br/><br/>
88
                    subject: <br/><input name="subject" value="' . Convert::raw2att($subject) . '" /><br/><br/>
89
                    message: <br/><input name="message" value="' . Convert::raw2att($message) . '" /><br/><br/>
90
                    <input type="submit" />
91
                </form>
92
            ';
93
        }
94
95
        if ($request->getVar('from')) {
96
            if (Director::is_cli()) {
97
                echo '
98
==========================
99
Outcome
100
==========================
101
                ';
102
            } else {
103
                echo '<h1>Outcome</h1>';
104
            }
105
106
            $outcome = mail($to, $subject . ' raw mail', $message);
107
            echo 'PHP mail sent: ' . ($outcome ? 'YES' : 'NO') . $this->newLine();
108
            $email = new Email($from, $to, $subject . ' silverstripe message', $message);
109
            $outcome = $email->sendPlain();
110
            echo 'Silverstripe e-mail sent: ' . ($outcome ? 'YES' : 'NO') . $this->newLine();
111
        }
112
    }
113
114
    protected function newLine()
115
    {
116
        if (Director::is_cli()) {
117
            return '
118
119
            ';
120
        }
121
122
        return '<br /><br />';
123
    }
124
}
125