ClientPoller.php ➔ poll_SQS_queues()   B
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 11
nc 12
nop 2
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
1
#!/usr/bin/php
2
3
<?php
4
5
/**
6
 * This is an example of a client application listening for SQS messages
7
 * The CPE stack sends output message back to the client using the "output" SQS queue
8
 * The application needs to listen to this output queue to get updates 
9
 * Using the CpeClientSdk you can easily listen to the SQS
10
 */
11
12
require __DIR__ . "/vendor/autoload.php";
13
14
function poll_SQS_queues($CpeClientSdk, $decodedClient)
15
{
16
    try {
17
        // Will poll for 2 seconds
18
        if ($msg = $CpeClientSdk->receive_message($decodedClient, 10))
19
        {
20
            if (!($decoded = json_decode($msg['Body']))) {
21
                throw new Exception("JSON output data is invalid!");
22
            } else {
23
                handle_output($decoded);
24
            }
25
        }
26
    } catch (Exception $e) {
27
        print("[ERROR] " . $e->getMessage() . "\n");
28
    }
29
                    
30
    // Message polled. We delete it from SQS
31
    if (isset($msg) && $msg != "") {
32
        $CpeClientSdk->delete_message($decodedClient, $msg);
33
    }
34
}
35
36
function handle_output($output)
37
{
38
    global $debug;
39
    
40
    if ($debug) {
41
        print_r($output);
42
    }
43
}
44
45
/**
46
 * CLIENT POLLER START
47
 */
48
49
$debug  = false;
50
$region = getenv("AWS_DEFAULT_REGION");
51
$key    = getenv("AWS_ACCESS_KEY_ID");
52
$secret = getenv("AWS_SECRET_KEY");
53
54 View Code Duplication
function usage()
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Best Practice introduced by
The function usage() has been defined more than once; this definition is ignored, only the first definition in client_example/ClientCommander.php (L110-123) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
55
{
56
    echo("Usage: php " . basename(__FILE__) . " -c configFile [-h] [-k <key>] [-s <secret>] [-r <region>]\n");
57
    echo("-h: Print this help\n");
58
    echo("-d: Debug mode\n");
59
    echo("-c: configFile\n");
60
    echo("-k <AWS key>: Optional. Will use env variables by default\n");
61
    echo("-s <AWS secret>: Optional. Will use env variables by default\n");
62
    echo("-r <AWS region>: Optional. Will use env variables by default\n");
63
    exit(0);
64
}
65
66 View Code Duplication
function check_input_parameters()
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Best Practice introduced by
The function check_input_parameters() has been defined more than once; this definition is ignored, only the first definition in client_example/ClientCommander.php (L63-108) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
67
{
68
    global $region;
69
    global $secret;
70
    global $key;
71
    global $debug;
72
    global $clientInfo;
73
    global $argv;
74
    
75
    // Handle input parameters
76
    if (!($options = getopt("c:k::s::r::hd"))) {
77
        usage();
78
    }
79
    if (isset($options['h'])) {
80
        usage();
81
    }
82
    
83
    if (isset($options['d'])) {
84
        $debug = true;
85
    }
86
87
    if (isset($options['c']))
88
    {
89
        $clientConfFile = $options['c'];
90
        if (!file_exists($clientConfFile)) {
91
            throw new Exception("The client config file is not valid!");
92
        }
93
        if (!($clientInfo = file_get_contents($clientConfFile))) {
94
            throw new Exception("Unable to read the file");
95
        }
96
    } else {
97
        throw new Exception("Please provide the client config file!\n");
98
    }
99
  
100
    if (isset($options['k'])) {
101
        $key = $options['k'];
102
    } else {
103
        $key = getenv("AWS_ACCESS_KEY_ID");
104
    }
105
    
106
    if (isset($options['s'])) {
107
        $secret = $options['s'];
108
    } else {
109
        $secret = getenv("AWS_SECRET_KEY");
110
    }
111
112
    if (isset($options['r'])) {
113
        $region = $options['r'];
114
    } else {
115
        $region = getenv("AWS_DEFAULT_REGION");
116
    }
117
    if (!$region) {
118
        throw new Exception("Please provide your AWS region as parameter or using AWS_DEFAULT_REGION env var !");
119
    }
120
}
121
122
// Instanciate ComSDK to communicate with the stack
123
try {
124
    check_input_parameters();
125
    $CpeClientSdk = new SA\CpeClientSdk($key, $secret, $region, $debug);
126
} catch (Exception $e) {
127
    exit($e->getMessage());
128
  }
129
130
// You must JSON decode it
131
$decodedClient = json_decode($clientInfo);
132
133
// Keep polling for output messages!
134
while (42) {
135
    poll_SQS_queues($CpeClientSdk, $decodedClient);
136
}
137