/*jslint node: true, nomen: true */

module.exports = function (childProcess, gearman) {
    "use strict";

    var syncUserProfileOnPostsByUserId,
        syncedCallback;

    syncUserProfileOnPostsByUserId = function (id, syncedCallback, worker) {
        var filteredId = parseInt(id, 10),
            cmdParams,
            cmd;

        if (isNaN(filteredId)) {
            syncedCallback({error: true, message: "User ID received is not a number."});
            return;
        }

        cmdParams = "--controller search --action sync-user-profile-on-posts --uid " + filteredId;

        cmd = "php " + __dirname + "/../../../bin/services " + cmdParams;

        childProcess.exec(cmd, function (error, stdout, stderr) {
            if (error === null) {
                syncedCallback({error: false, message: stdout}, worker);
                return;
            }

            syncedCallback({error: true, message: stderr}, worker);
        });
    };

    syncedCallback = function (result, worker) {
        if (result.error) {
            worker.error();
            console.error(result.message + "\n");
            return;
        }

        worker.end();
        console.info(result.message + "\n");
    };

    exports.setUpSyncUserProfileOnPostsFromUserWorker = function () {
        gearman.registerWorker("syncUserProfileOnPosts", function (payload, worker) {
            var uid = payload.toString("utf-8");

            syncUserProfileOnPostsByUserId(uid, syncedCallback, worker);

            worker.end();
        });
    };

    return exports;
};

@@ -86,17 +86,17 @@
 ) %7B%0A    
-%22
+'
 use stri
@@ -97,17 +97,17 @@
 e strict
-%22
+'
 ;%0A%0A    v
@@ -410,17 +410,17 @@
 essage: 
-%22
+'
 User ID 
@@ -444,17 +444,17 @@
  number.
-%22
+'
 %7D);%0A    
@@ -500,17 +500,17 @@
 arams = 
-%22
+'
 --contro
@@ -563,17 +563,17 @@
 s --uid 
-%22
+'
  + filte
@@ -598,14 +598,14 @@
 d = 
-%22
+'
 php 
-%22
+'
  + _
@@ -615,17 +615,17 @@
 rname + 
-%22
+'
 /../../.
@@ -639,17 +639,17 @@
 ervices 
-%22
+'
  + cmdPa
@@ -1099,28 +1099,28 @@
 t.message + 
-%22%5Cn%22
+'%5Cn'
 );%0A         
@@ -1205,12 +1205,12 @@
 e + 
-%22%5Cn%22
+'%5Cn'
 );%0A 
@@ -1317,17 +1317,17 @@
 rWorker(
-%22
+'
 syncUser
@@ -1340,17 +1340,17 @@
 eOnPosts
-%22
+'
 , functi
@@ -1415,15 +1415,15 @@
 ing(
-%22
+'
 utf-8
-%22
+'
 );%0A%0A

improve code style of manager.js