Passed
Push — master ( 79ac63...47551c )
by Nicolaas
09:13
created

SendMailTest::run()   C

Complexity

Conditions 13
Paths 12

Size

Total Lines 58
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 0 Features 0
Metric Value
cc 13
eloc 34
c 10
b 0
f 0
nc 12
nop 1
dl 0
loc 58
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\Convert;
8
9
use SilverStripe\Core\Config\Config;
10
11
use SilverStripe\Core\Kernel;
12
13
use SilverStripe\Core\Injector\Injector;
14
use SilverStripe\Dev\BuildTask;
15
16
class SendMailTest extends BuildTask
17
{
18
    protected $title = 'Test if emails are working';
19
20
    private static $segment = 'testemail';
21
22
    public function run($request)
23
    {
24
        /** @var Kernel $kernel */
25
        $kernel = Injector::inst()->get(Kernel::class);
26
        $kernel->setEnvironment('dev');
27
        $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...
28
        $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...
29
        $adminEmail = Config::inst()->get(Email::class, 'admin_email');
30
        if(is_array($adminEmail)) {
31
            $keys = array_keys($adminEmail);
32
            $adminEmail = array_pop($keys);
33
        }
34
        $from = $request->getVar('from') ?: $adminEmail  ;
35
        $to = $request->getVar('to') ?: $adminEmail;        
36
        $subject = $request->getVar('subject') ?: 'testing email';
37
        $message = $request->getVar('message') ?: 'Message goes here';
38
        if (Director::is_cli()) {
39
            echo '
40
41
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

41
from: ' . /** @scrutinizer ignore-type */ Convert::raw2att($from) . '
Loading history...
42
43
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

43
to: ' . /** @scrutinizer ignore-type */ Convert::raw2att($to) . '
Loading history...
44
45
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

45
subject:' . /** @scrutinizer ignore-type */ Convert::raw2att($subject) . '" /><br/><br/>
Loading history...
46
47
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

47
message: ' . /** @scrutinizer ignore-type */ Convert::raw2att($message) . '
Loading history...
48
49
Change values like this: sake dev/tasks/testemail [email protected] [email protected] subject=test message=hello
50
            ';
51
        } else {
52
            echo '
53
                <style>
54
                    input {width: 80vw; max-width: 500px; padding: 5px;}
55
                </style>
56
                <form action="" method="get">
57
                    from: <br/><input name="from" value="' . Convert::raw2att($from) . '" /><br/><br/>
58
                    to: <br/><input name="to" value="' . Convert::raw2att($to) . '" /><br/><br/>
59
                    subject: <br/><input name="subject" value="' . Convert::raw2att($subject) . '" /><br/><br/>
60
                    message: <br/><input name="message" value="' . Convert::raw2att($message) . '" /><br/><br/>
61
                    <input type="submit" />
62
                </form>
63
            ';
64
        }
65
        if($request->getVar('from')) {
66
            if (Director::is_cli()) {
67
                echo '
68
==========================
69
Outcome
70
==========================
71
                ';
72
            } else {
73
                echo '<h1>Outcome</h1>';
74
            }
75
            $outcome = mail($to, $subject . ' raw mail', $message);
76
            echo 'PHP mail sent: ' . ($outcome ? 'YES' : 'NO') . $this->newLine();
77
            $email = new Email($from, $to, $subject . ' silverstripe message', $message);
78
            $outcome = $email->sendPlain();
79
            echo 'Silverstripe e-mail sent: ' . ($outcome ? 'YES' : 'NO') . $this->newLine();
80
        }
81
    }
82
83
    protected function newLine()
84
    {
85
        if (Director::is_cli()) {
86
            return '
87
88
            ';
89
        }
90
        return '<br /><br />';
91
    }
92
}
93