Completed
Push — master ( b029a9...ae7e79 )
by Patrick
03:01
created

EmailProvider::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 0
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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