initial commit
[namibia] / module / Utility / src / Utility / Comms / TransUnion.php
1 <?php
2 namespace Utility\Comms;
3
4
5
6 use Utility;
7 /**
8  * Facilitates chatting to TransUnion JSON api.
9  * @author andre.fourie
10  */
11 class TransUnion
12 {
13
14         /**
15          * Production URL
16          * @var string
17          */
18         static private $liveUrl = 'https://tuaba.transunion.co.za/100/TransUnionAuto.JSon.svc';
19
20         /**
21          * Development/staging URL
22          * @var string
23          */
24         static private $devUrl  = 'https://devtuaba.transunion.co.za/100/TransUnionAuto.JSon.svc';
25
26         /**
27          * Development/staging credentials.
28          * @var array
29          */
30         static private $devCredentials = array(
31                                                 'Username' => 'TuabaDev',
32                                                 'Password' => '2!2E2A736@'
33                                                 );
34
35
36
37         /**
38          * Collect vehicle particulars from VIN number.
39          * @param  string $vin
40          * @param  integer $regYear
41          * @param  integer $mileage
42          * @param  integer $condition
43          * @return array
44          */
45         static public function searchByVin($vin, $regYear, $mileage = null, $condition = null)
46         {
47                 $params = array(
48                                 'Credential' => array(),
49                                 'VIN'        => $vin,
50                                 'RegYear'    => $regYear
51                                 );
52                 if (!is_null($mileage) && !is_null($condition)
53                                 && is_numeric($mileage) && is_numeric($condition)
54                                 && 0 < $condition && 6 > $condition)
55                 {
56                         $params['Mileage']   = $mileage;
57                         $params['Condition'] = $condition;
58                 }
59                 return self::_speaketh('SearchByVIN', $params);
60         }
61
62         /**
63          * Collect vehicle particulars from Registration number.
64          * @param  string  $regNo
65          * @param  integer $regYear
66          * @param  integer $mileage
67          * @param  integer $condition
68          * @return array
69          */
70         static public function searchByRegNo($regNo, $regYear, $mileage = null, $condition = null)
71         {
72                 $params = array(
73                                 'Credential' => array(),
74                                 'RegNo'      => $regNo,
75                                 'RegYear'    => $regYear
76                 );
77                 if (!is_null($mileage) && !is_null($condition)
78                                 && is_numeric($mileage) && is_numeric($condition)
79                                 && 0 < $condition && 6 > $condition)
80                 {
81                         $params['Mileage']   = $mileage;
82                         $params['Condition'] = $condition;
83                 }
84                 return self::_speaketh('SearchByRegNo', $params);
85         }
86
87         /**
88          * Collect vehicle particulars from MM code.
89          * @param  string  $mmCode
90          * @param  integer $regYear
91          * @param  integer $mileage
92          * @param  integer $condition
93          * @return array
94          */
95         static public function searchByMmCode($mmCode, $regYear, $mileage = null, $condition = null)
96         {
97                 if ('other' == $mmCode)
98                 {
99                         return array();
100                 }
101                 $mmCode = str_pad($mmCode, 8, "0", STR_PAD_LEFT);
102                 $params = array(
103                                 'Credential' => array(),
104                                 'MMCode'     => $mmCode,
105                                 'RegYear'    => $regYear
106                 );
107                 if (!is_null($mileage) && !is_null($condition)
108                                 && is_numeric($mileage) && is_numeric($condition)
109                                 && 0 < $condition && 6 > $condition)
110                 {
111                         $params['Mileage']   = $mileage;
112                         $params['Condition'] = $condition;
113                 }
114                 return self::_speaketh('SearchByMMCode', $params);
115         }
116
117         /**
118          * Speak to the TransUnion server.
119          * @param  string $channel
120          * @param  array  $message
121          * @return array|boolean
122          */
123         static private function _speaketh($method, array $params)
124         {
125                 $config = \Utility\Registry::getConfigParam('Transunion');
126                 $production = ('development' != getenv('APPLICATION_ENV'))
127                         ? true
128                         : false;
129                 $production = true;
130                 $url = $production
131                         ? self::$liveUrl
132                         : self::$devUrl;
133                 $credentials = $production
134                         ? array(
135                                                 'Username' => $config['Username'],
136                                                 'Password' => $config['Password']
137                                                 )
138                         : self::$devCredentials;
139
140                 $params['Credential'] = $credentials;
141                 $cmd = \Zend\Json\Json::encode($params);
142                 try
143                 {
144                         if (false && $production && function_exists('curl_init'))
145                         {
146                                 #-> Have curl, use it.
147                                 $ch = curl_init($url . '/' . $method);
148                                 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
149                                 curl_setopt($ch, CURLOPT_POSTFIELDS, $cmd);
150                                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
151                                 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
152                                                 'Content-Type: application/json',
153                                                 'Content-Length: ' . strlen($cmd))
154                                 );
155                                 $result = curl_exec($ch);
156                                 curl_close($ch);
157                                 return self::evalResult($result);
158                         }
159                         else
160                         {
161                                 #-> No curl, use the slower option.
162                                 $result = file_get_contents($url . '/' . $method, null, stream_context_create(array(
163                                                 'http' => array(
164                                                                 'method'  => 'POST',
165                                                                 'header'  => 'Content-Type: application/json' . "\r\n"
166                                                                                    . 'Content-Length: ' . strlen($cmd) . "\r\n",
167                                                                 'content' => $cmd,
168                                                 ),
169                                 )));
170                                 return self::evalResult($result, $params);
171                         }
172                 }
173                 catch (\Exception $e)
174                 {
175                         \Utility\Debug::errorLog(__CLASS__, "$e");
176                         return false;
177                 }
178         }
179
180         /**
181          * Pack result into array and provide a text status.
182          * @param unknown $result
183          * @return array|NULL
184          */
185         static protected function evalResult($result, $params)
186         {
187                 $authData = \Utility\Registry::getAuthData();
188                 $em = \Utility\Registry::getEntityManager();
189                 $logEntry = new \Utility\Entity\ApiLog();
190                 $logEntry->fromCompany = isset($authData['company']) && isset($authData['company']['id'])
191                         ? $em->getReference(
192                                 '\Company\Entity\Company',
193                                 $authData['company']['id']
194                                 )
195                         : null;
196                 $logEntry->fromProfile = isset($authData['id'])
197                         ? $em->getReference(
198                                 '\User\Entity\Profile',
199                                 $authData['id']
200                                 )
201                         : null;
202                 $logEntry->service = 'TransUnion';
203                 $logEntry->request = $params;
204                 try
205                 {
206                         $result = \Zend\Json\Json::decode($result, \Zend\Json\Json::TYPE_ARRAY);
207                         $statusMap = array(
208                                         2 => 'Success',
209                                         3 => 'No Results From TransUnion',
210                                         4 => 'No Values From TransUnion',
211                                         5 => 'Year is out of range'
212                                         );
213                         isset($result['Status'])
214                                         && isset($statusMap[$result['Status']])
215                                         && $result['Status'] = $statusMap[$result['Status']];
216                         $logEntry->response = $result;
217                         $logEntry->status = isset($result['Status'])
218                                 ? $result['Status']
219                                 : 'Error';
220                         $em->persist($logEntry);
221                         $em->flush();
222                         return $result;
223                 }
224                 catch (\Exception $e)
225                 {
226                         $logEntry->status = 'Error';
227                         $em->persist($logEntry);
228                         $em->flush();
229                         return null;
230                 }
231         }
232
233
234 }
235