Client   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 6
c 2
b 0
f 1
lcom 1
cbo 1
dl 0
loc 47
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A subscribe() 0 13 2
A request() 0 10 2
1
<?php
2
/*
3
 MIT License
4
 ===========
5
6
 Copyright (c) 2012
7
8
 Permission is hereby granted, free of charge, to any person obtaining a
9
 copy of this software and associated documentation files (the "Software"),
10
 to deal in the Software without restriction, including without limitation
11
 the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
 and/or sell copies of the Software, and to permit persons to whom the
13
 Software is furnished to do so, subject to the following conditions:
14
15
 The above copyright notice and this permission notice shall be included in
16
 all copies or substantial portions of the Software.
17
18
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24
 DEALINGS IN THE SOFTWARE.
25
 */
26
27
namespace MailerliteBundle\Service;
28
29
use GuzzleHttp\Client as GuzzleClient;
30
31
class Client extends GuzzleClient
32
{
33
34
	/**
35
	 * api key
36
	 *
37
	 * @param string
38
	 */
39
	private $apiKey;
40
41
	public function __construct(array $config)
42
	{
43
		if (!array_key_exists('api_key', $config)) {
44
			throw new \InvalidArgumentException('API Key must be provided.');
45
		}
46
47
		$this->apiKey = $config['api_key'];
48
		unset($config['api_key']);
49
50
		parent::__construct($config);
51
	}
52
53
	public function subscribe($email, $name, $listId = null)
54
	{
55
        $listId = !is_null($listId) ? (int)$listId : (int)$this->getConfig('list_id');
56
		return $this->post(
57
			sprintf('subscribers/%d/', $listId),
58
			[
59
				'form_params' => [
60
					'email' => (string)$email,
61
					'name' => (string)$name
62
				]
63
			]
64
		);
65
	}
66
67
	public function request($method, $uri = null, array $options = [])
68
	{
69
		if (isset($options['query'])) {
70
			$options['query'] = array_merge(['apiKey' => $this->apiKey], $options['query']);
71
		} else {
72
			$options['query'] = ['apiKey' => $this->apiKey];
73
		}
74
75
		return parent::request($method, $uri, $options);
76
	}
77
}
78