<?php

$trackinginfo;

$code = $VARS["code"];
$carrier = "";
if (!empty($VARS["carrier"])) {
    $carrier = $VARS["carrier"];
}

try {
    $cacheresp = $memcache->get("logistics.tracking.$code.$carrier");
    if ($cacheresp !== false && empty($VARS["nocache"])) {
        exitWithJson(json_decode($cacheresp, true));
    }

    $trackinginfo = Tracking::track($code, $carrier);
} catch (TrackingException $ex) {
    if (env("debugmode", false)) {
        echo $ex->getTraceAsString();
    }
    $output = [
        "status" => "ERROR",
        "msg" => $ex->getMessage()
    ];
    $memcache->set("logistics.tracking.$code.$carrier", json_encode($output), 60 * 10);
    exitWithJson($output);
}

if (is_null($trackinginfo)) {
    sendJsonResp("Could not find any results for that tracking code.", "ERROR");
}

$output = $trackinginfo->toObject();
$memcache->set("logistics.tracking.$code.$carrier", json_encode($output), 60 * 60);
exitWithJson($output);
