MailChimp::doRequest()   C
last analyzed

Complexity

Conditions 7
Paths 12

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 21
nc 12
nop 3
1
<?php
2
/*
3
 * This file is part of the MailChimpEcommerceBundle package.
4
 *
5
 * Copyright (c) 2017 kevin92dev.es
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * Feel free to edit as you please, and have fun.
11
 *
12
 * @author Kevin Murillo <[email protected]>
13
 */
14
15
namespace Kevin92dev\MailChimpEcommerceBundle\Services;
16
17
use Kevin92dev\MailChimpEcommerceBundle\Exceptions\CustomerNotFoundException;
18
use Kevin92dev\MailChimpEcommerceBundle\Exceptions\OrderNotFoundException;
19
use Kevin92dev\MailChimpEcommerceBundle\Exceptions\ProductNotFoundException;
20
use Kevin92dev\MailChimpEcommerceBundle\RequestTypes;
21
use GuzzleHttp\Client;
22
use GuzzleHttp\Exception\RequestException;
23
24
/**
25
 * Mailchimp
26
 *
27
 * @author Kevin Murillo <[email protected]>
28
 */
29
class MailChimp
30
{
31
    /**
32
     * @var string
33
     */
34
    private $apiKey;
35
36
    /**
37
     * @var string
38
     */
39
    private $storeId;
40
41
    /**
42
     * @var Client
43
     */
44
    private $httpClient;
45
46
    /**
47
     * @var string
48
     */
49
    private $endPoint;
50
51
    /**
52
     * Initializes MailChimp
53
     *
54
     * @param string $apiKey Mailchimp api key
55
     * @param string $storeId
56
     * @param Client $httpClient The HTTP Guzzle Client
57
     * @param bool   $ssl Enable secure connectionss
58
     */
59
    public function __construct($apiKey, $storeId, Client $httpClient, $ssl = true)
60
    {
61
        $this->apiKey = $apiKey;
62
        $this->storeId = $storeId;
63
        $this->httpClient = $httpClient;
64
65
        $key = preg_split("/-/", $this->apiKey);
66
67
        if ($ssl) {
68
            $this->endPoint = 'https://'.$key[1].'.api.mailchimp.com/3.0/ecommerce/stores/'.$storeId;
69
        } else {
70
            $this->endPoint = 'http://'.$key[1].'.api.mailchimp.com/3.0/ecommerce/stores'.$storeId;
71
        }
72
    }
73
74
    /**
75
     * MailChimp Requests
76
     *
77
     * @param string $method
78
     * @param array $body
79
     * @param string $resource
80
     *
81
     * @return \Psr\Http\Message\ResponseInterface|null
82
     * @throws CustomerNotFoundException
83
     * @throws ProductNotFoundException
84
     * @throws \Exception
85
     */
86
    public function doRequest($method, $body, $resource = '')
87
    {
88
        $url = $this->endPoint.$resource;
89
90
        // Set auth
91
        $data['auth'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
92
            'user', $this->apiKey
93
        ];
94
95
        // .. and body
96
        if ($method != RequestTypes::$GET) {
97
            $data['json'] = $body;
98
        }
99
100
        try {
101
            return $this->httpClient->request($method, $url, $data);
102
        } catch (RequestException $e) {
103
            $statusCode = $e->getResponse()->getStatusCode();
104
105
            switch ($statusCode) {
106
                case 404:
107
                    if (strpos($resource, 'products') !== false) {
108
                        throw new ProductNotFoundException();
109
                    } elseif (strpos($resource, 'customers') !== false) {
110
                        throw new CustomerNotFoundException();
111
                    } elseif (strpos($resource, 'orders') !== false) {
112
                        throw new OrderNotFoundException();
113
                    }
114
                    break;
115
                default:
116
                    throw new \Exception($e->getResponse()->getBody());
117
            }
118
        }
119
    }
120
}
121