Completed
Push — master ( 06220a...1c737a )
by Mauro
02:34
created

PilotApiClient   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 97.3%

Importance

Changes 8
Bugs 1 Features 0
Metric Value
wmc 10
c 8
b 1
f 0
lcom 1
cbo 5
dl 0
loc 99
ccs 36
cts 37
cp 0.973
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 2
B storeLead() 0 26 4
A getGuzzleClient() 0 4 1
A setAppKey() 0 5 1
A getAppKey() 0 7 2
1
<?php
2
/**
3
 * Class PilotApiClient
4
 *
5
 * @author Cristian Jimenez <[email protected]>
6
 * @author Mauro Moreno     <[email protected]>
7
 */
8
9
namespace Zephia\PilotApiClient\Client;
10
11
use GuzzleHttp\Client as GuzzleClient;
12
use Zephia\PilotApiClient\Exception\InvalidArgumentException;
13
use Zephia\PilotApiClient\Exception\LogicException;
14
use Zephia\PilotApiClient\Model\LeadData;
15
16
class PilotApiClient
17
{
18
    /**
19
     * Pilot API URI
20
     */
21
    const BASE_URI = 'http://www.pilotsolution.com.ar/api/webhooks/welcome.php';
22
23
    private $appKey;
24
    private $debug;
25
    private $guzzleClient;
26
27
    /**
28
     * PilotApi constructor.
29
     * @param array $config
30
     */
31 9
    public function __construct(array $config = [])
32
    {
33
        $defaults = [
34 9
            'debug' => false,
35 9
            'app_key' => null,
36 9
        ];
37
38 9
        $config = array_merge($defaults, $config);
39
40 9
        if (!empty($config['app_key'])) {
41 6
            $this->setAppKey($config['app_key']);
42 6
        }
43 9
        $this->debug = $config['debug'];
44
45 9
        $this->guzzleClient = new GuzzleClient();
46 9
    }
47
48
    /**
49
     * Stores a lead
50
     *
51
     * @param LeadData $lead_data
52
     * @param string $notification_email
53
     *
54
     * @throws InvalidArgumentException
55
     *
56
     * @return mixed
57
     */
58 4
    public function storeLead(LeadData $lead_data, $notification_email = '')
59
    {
60
        $form_params = [
61 4
            'debug' => $this->debug,
62 4
            'action' => 'create',
63 4
            'appkey' => $this->getAppKey(),
64 4
        ];
65
66 4
        $form_params = array_merge($form_params, $lead_data->toArray());
67
68 4
        if (!empty($notification_email)) {
69 1
            $form_params['notification_email'] = $notification_email;
70 1
        }
71
72 4
        $response = $this->guzzleClient->post(self::BASE_URI, [
73
            'body' => $form_params
74 4
        ]);
75
76 3
        if ($response->getStatusCode() === 200) {
77 3
            $content = json_decode($response->getBody()->getContents());
78 4
            if ($content->success === false) {
79 1
                throw new InvalidArgumentException($content->data);
80
            }
81 2
            return $content;
82
        }
83
    }
84
85
    /**
86
     * @return GuzzleClient
87
     */
88 7
    public function getGuzzleClient()
89
    {
90 7
        return $this->guzzleClient;
91
    }
92
93
    /**
94
     * @param $app_key
95
     *
96
     * @return $this
97
     */
98 6
    public function setAppKey($app_key)
99
    {
100 6
        $this->appKey = $app_key;
101 6
        return $this;
102
    }
103
104
    /**
105
     * @return mixed
106
     */
107 6
    public function getAppKey()
108
    {
109 6
        if (empty($this->appKey)) {
110 1
            throw new LogicException('App Key is undefined.');
111
        }
112 5
        return $this->appKey;
113
    }
114
}
115