1 | <?php |
||
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 | $form_params['notification_email'] = $notification_email; |
||
70 | } |
||
71 | |||
72 | 4 | $response = $this->guzzleClient->post(self::BASE_URI, [ |
|
73 | 'body' => $form_params |
||
74 | 4 | ]); |
|
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 | 7 | public function getGuzzleClient() |
|
96 | |||
97 | /** |
||
98 | * @param $app_key |
||
99 | * |
||
100 | * @return $this |
||
101 | */ |
||
102 | 6 | public function setAppKey($app_key) |
|
107 | |||
108 | /** |
||
109 | * @return mixed |
||
110 | */ |
||
111 | 6 | public function getAppKey() |
|
118 | } |
||
119 |