Completed
Pull Request — master (#7)
by Patrick
04:03
created

class.EmailProvider.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * EmailProvider class
4
 *
5
 * This file describes the Singleton EmailProvider class
6
 *
7
 * PHP version 5 and 7
8
 *
9
 * @author Patrick Boyd / [email protected]
10
 * @copyright Copyright (c) 2015, Austin Artistic Reconstruction
11
 * @license http://www.apache.org/licenses/ Apache 2.0 License
12
 */
13
14
/**
15
 * use the FlipsideSettings class
16
 */
17
require_once("/var/www/secure_settings/class.FlipsideSettings.php");
18
19
/**
20
 * Allow other classes to be loaded as needed
21
 */
22
require_once('Autoload.php');
23
24
/**
25
 * A singleton class allowing the caller to send Email
26
 *
27
 * This class will abstract out how email is sent
28
 */
29
class EmailProvider extends Singleton
30
{
31
    /** An array of methods that can be used to send email */
32
    protected $methods;
33
34
    /**
35
     * Enumerate all supported EmailServices and instacetate them
36
     */
37 View Code Duplication
    protected function __construct()
38
    {
39
        $this->methods = array();
40
        if(isset(FlipsideSettings::$email_providers))
41
        {
42
            $keys = array_keys(FlipsideSettings::$email_providers);
43
            $count = count($keys);
44
            for($i = 0; $i < $count; $i++)
45
            {
46
                $class = $keys[$i];
47
                array_push($this->methods, new $class(FlipsideSettings::$email_providers[$keys[$i]]));
48
            }
49
        }
50
    }
51
52
    /**
53
     * Get the email provider by name
54
     *
55
     * @param string $methodName The class name of the email method
56
     *
57
     * @return false|\Email\EmailService The Email service specified or false if it is not found
58
     */
59 View Code Duplication
    public function getEmailMethod($methodName)
60
    {
61
        $count = count($this->methods);
62
        for($i = 0; $i < $count; $i++)
63
        {
64
            if(strcasecmp(get_class($this->methods[$i]), $methodName) === 0)
65
            {
66
                return $this->methods[$i];
67
            }
68
        }
69
        return false;
70
    }
71
72
    /**
73
     * Send the email
74
     *
75
     * @param Email\Email $email The email message to send
76
     * @param string $methodName The class name of the email method
77
     *
78
     * @return boolean True if the email was sent, false otherwise
79
     */
80
    public function sendEmail($email, $methodName = false)
81
    {
82
        if($methodName === false)
83
        {
84
            $res = false;
85
            $count = count($this->methods);
86
            for($i = 0; $i < $count; $i++)
87
            {
88
                $res = $this->methods[$i]->sendEmail($email);
89
                if($res !== false)
90
                {
91
                    return $res;
92
                }
93
            }
94
            return $res;
95
        }
96
        else
97
        {
98
            $method = $this->getEmailMethod($methodName);
99
            return $method->sendEmail($email);
100
        }
101
    }
102
}
103
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
104
?>
0 ignored issues
show
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
105