Issues (3627)

Swiftmailer/Sparkpost/SparkpostFactory.php (1 issue)

1
<?php
2
3
namespace Mautic\EmailBundle\Swiftmailer\Sparkpost;
4
5
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
6
use SparkPost\SparkPost;
7
8
final class SparkpostFactory implements SparkpostFactoryInterface
9
{
10
    /**
11
     * @var GuzzleAdapter
12
     */
13
    private $client;
14
15
    public function __construct(GuzzleAdapter $client)
16
    {
17
        $this->client = $client;
18
    }
19
20
    /**
21
     * @param string   $host
22
     * @param string   $apiKey
23
     * @param int|null $port
24
     *
25
     * @return SparkPost
26
     */
27
    public function create($host, $apiKey, $port = null)
28
    {
29
        if ((false === strpos($host, '://') && '/' != substr($host, 0, 1))) {
30
            $host = 'https://'.$host;
31
        }
32
33
        $options = [
34
            'key' => ($apiKey) ?: 1234, // prevent Exception: You must provide an API key
35
        ];
36
37
        if ($port) {
38
            $options['port'] = $port;
39
        }
40
41
        $hostInfo = parse_url($host);
42
        if ($hostInfo) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $hostInfo of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
43
            $options['protocol'] =  $hostInfo['scheme'];
44
45
            if (empty($port)) {
46
                $options['port'] = 'https' === $hostInfo['scheme'] ? 443 : 80;
47
            }
48
49
            $host = $hostInfo['host'];
50
            if (isset($hostInfo['path'])) {
51
                $path = $hostInfo['path'];
52
                if (preg_match('~/api/(v\d+)$~i', $path, $matches)) {
53
                    // Remove /api from the path and extract the version in case differnt than the Sparkpost SDK default
54
                    $path               = str_replace($matches[0], '', $path);
55
                    $options['version'] = $matches[1];
56
                }
57
58
                // Append whatever is left over to the host (assuming Momentum can be in a subfolder?)
59
                if ('/' !== $path) {
60
                    $host .= $path;
61
                }
62
            }
63
64
            $options['host'] = $host;
65
        }
66
67
        // Must always return a SparkPost host or else Symfony will fail to build the container if host is empty
68
        return new SparkPost($this->client, $options);
69
    }
70
}
71