Completed
Push — master ( 5d11b6...3c08ac )
by Patrick
03:06
created

AmazonSES::canSend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Email;
3
4
require(dirname(__FILE__).'/../vendor/autoload.php');
5
class AmazonSES extends EmailService
6
{
7
    protected $ses;
8
9
    public function __construct($params)
10
    {
11
        $provider = \Aws\Credentials\CredentialProvider::ini('default', $params['ini']);
12
        //$credentials = \Aws\Common\Credentials\Credentials::fromIni('default', $params['ini']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
13
14
        $this->ses = \Aws\Ses\SesClient::factory([
0 ignored issues
show
Deprecated Code introduced by
The method Aws\AwsClient::factory() has been deprecated.

This method has been deprecated.

Loading history...
15
                'version' => 'latest',
16
                'region'  => 'us-west-2',
17
                'credentials' => $provider]);
18
    }
19
20
    public function canSend()
21
    {
22
        $result = $this->ses->getSendQuota();
23
        $result = $result->getAll();
0 ignored issues
show
Bug introduced by
The method getAll() does not seem to exist on object<Aws\Result>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
24
        $res = $result['Max24HourSend'] - $result['SentLast24Hours'];
25
        return $res;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $res; (integer|double) is incompatible with the return type of the parent method Email\EmailService::canSend 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...
26
    }
27
28
    public function sendEmail($email)
29
    {
30
        $tos = $email->getToAddresses();
31
        if(is_array($tos))
32
        {
33
            foreach($tos as $to)
34
            {
35
                if(strstr($to, 'free.fr') !== false)
36
                {
37
                    die('Spammer abuse filter!');
38
                }
39
            }
40
        }
41
42
        if($email->hasAttachments())
43
        {
44
            //Amazeon sendEmail doesn't support attachments. We need to use sendRawEmail
45
            $args = array();
46
            $args['RawMessage'] = array();
47
            $args['RawMessage']['Data'] = base64_encode($email->getRawMessage());
48
            try {
49
                return $this->ses->sendRawEmail($args);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->ses->sendRawEmail($args); (Aws\Result) is incompatible with the return type of the parent method Email\EmailService::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...
50
            } catch(\Exception $e) {
51
                return false;
52
            }
53
        }
54
        else
55
        {
56
            $args = array();
57
            $args['Source'] = $email->getFromAddress();
58
            $args['Destination'] = array();
59
            $args['Destination']['ToAddresses'] = $email->getToAddresses();
60
            $args['Destination']['CcAddresses'] = $email->getCCAddresses();
61
            $args['Destination']['BccAddresses'] = $email->getBCCAddresses();
62
            $args['Message'] = array();
63
            $args['Message']['Subject'] = array();
64
            $args['Message']['Subject']['Data'] = $email->getSubject();
65
            $args['Message']['Body'] = array();
66
            $args['Message']['Body']['Text'] = array();
67
            $args['Message']['Body']['Html'] = array();
68
            $args['Message']['Body']['Text']['Data'] = $email->getTextBody();
69
            $args['Message']['Body']['Html']['Data'] = $email->getHtmlBody();
70
            $args['ReplyToAddresses'] = array($email->getReplyTo());
71
            try {
72
                return $this->ses->sendEmail($args);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->ses->sendEmail($args); (Aws\Result) is incompatible with the return type of the parent method Email\EmailService::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...
73
            } catch(\Exception $e) {
74
                return false;
75
            }
76
        }
77
    }
78
}
79
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
80