Passed
Pull Request — master (#5)
by Tibor
03:15
created

UploadPie::resolveAuthParam()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Bex\Behat\ScreenshotExtension\Driver;
4
5
use Bex\Behat\ScreenshotExtension\Driver\ImageDriverInterface;
6
use Bex\Behat\ScreenshotExtension\Driver\Service\UploadPieApi;
7
use Buzz\Client\Curl;
8
use Buzz\Message\Form\FormRequest;
9
use Buzz\Message\Form\FormUpload;
10
use Buzz\Message\Response;
11
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
12
use Symfony\Component\DependencyInjection\ContainerBuilder;
13
14
class UploadPie implements ImageDriverInterface
15
{
16
    const CONFIG_PARAM_EXPIRE = 'expire';
17
    const CONFIG_PARAM_AUTH = 'auth';
18
19
    /**
20
     * @var array
21
     */
22
    private $expireMapping = ['30m' => 1, '1h' => 2, '6h' => 3, '1d' => 4, '1w' => 5];
23
24
    /**
25
     * @var UploadPieApi
26
     */
27
    private $api;
28
29
    /**
30
     * @var int
31
     */
32
    private $expire;
33
34
    /**
35
     * @var string
36
     */
37
    private $auth;
38
39
    /**
40
     * @param UploadPieApi|null $api
41
     */
42
    public function __construct(UploadPieApi $api = null)
43
    {
44
        $this->api = $api ?: new UploadPieApi();
45
    }
46
47
    /**
48
     * @param  ArrayNodeDefinition $builder
49
     */
50
    public function configure(ArrayNodeDefinition $builder)
51
    {
52
        $builder
53
            ->children()
54
                ->enumNode(self::CONFIG_PARAM_EXPIRE)
55
                    ->values(array('30m', '1h', '6h', '1d', '1w'))
56
                    ->defaultValue('30m')
57
                ->end()
58
                ->scalarNode(self::CONFIG_PARAM_AUTH)
59
                    ->isRequired()
60
                    ->cannotBeEmpty()
61
                ->end()
62
            ->end();
63
    }
64
65
    /**
66
     * @param  ContainerBuilder $container
67
     * @param  array            $config
68
     */
69
    public function load(ContainerBuilder $container, array $config)
70
    {
71
        $this->expire = $this->convertExpireValue($config[self::CONFIG_PARAM_EXPIRE]);
72
        $this->auth = $this->resolveAuthParam($config[self::CONFIG_PARAM_AUTH]);
73
    }
74
75
    /**
76
     * @param string $binaryImage
77
     * @param string $filename
78
     *
79
     * @return string URL to the image
80
     */
81
    public function upload($binaryImage, $filename)
82
    {
83
        return $this->api->call($binaryImage, $filename, $this->expire, $this->auth);
84
    }
85
86
    /**
87
     * @param  string $expire
88
     *
89
     * @return int
90
     */
91
    private function convertExpireValue($expire)
92
    {
93
        return $this->expireMapping[$expire];
94
    }
95
96
    /**
97
     * @param  string $auth
98
     *
99
     * @return string
100
     */
101
    private function resolveAuthParam($auth)
102
    {
103
        $value = getenv($auth);
104
105
        if ($value !== false) {
106
            return $value;
107
        }
108
109
        return $auth;
110
    }
111
}
112