1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bex\Behat\ScreenshotExtension\Driver; |
4
|
|
|
|
5
|
|
|
use Aws\S3\S3Client; |
6
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
8
|
|
|
|
9
|
|
|
class AwsS3 implements ImageDriverInterface |
10
|
|
|
{ |
11
|
|
|
const CONFIG_PARAM_BUCKET = 'bucket'; |
12
|
|
|
const CONFIG_PARAM_VERSION = 'version'; |
13
|
|
|
const CONFIG_PARAM_REGION = 'region'; |
14
|
|
|
const CONFIG_PARAM_CREDENTIALS_KEY = 'credentials_key'; |
15
|
|
|
const CONFIG_PARAM_CREDENTIALS_SECRET = 'credentials_secret'; |
16
|
|
|
const CONFIG_PARAM_CREDENTIALS_TOKEN = 'credentials_token'; |
17
|
|
|
const CONFIG_PARAM_CLIENT_FACTORY = 'client_factory'; |
18
|
|
|
/** |
19
|
|
|
* @var S3Client |
20
|
|
|
*/ |
21
|
|
|
private $api; |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $bucket; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ArrayNodeDefinition $builder |
29
|
|
|
*/ |
30
|
|
|
public function configure(ArrayNodeDefinition $builder) |
31
|
|
|
{ |
32
|
|
|
$builder |
33
|
|
|
->children() |
34
|
|
|
->scalarNode(self::CONFIG_PARAM_BUCKET)->isRequired()->end() |
35
|
|
|
->scalarNode(self::CONFIG_PARAM_VERSION)->defaultValue('latest')->end() |
36
|
|
|
->scalarNode(self::CONFIG_PARAM_REGION)->isRequired()->end() |
37
|
|
|
->scalarNode(self::CONFIG_PARAM_CREDENTIALS_KEY)->end() |
38
|
|
|
->scalarNode(self::CONFIG_PARAM_CREDENTIALS_SECRET)->end() |
39
|
|
|
->scalarNode(self::CONFIG_PARAM_CREDENTIALS_TOKEN)->end() |
40
|
|
|
->scalarNode(self::CONFIG_PARAM_CLIENT_FACTORY)->defaultNull()->end() |
41
|
|
|
->end(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param ContainerBuilder $container |
46
|
|
|
* @param array $config |
47
|
|
|
*/ |
48
|
|
|
public function load(ContainerBuilder $container, array $config) |
49
|
|
|
{ |
50
|
|
|
$this->bucket = $config[self::CONFIG_PARAM_BUCKET]; |
51
|
|
|
|
52
|
|
|
$version = $config[self::CONFIG_PARAM_VERSION]; |
53
|
|
|
$region = $config[self::CONFIG_PARAM_REGION]; |
54
|
|
|
$credentials = null; |
55
|
|
|
if ($config[self::CONFIG_PARAM_CREDENTIALS_KEY] && $config[self::CONFIG_PARAM_CREDENTIALS_SECRET]) { |
56
|
|
|
$credentials = [ |
57
|
|
|
'key' => $config[self::CONFIG_PARAM_CREDENTIALS_KEY], |
58
|
|
|
'secret' => $config[self::CONFIG_PARAM_CREDENTIALS_SECRET], |
59
|
|
|
'token' => $config[self::CONFIG_PARAM_CREDENTIALS_TOKEN], |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
$clientFactory = $config[self::CONFIG_PARAM_CLIENT_FACTORY] ?: [$this, 'createClient']; |
63
|
|
|
if (!is_callable($clientFactory)) { |
64
|
|
|
throw new \RuntimeException('Invalid S3 API client factory callback'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$args = [ |
68
|
|
|
'version' => $version, |
69
|
|
|
'region' => $region, |
70
|
|
|
'credentials' => $credentials, |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
$this->api = call_user_func($clientFactory, $args); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $binaryImage |
78
|
|
|
* @param string $filename |
79
|
|
|
* |
80
|
|
|
* @return string URL to the image |
81
|
|
|
*/ |
82
|
|
|
public function upload($binaryImage, $filename) |
83
|
|
|
{ |
84
|
|
|
$result = $this->api->upload($this->bucket, $filename, $binaryImage, 'public-read'); |
85
|
|
|
|
86
|
|
|
return $result['ObjectURL']; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param array $args |
91
|
|
|
* |
92
|
|
|
* @return S3Client |
93
|
|
|
*/ |
94
|
|
|
public function createClient($args) |
95
|
|
|
{ |
96
|
|
|
return new S3Client($args); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|