Completed
Push — master ( 547f17...15a09e )
by Shingo
02:19
created

SendgridTransport::setTo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
namespace Sichikawa\LaravelSendgridDriver\Transport;
3
4
use GuzzleHttp\ClientInterface;
5
use Illuminate\Mail\Transport\Transport;
6
use Swift_Attachment;
7
use Swift_Mime_Message;
8
9
class SendgridTransport extends Transport
10
{
11
    const MAXIMUM_FILE_SIZE = 7340032;
12
13
    private $client;
14
    private $options;
15
16
    public function __construct(ClientInterface $client, $api_key)
17
    {
18
        $this->client = $client;
19
        $this->options = [
20
            'headers' => ['Authorization' => 'Bearer ' . $api_key]
21
        ];
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function send(Swift_Mime_Message $message, &$failedRecipients = null)
28
    {
29
        list($from, $fromName) = $this->getFromAddresses($message);
30
        $payload = $this->options;
31
32
        $data = [
33
            'from'     => $from,
34
            'fromname' => isset($fromName) ? $fromName : null,
35
            'subject'  => $message->getSubject(),
36
            'html'     => $message->getBody()
37
        ];
38
        $this->setTo($data, $message);
39
        $this->setCc($data, $message);
40
        $this->setBcc($data, $message);
41
        $this->setAttachment($data, $message);
42
43
        if (version_compare(ClientInterface::VERSION, '6') === 1) {
44
            $payload += ['form_params' => $data];
45
        } else {
46
            $payload += ['body' => $data];
47
        }
48
49
        return $this->client->post('https://api.sendgrid.com/api/mail.send.json', $payload);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->client->po....send.json', $payload); (GuzzleHttp\Message\ResponseInterface) is incompatible with the return type declared by the interface Swift_Transport::send of type integer.

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
    }
51
52
    /**
53
     * @param  $data
54
     * @param  Swift_Mime_Message $message
55
     */
56
    protected function setTo(&$data, Swift_Mime_Message $message)
57
    {
58
        if ($from = $message->getTo()) {
59
            $data['to'] = array_keys($from);
60
            $data['toname'] = array_values($from);
61
        }
62
    }
63
64
    /**
65
     * @param $data
66
     * @param Swift_Mime_Message $message
67
     */
68
    protected function setCc(&$data, Swift_Mime_Message $message)
69
    {
70
        if ($cc = $message->getCc()) {
71
            $data['cc'] = array_keys($cc);
72
            $data['ccname'] = array_values($cc);
73
        }
74
    }
75
76
    /**
77
     * @param $data
78
     * @param Swift_Mime_Message $message
79
     */
80
    protected function setBcc(&$data, Swift_Mime_Message $message)
81
    {
82
        if ($bcc = $message->getBcc()) {
83
            $data['bcc'] = array_keys($bcc);
84
            $data['bccname'] = array_values($bcc);
85
        }
86
    }
87
88
    /**
89
     * Get From Addresses.
90
     *
91
     * @param Swift_Mime_Message $message
92
     * @return array
93
     */
94
    protected function getFromAddresses(Swift_Mime_Message $message)
95
    {
96
        if ($message->getFrom()) {
97
            foreach ($message->getFrom() as $address => $name) {
98
                return [$address, $name];
99
            }
100
        }
101
        return [];
102
    }
103
104
    /**
105
     * Set Attachment Files.
106
     *
107
     * @param $data
108
     * @param Swift_Mime_Message $message
109
     */
110
    protected function setAttachment(&$data, Swift_Mime_Message $message)
111
    {
112
        foreach ($message->getChildren() as $attachment) {
113
            if (!$attachment instanceof Swift_Attachment || !strlen($attachment->getBody()) > self::MAXIMUM_FILE_SIZE) {
114
                continue;
115
            }
116
            $handler = tmpfile();
117
            fwrite($handler, $attachment->getBody());
118
            $data['files[' . $attachment->getFilename() . ']'] = $handler;
119
        }
120
    }
121
}
122