getFromS3.php ➔ check_input_parameters()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 10
Code Lines 9

Duplication

Lines 6
Ratio 60 %

Importance

Changes 0
Metric Value
cc 7
eloc 9
nc 4
nop 1
dl 6
loc 10
rs 8.2222
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Script used to get a file in AWS S3
5
 **/
6
7
require __DIR__ . "/../../vendor/autoload.php";
8
9 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 src/activities/TranscodeAssetActivity.php (L282-292) 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...
10
{
11
    echo("Usage: php ". basename(__FILE__) . " [-h] [--force] --bucket <s3 bucket> --file <filename> --to <filepath>\n");
12
    echo("--help, -h: Print this help\n");
13
    echo("--bucket <s3 bucket>: Name of the S3 bucket\n");
14
    echo("--file <filename>: Name of the file in the S3 bucket\n");
15
    echo("--to <filepath>: Full path to file where to save. You can override original filename.\n");
16
    echo("--force: Force download even if file exists locally\n\n");
17
    exit(0);
18
}
19
20
function check_input_parameters($options)
21
{
22 View Code Duplication
    if (!count($options) || isset($options['h']) ||
23
        isset($options['help']))
24
        usage();
25
26 View Code Duplication
    if (!isset($options['bucket']) || !isset($options['file']) ||
27
        !isset($options['to']))
28
        throw new \SA\CpeSdk\CpeException("Missing mandatory parameter!");
29
}
30
31
$options = getopt("h", array("bucket:", "file:", "to:", "force::", "help::"));
32
check_input_parameters($options);
33
34
// If local file already exists. We don't download unless --force
35
if (!isset($options['force']) &&
36
    file_exists($options['to']) &&
37
    filesize($options['to']))
38
{
39
    $out = [ "status" => "SUCCESS",
40
             "msg" => "[".__FILE__."] Using local copy: '" . $options['to']  . "'" ];
41
    print json_encode($out)."\n";
42
    exit(0);
43
}
44
45
# Check if preper env vars are setup
46
if (!($region = getenv("AWS_DEFAULT_REGION")))
47
    throw new \SA\CpeSdk\CpeException("Set 'AWS_DEFAULT_REGION' environment variable!");
48
49
// Get S3 client
50
$s3 = new \Aws\S3\S3Client([
51
    'version' => 'latest',
52
    'region'  => $region
53
]);
54
55
// Download and Save object to a local file.
56
$res = $s3->getObject(array(
57
    'Bucket' => $options['bucket'],
58
    'Key'    => ltrim($options['file'], '/'),
59
    'SaveAs' => $options['to']
60
));
61
62
$out = [ "status" => "SUCCESS",
63
         "msg" => "[".__FILE__."] Download '" . $options['bucket'] . "/" . $options['file'] . "' successful !" ];
64
65
print json_encode($out)."\n";
66