public static function updateUserImage($userId)
 {
     $result = new FunctionResult();
     $result->success = false;
     if (!empty($userId)) {
         $user = GameUsers::getGameUserById($userId);
         if (!empty($user)) {
             $tmpImgPath = UserProfileImageUtils::downloadFBProfileImage($user->facebookId);
             $profileImageUrl = null;
             if (!empty($tmpImgPath)) {
                 try {
                     $s3 = new S3(AWS_API_KEY, AWS_SECRET_KEY);
                     $s3->setEndpoint(AWS_S3_ENDPOINT);
                     $imageName = "pi_" . $userId . ".png";
                     $s3Name = "profile.images/" . $userId . "/" . $imageName;
                     $res = $s3->putObjectFile($tmpImgPath, AWS_S3_BUCKET, $s3Name, S3::ACL_PUBLIC_READ);
                     if ($res) {
                         $profileImageUrl = 'http://' . AWS_S3_BUCKET . '.s3.amazonaws.com/' . $s3Name;
                         try {
                             unlink($tmpImgPath);
                         } catch (Exception $exc) {
                             error_log($exc->getTraceAsString());
                         }
                     } else {
                         $result->result = json_encode($res);
                     }
                 } catch (Exception $exc) {
                     $result->result = $exc->getTraceAsString();
                 }
             } else {
                 $profileImageUrl = USER_DEFAULT_IMAGE_URL;
             }
             if (!empty($profileImageUrl)) {
                 $user->setProfilePicture($profileImageUrl);
                 try {
                     $user->updateToDatabase(DBUtils::getConnection());
                     $result->success = true;
                     $result->result = null;
                 } catch (Exception $exc) {
                     $result->result = $exc->getTraceAsString();
                 }
             }
         } else {
             $result->result = "user not found";
         }
     } else {
         $result->result = "user id empty";
     }
     return $result;
 }
 public function updateImage()
 {
     $log = KLogger::instance(KLOGGER_PATH . "processors/", KLogger::DEBUG);
     $log->logInfo("user > updateImage > start userId : " . $this->userId);
     try {
         $userId = $this->userId;
         if (!empty($userId)) {
             $result = UserProfileImageUtils::updateUserImage($userId);
             $log->logInfo("user > updateImage >userId : " . $this->userId . " Result " . json_encode($result));
         } else {
             $log->logError("user > updateImage > start userId : " . $this->userId . " user found user id empty");
         }
     } catch (Exception $exc) {
         $log->logError("user > updateImage > error userId : " . $this->userId . " Error :" . $exc->getMessage());
     }
     $log->logInfo("user > updateImage > finished userId : " . $this->userId);
 }
ini_set('max_execution_time', 600);
define("__ROOT__", __DIR__ . "/../");
require_once __ROOT__ . 'config/constants.php';
require_once __ROOT__ . 'utils/Functions.php';
require_once __ROOT__ . 'utils/UserProfileImageFunctions.php';
require_once __ROOT__ . 'vendors/s3/S3.php';
require_once __ROOT__ . 'vendors/KLogger.php';
require_once __ROOT__ . "models/GameUsers.class.php";
$userId = null;
if (isset($_GET["userId"])) {
    $userId = $_GET["userId"];
} else {
    if (isset($_POST["userId"])) {
        $userId = $_POST["userId"];
    }
}
$result = new FunctionResult();
$result->success = false;
$log = KLogger::instance(KLOGGER_PATH . "jobs/", KLogger::DEBUG);
if (!empty($userId)) {
    $result = UserProfileImageUtils::updateUserImage($userId);
    if (!$result->success) {
        $log->logError($result->result);
    }
} else {
    $log->logError("user id empty");
    $result->result = "user id empty";
}
echo json_encode($result);
exit(1);