Completed
Branch master (3ee61a)
by Patrick
03:04
created

class.EmailProvider.php (2 issues)

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
 * Allow other classes to be loaded as needed
16
 */
17
require_once('Autoload.php');
18
19
/**
20
 * A singleton class allowing the caller to send Email
21
 *
22
 * This class will abstract out how email is sent
23
 */
24
class EmailProvider extends Provider
25
{
26
    /** An array of methods that can be used to send email */
27
    protected $methods;
28
29
    /**
30
     * Enumerate all supported EmailServices and instacetate them
31
     */
32
    protected function __construct()
33
    {
34
        $settings = \Settings::getInstance();
35
        $this->methods = $settings->getClassesByPropName('email_providers');
36
    }
37
38
    /**
39
     * Send the email
40
     *
41
     * @param Email\Email $email The email message to send
42
     * @param string $methodName The class name of the email method
43
     *
44
     * @return boolean True if the email was sent, false otherwise
45
     */
46
    public function sendEmail($email, $methodName = false)
0 ignored issues
show
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...
47
    {
48
        if($methodName === false)
49
        {
50
            return $this->callOnEach('sendEmail', array($email));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->callOnEach...Email', array($email)); (Auth\Group|Auth\User|false) is incompatible with the return type documented by EmailProvider::sendEmail of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
51
        }
52
        else
53
        {
54
            $method = $this->getMethodByName($methodName);
55
            return $method->sendEmail($email);
56
        }
57
    }
58
}
59
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
60