CheckEmailRedirection   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 29
c 1
b 0
f 0
dl 0
loc 56
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A mergeConfiguredEmails() 0 17 6
A getCalculatedAnswer() 0 26 3
1
<?php
2
3
namespace Sunnysideup\HealthCheckProvider\Checks\Server;
4
5
use SilverStripe\Control\Email\Email;
6
use SilverStripe\Core\Config\Config;
7
use SilverStripe\Core\Environment;
8
use Sunnysideup\HealthCheckProvider\Checks\HealthCheckItemRunner;
9
10
class CheckEmailRedirection extends HealthCheckItemRunner
11
{
12
    public function getCalculatedAnswer()
13
    {
14
        $array['admin_email'] = Config::inst()->get(Email::class, 'admin_email');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$array was never initialized. Although not strictly required by PHP, it is generally a good practice to add $array = array(); before regardless.
Loading history...
15
        $array['send_all_emails_from'] = $this->mergeConfiguredEmails(
16
            'send_all_emails_from',
17
            'SS_SEND_ALL_EMAILS_FROM'
18
        );
19
        $array['send_all_emails_to'] = $this->mergeConfiguredEmails(
20
            'send_all_emails_to',
21
            'SS_SEND_ALL_EMAILS_TO'
22
        );
23
        $array['cc_all_emails_to'] = $this->mergeConfiguredEmails(
24
            'cc_all_emails_to',
25
            'SS_CC_ALL_EMAILS_TO'
26
        );
27
        $array['bcc_all_emails_to'] = $this->mergeConfiguredEmails(
28
            'bcc_all_emails_to',
29
            'SS_BCC_ALL_EMAILS_TO'
30
        );
31
        foreach ($array as $key => $value) {
32
            if (! $value) {
33
                unset($array[$key]);
34
            }
35
        }
36
37
        return $array;
38
    }
39
40
    /**
41
     * Normalise email list from config merged with env vars
42
     * COPIED FROM Email::class
43
     *
44
     * @param string $config Config key
45
     * @param string $env Env variable key
46
     *
47
     * @return string Array of email addresses
48
     */
49
    protected function mergeConfiguredEmails(string $config, string $env): string
50
    {
51
        // Normalise config list
52
        $normalised = [];
53
        $source = (array) Email::config()->get($config);
54
        foreach ($source as $address => $name) {
55
            if ($address && ! is_numeric($address)) {
56
                $normalised[$address] = $name;
57
            } elseif ($name) {
58
                $normalised[$name] = null;
59
            }
60
        }
61
        $extra = Environment::getEnv($env);
62
        if ($extra) {
63
            $normalised[$extra] = null;
64
        }
65
        return implode(', ', $normalised);
66
    }
67
}
68