Completed
Push — master ( 337a96...25b1b5 )
by Shingo
11s
created

SendGrid::sgEncode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Sichikawa\LaravelSendgridDriver;
4
5
use Illuminate\Mail\Mailable;
6
use Sichikawa\LaravelSendgridDriver\Transport\SendgridTransport;
7
use Swift_Message;
8
9
trait SendGrid
10
{
11
12
    /**
13
     * @param null|array $params
14
     * @return $this
15
     */
16
    public function sendgrid($params)
17
    {
18
        if ($this instanceof Mailable && $this->mailDriver() == "sendgrid") {
19
            $this->withSwiftMessage(function (Swift_Message $message) use ($params) {
20
                $message->embed(new \Swift_Image(static::sgEncode($params), SendgridTransport::SMTP_API_NAME));
0 ignored issues
show
Bug introduced by
It seems like $params defined by parameter $params on line 16 can also be of type null; however, Sichikawa\LaravelSendgri...er\SendGrid::sgEncode() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
21
            });
22
        }
23
        return $this;
24
    }
25
26
    /**
27
     * @return string
28
     */
29
    private function mailDriver()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
30
    {
31
        return function_exists('config') ? config('mail.driver') : env('MAIL_DRIVER');
32
    }
33
34
    /**
35
     * @param array $params
36
     * @return string
37
     */
38
    public static function sgEncode($params)
39
    {
40
        if (is_string($params)) {
41
            return $params;
42
        }
43
        return json_encode($params);
44
    }
45
46
    /**
47
     * @param string $strParams
48
     * @return array
49
     */
50
    public static function sgDecode($strParams)
51
    {
52
        if (!is_string($strParams)) {
53
            return (array) $strParams;
54
        }
55
        $params = json_decode($strParams, true);
56
        return is_array($params) ? $params : [];
57
    }
58
59
}
60