GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( fa157d...54321f )
by Arturs
01:51
created

AwsS3   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 101
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 14 1
C load() 0 31 7
A upload() 0 7 1
A createClient() 0 4 1
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
    const CONFIG_PARAM_NAMESPACE = 'namespace';
19
    /**
20
     * @var S3Client
21
     */
22
    private $api;
23
    /**
24
     * @var string
25
     */
26
    private $bucket;
27
    /**
28
     * @var string
29
     */
30
    private $namespace;
31
32
    /**
33
     * @param  ArrayNodeDefinition $builder
34
     */
35
    public function configure(ArrayNodeDefinition $builder)
36
    {
37
        $builder
38
            ->children()
39
                ->scalarNode(self::CONFIG_PARAM_BUCKET)->isRequired()->end()
40
                ->scalarNode(self::CONFIG_PARAM_VERSION)->defaultValue('latest')->end()
41
                ->scalarNode(self::CONFIG_PARAM_REGION)->isRequired()->end()
42
                ->scalarNode(self::CONFIG_PARAM_CREDENTIALS_KEY)->defaultNull()->end()
43
                ->scalarNode(self::CONFIG_PARAM_CREDENTIALS_SECRET)->defaultNull()->end()
44
                ->scalarNode(self::CONFIG_PARAM_CREDENTIALS_TOKEN)->defaultNull()->end()
45
                ->scalarNode(self::CONFIG_PARAM_CLIENT_FACTORY)->defaultNull()->end()
46
                ->scalarNode(self::CONFIG_PARAM_NAMESPACE)->defaultNull()->end()
47
            ->end();
48
    }
49
50
    /**
51
     * @param  ContainerBuilder $container
52
     * @param  array $config
53
     */
54
    public function load(ContainerBuilder $container, array $config)
55
    {
56
        $this->bucket = $config[self::CONFIG_PARAM_BUCKET];
57
58
        $this->namespace = $config[self::CONFIG_PARAM_NAMESPACE] ?: sprintf('%d%04d', time(), rand(0, 9999));
59
        $version = $config[self::CONFIG_PARAM_VERSION];
60
        $region = $config[self::CONFIG_PARAM_REGION];
61
        $credentials = null;
62
        if ($config[self::CONFIG_PARAM_CREDENTIALS_KEY] && $config[self::CONFIG_PARAM_CREDENTIALS_SECRET]) {
63
            $credentials = [
64
                'key' => $config[self::CONFIG_PARAM_CREDENTIALS_KEY],
65
                'secret' => $config[self::CONFIG_PARAM_CREDENTIALS_SECRET],
66
                'token' => $config[self::CONFIG_PARAM_CREDENTIALS_TOKEN],
67
            ];
68
        } elseif ($config[self::CONFIG_PARAM_CREDENTIALS_KEY] === false) {
69
            $credentials = false;
70
        }
71
72
        $clientFactory = $config[self::CONFIG_PARAM_CLIENT_FACTORY] ?: [$this, 'createClient'];
73
        if (!is_callable($clientFactory)) {
74
            throw new \RuntimeException('Invalid S3 API client factory callback');
75
        }
76
77
        $args = [
78
            'version' => $version,
79
            'region' => $region,
80
            'credentials' => $credentials,
81
        ];
82
83
        $this->api = call_user_func($clientFactory, $args);
84
    }
85
86
    /**
87
     * @param string $binaryImage
88
     * @param string $filename
89
     *
90
     * @return string URL to the image
91
     */
92
    public function upload($binaryImage, $filename)
93
    {
94
        $path = join('/', [$this->namespace, $filename]);
95
        $result = $this->api->upload($this->bucket, $path, $binaryImage, 'public-read');
96
97
        return $result['ObjectURL'];
98
    }
99
100
    /**
101
     * @param array $args
102
     *
103
     * @return S3Client
104
     */
105
    public function createClient($args)
106
    {
107
        return new S3Client($args);
108
    }
109
}
110