generated from amattu2/project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathServiceHistory.class.php
208 lines (186 loc) · 6.23 KB
/
ServiceHistory.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/*
* Produced: Sat Dec 11 2021
* Author: Alec M.
* GitHub: https://amattu.com/links/github
* Copyright: (C) 2021 Alec M.
* License: License GNU Affero General Public License v3.0
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Class Namespace
namespace amattu\CARFAX;
/**
* This is a CARFAX Service History API wrapper class
*/
class ServiceHistory {
/**
* Service History API endpoint
*
* @var string
*/
private static $endpoint = "https://servicesocket.carfax.com/data/1";
/**
* CARFAX provided Product Key
*
* @var string
*/
private static $productDataId = "";
/**
* CARFAX provided Location ID
*
* @var string
*/
private static $locationId = "";
/**
* A Static function to Update the Location ID
*
* @param string $locationId
* @return void
* @author Alec M.
*/
public static function setLocationId(string $locationId) : void
{
self::$locationId = $locationId;
}
/**
* A Static function to Update the Product Data ID
*
* @param string $productDataId
* @return void
* @author Alec M.
*/
public static function setProductDataId(string $productDataId) : void
{
self::$productDataId = $productDataId;
}
/**
* A Static function to use cURL to make a request to the Service History API
*
* @param string $VIN
* @return array [
* "Decode" => Array,
* "Overview" => Array,
* "History" => Array,
* ]
* @throws InvalidArgumentException
* @throws UnexpectedValueException
* @author Alec M.
*/
public static function get(string $VIN) : array
{
// Validate the VIN
if (!preg_match("/^[A-Z0-9]{17}$/", $VIN) || strlen($VIN) != 17) {
throw new \InvalidArgumentException("Invalid VIN provided");
}
// Validate the Product Data ID
if (self::$productDataId == "" || strlen(self::$productDataId) != 16) {
throw new \UnexpectedValueException("Product Data ID not valid");
}
// Validate the Location ID
if (self::$locationId == "" || strlen(self::$locationId) <= 1 || strlen(self::$locationId) > 50) {
throw new \UnexpectedValueException("Location ID not valid");
}
// Submit the request
$result = self::post([
"productDataId" => self::$productDataId,
"locationId" => self::$locationId,
"vin" => $VIN,
]);
$formatted_result = [
"Decode" => [],
"Overview" => [],
"Records" => [],
];
// Validate the result
if (!$result || empty($result)) {
return $formatted_result;
}
// Parse VIN Decode
if (!empty($result["serviceHistory"])) {
$formatted_result["Decode"]["VIN"] = $result["serviceHistory"]["vin"];
$formatted_result["Decode"]["Year"] = $result["serviceHistory"]["year"];
$formatted_result["Decode"]["Make"] = $result["serviceHistory"]["make"];
$formatted_result["Decode"]["Model"] = $result["serviceHistory"]["model"];
$formatted_result["Decode"]["Trim"] = $result["serviceHistory"]["bodyTypeDescription"] ?: "";
$formatted_result["Decode"]["Driveline"] = $result["serviceHistory"]["driveline"] ?: "";
}
// Parse serviceCategories
if (!empty($result["serviceHistory"]) && !empty($result["serviceHistory"]["serviceCategories"])) {
foreach ($result["serviceHistory"]["serviceCategories"] as $category) {
$formatted_result["Overview"][] = [
"Name" => $category["serviceName"],
"Date" => isset($category["dateOfLastService"]) ? $category["dateOfLastService"] : null,
"Odometer" => isset($category["odometerOfLastService"]) ? intval(str_replace(",", "", $category["odometerOfLastService"])) : 0,
];
}
}
// Parse displayRecords
if (!empty($result["serviceHistory"]) && !empty($result["serviceHistory"]["displayRecords"])) {
foreach ($result["serviceHistory"]["displayRecords"] as $record) {
$formatted_result["Records"][] = [
"Date" => $record["displayDate"] !== "Not Reported" ? $record["displayDate"] : null,
"Odometer" => isset($record["odometer"]) ? intval(str_replace(",", "", $record["odometer"])) : 0,
"Services" => is_array($record["text"]) ? $record["text"] : [],
"Type" => $record["type"] === "service" ? "Service" : "Recall",
];
}
}
// Return the formatted result
return $formatted_result;
}
/**
* A private function to submit a POST request to the Service History API
*
* @param array $fields
* @return ?array $response
* @throws None
* @author Alec M.
*/
private static function post(array $fields) : ?array
{
// Create a cURL handle
$ch = curl_init();
// Set the options
curl_setopt($ch, CURLOPT_URL, self::$endpoint);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/json",
"Accept: application/json",
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// Execute the request
$data = null;
$resp = curl_exec($ch);
$errn = curl_error($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// Validate the response
if (!$resp || $errn || $status_code !== 200 || !($data = json_decode($resp, true))) {
return null;
}
// Check for errorMessages
if ($data["errorMessages"] && !empty($data["errorMessages"])) {
return null;
}
// Check for serviceHistory
if (!$data["serviceHistory"] || !is_array($data["serviceHistory"])) {
return null;
}
// Return the parsed response
return $data;
}
}