Completed
Push — master ( a06a95...a1d542 )
by dan
15s
created

S3Bridge.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace IrishDan\ResponsiveImageBundle;
4
5
use Aws\CommandPool;
6
use Aws\Exception\AwsException;
7
use Aws\S3\S3Client;
8
9
/**
10
 * Class S3Bridge
11
 *
12
 * @package ResponsiveImageBundle
13
 */
14
class S3Bridge
15
{
16
    /**
17
     * @var
18
     */
19
    private $bucket;
20
    /**
21
     * @var
22
     */
23
    private $region;
24
    /**
25
     * @var string
26
     */
27
    private $directory;
28
    /**
29
     * @var
30
     */
31
    private $key;
32
    /**
33
     * @var
34
     * ['system/location', 'styled/location']
35
     */
36
    private $paths;
37
    /**
38
     * @var
39
     */
40
    private $secret;
41
    /**
42
     * @var
43
     */
44
    private $s3;
45
46
    /**
47
     * S3Bridge constructor.
48
     *
49
     * @param $config
50
     */
51
    public function __construct($config)
52
    {
53
        if (!empty($config['bucket'])) {
54
            $this->bucket = $config['bucket'];
55
            $this->directory = empty($config['directory']) ? '' : $config['directory'] . '/';
56
            $this->key = $config['access_key_id'];
57
            $this->region = $config['region'];
58
            $this->secret = $config['secret_access_key'];
59
            $this->version = $config['version'];
0 ignored issues
show
The property version does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
60
        }
61
    }
62
63
    /**
64
     * @param $storePath
65
     * @param $key
66
     */
67
    public function fetchS3Object($storePath, $key)
68
    {
69
        $this->getClient();
70
        // Save object to a file.
71
        $result = $this->s3->getObject([
0 ignored issues
show
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
72
            'Bucket' => $this->bucket,
73
            'Key' => $key,
74
            'SaveAs' => $storePath,
75
        ]);
76
    }
77
78
    /**
79
     * Initialise the S3 client.
80
     */
81
    public function getClient()
82
    {
83
        // AWS access info
84
        $this->s3 = S3Client::factory([
85
            'version' => $this->version,
86
            'region' => $this->region,
87
            'credentials' => [
88
                'key' => $this->key,
89
                'secret' => $this->secret,
90
            ],
91
        ]);
92
    }
93
94
    /**
95
     * Removes all the images set in the $this->paths array from the configured S3 bucket.
96
     */
97
    public function removeFromS3()
98
    {
99
        $this->getClient();
100
        $objects = [];
101
        foreach ($this->paths as $path => $file) {
102
            $objects[] = ['Key' => $this->directory . $file];
103
        }
104
        $result = $this->s3->deleteObjects([
0 ignored issues
show
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
105
            'Bucket' => $this->bucket,
106
            'Delete' => [
107
                'Objects' => $objects,
108
            ],
109
        ]);
110
    }
111
112
    /**
113
     * @param      $paths
114
     * @param bool $clear
115
     */
116
    public function setPaths($paths, $clear = false)
117
    {
118
        if ($clear) {
119
            $this->paths = [];
120
        }
121
        foreach ($paths as $systemLocation => $styledLocation) {
122
            $this->paths[$systemLocation] = $styledLocation;
123
        }
124
    }
125
126
    /**
127
     * Tranfers all the images set in the $this->paths array to the configured S3 bucket.
128
     */
129
    public function uploadToS3()
130
    {
131
        $this->getClient();
132
        $commands = [];
133
        foreach ($this->paths as $path => $file) {
134
            $commands[] = $this->s3->getCommand(
135
                'PutObject', [
136
                    'region' => $this->region,
137
                    'Bucket' => $this->bucket,
138
                    'Key' => $this->directory . $file,
139
                    'SourceFile' => $path,
140
                    'ACL' => 'public-read',
141
                    'StorageClass' => 'REDUCED_REDUNDANCY',
142
                ]
143
            );
144
145
            $pool = new CommandPool($this->s3, $commands);
146
            $promise = $pool->promise();
147
148
            // Force the pool to complete synchronously
149
            try {
150
                $result = $promise->wait();
0 ignored issues
show
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
151
            } catch (AwsException $e) {
0 ignored issues
show
The class Aws\Exception\AwsException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
152
                // @TODO: Handle the error.
153
            }
154
        }
155
    }
156
157
    /**
158
     * @return array
159
     */
160
    public function listImages()
161
    {
162
        $images = [];
163
        $this->getClient();
164
165
        try {
166
            $iterator = $this->s3->getIterator('ListObjects', [
167
                'Bucket' => $this->bucket,
168
            ]);
169
170
            foreach ($iterator as $object) {
171
                $images[] = $object;
172
            }
173
        } catch (AwsException $exception) {
0 ignored issues
show
The class Aws\Exception\AwsException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
174
            // @TODO: Improve exceptions handling.
175
        }
176
177
        return $images;
178
    }
179
}