S3Utils::put_file_into_s3()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 22
nc 4
nop 7
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * This class allows you to call the two S3 scripts
4
 * to download and upload files.
5
 * The scripts are executed using the CommandExecuter
6
  */
7
8
require_once __DIR__ . '/CommandExecuter.php';
9
10
use SA\CpeSdk;
11
12
class S3Utils
13
{
14
    public $cpeLogger;
15
16
    const S3_OPS_FAILED        = "S3_OPS_FAILED";
17
    const NO_OUTPUT_DATA       = "NO_OUTPUT_DATA";
18
    
19
    // External S3 Scripts
20
    const GET_FROM_S3          = "/../scripts/getFromS3.php";
21
    const PUT_IN_S3            = "/../scripts/putInS3.php";
22
    
23
    public function __construct($cpeLogger = null)
24
    {
25
        if (!$cpeLogger)
26
            $this->cpeLogger = new CpeSdk\CpeLogger(null, 'S3Utils');
27
        $this->cpeLogger = $cpeLogger;
28
    }
29
30
    // Get a file from S3 using external script localted in "scripts" folder
31
    public function get_file_from_s3(
32
        $bucket, 
33
        $filename, 
34
        $saveFileTo,
35
        $callback = null, 
36
        $callbackParams = null,
37
        $logKey = null)
38
    {   
39
        $cmd = "php " . __DIR__ . self::GET_FROM_S3;
40
        $cmd .= " --bucket $bucket";
41
        $cmd .= " --file $filename";
42
        $cmd .= " --to $saveFileTo";
43
    
44
        // HAndle execution
45
        return ($this->handle_s3_ops(
46
                self::GET_FROM_S3,
47
                $cmd, 
48
                $callback,
49
                $callbackParams,
50
                $logKey));
51
    }
52
53
    // Get a file from S3 using external script localted in "scripts" folder
54
    public function put_file_into_s3(
55
        $bucket, 
56
        $filename, 
57
        $pathToFileToSend, 
58
        $options, 
59
        $callback = null, 
60
        $callbackParams = null,
61
        $logKey = null)
62
    {
63
        $cmd  = "php " . __DIR__ . self::PUT_IN_S3;
64
        $cmd .= " --bucket $bucket";
65
        $cmd .= " --file $filename";
66
        $cmd .= " --from $pathToFileToSend";
67
        if ($options['rrs'])
68
            $cmd .= " --rrs";
69
        if ($options['encrypt'])
70
            $cmd .= " --encrypt";
71
    
72
        // HAndle execution
73
        return ($this->handle_s3_ops(
74
                self::PUT_IN_S3,
75
                $cmd, 
76
                $callback,
77
                $callbackParams,
78
                $logKey)
79
        );
80
    }
81
82
    // Execute S3 $cmd and capture output
83
    private function handle_s3_ops($caller, $cmd, $callback, $callbackParams, $logKey)
84
    {
85
        // Use executer to start external S3 script
86
        // The array request listening to 1 (STDOUT) and 2 (STDERR)
87
        $executer = new CommandExecuter($this->cpeLogger, $logKey);
88
        $out = $executer->execute(
89
            $cmd,
90
            1,
91
            array(1 => array("pipe", "w"),
92
                  2 => array("pipe", "w")),
93
            $callback,
94
            $callbackParams, 
95
            true,
96
            2
97
        );
98
        
99
        if ($out['outErr'])
100
            throw new CpeSdk\CpeException($out['outErr'],
101
                self::S3_OPS_FAILED);
102
103
        if (!$out['out'])
104
            throw new CpeSdk\CpeException("Script '$caller' didn't return any data !",
105
                self::NO_OUTPUT_DATA);
106
    
107
        if (!($decoded = json_decode($out['out'], true)))
108
            throw new CpeSdk\CpeException($out['out'],
109
                self::S3_OPS_FAILED);
110
    
111
        if ($decoded["status"] == "ERROR")
112
            throw new CpeSdk\CpeException($decoded["msg"],
113
                self::S3_OPS_FAILED);
114
115
        return ($decoded);
116
    }
117
118
}