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

EmailProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 35.14 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 26
loc 74
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 14 14 3
A getEmailMethod() 12 12 3
B sendEmail() 0 22 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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