Completed
Push — master ( 097f33...b796ca )
by Cristian
03:06
created

PilotApiClient   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 83.78%

Importance

Changes 6
Bugs 1 Features 0
Metric Value
wmc 9
c 6
b 1
f 0
lcom 1
cbo 4
dl 0
loc 96
ccs 31
cts 37
cp 0.8378
rs 10

4 Methods

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