Completed
Pull Request — master (#55)
by
unknown
05:07
created

getFromS3.php ➔ check_input_parameters()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 9
Ratio 69.23 %

Importance

Changes 0
Metric Value
cc 7
eloc 9
nc 4
nop 1
dl 9
loc 13
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
use Aws\S3\S3Client;
10
11 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...
12
{
13
    echo("Usage: php ". basename(__FILE__) . " [-h] [--force] --bucket <s3 bucket> --file <filename> --to <filepath>\n");
14
    echo("--help, -h: Print this help\n");
15
    echo("--bucket <s3 bucket>: Name of the S3 bucket\n");
16
    echo("--file <filename>: Name of the file in the S3 bucket\n");
17
    echo("--to <filepath>: Full path to file where to save. You can override original filename.\n");
18
    echo("--force: Force download even if file exists locally\n\n");
19
    exit(0);
20
}
21
22
function check_input_parameters($options)
23
{
24 View Code Duplication
    if (!count($options) || isset($options['h']) ||
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
25
        isset($options['help']))
26
        usage();
27
    
28 View Code Duplication
    if (!isset($options['bucket']) || !isset($options['file']) ||
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
29
        !isset($options['to']))
30
    {
31
        print "Error: Missing mandatory parameter !\n";
32
        usage();
33
    }
34
}
35
36
$options = getopt("h", array("bucket:", "file:", "to:", "force::", "help::"));
37
check_input_parameters($options);
38
39
// If local file already exists. We don't download unless --force
40
if (!isset($options['force']) && 
41
    file_exists($options['to']) &&
42
    filesize($options['to']))
43
{
44
    print json_encode([ "status" => "SUCCESS",
45
            "msg" => "[".__FILE__."] Using local copy: '" . $options['to']  . "'" ]);
46
    exit(0);
47
}
48
49
try {
50
    // Get S3 client
51
    $s3 = S3Client::factory();
52
    
53
    // Download and Save object to a local file.
54
    $s3->getObject(array(
55
            'Bucket' => $options['bucket'],
56
            'Key'    => $options['file'],
57
            'SaveAs' => $options['to']
58
        ));
59
60
    // Print JSON error output
61
    print json_encode([ "status" => "SUCCESS",
62
            "msg" => "[".__FILE__."] Download '" . $options['bucket'] . "/" . $options['file'] . "' successful !" ]);
63
} 
64
catch (Exception $e) {
65
    $err = "Unable to get '" . $options['bucket'] . "/" . $options['file'] . "' file from S3 ! " . $e->getMessage();
66
    // Print JSON error output
67
    print json_encode([ "status" => "ERROR",
68
            "msg" => "[".__FILE__."] $err" ]);
69
    
70
    die("[".__FILE__."] $err");
71
}
72