Passed
Push — master ( abb8c8...9e21c4 )
by Michiel
06:37
created

S3::createBucket()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
use Phing\Exception\BuildException;
21
22
/**
23
 * Abstract Service_Amazon_S3 class.
24
 *
25
 * Provides common methods and properties to all of the S3 tasks
26
 *
27
 * @version $ID$
28
 * @package phing.tasks.ext
29
 * @author  Andrei Serdeliuc <[email protected]>
30
 */
31
abstract class S3 extends Amazon
32
{
33
    /**
34
     * Services_Amazon_S3 client
35
     *
36
     * (default value: null)
37
     *
38
     * @var Aws\S3\S3Client
39
     */
40
    protected $client = null;
41
42
    /**
43
     * @var string
44
     */
45
    protected $bucket;
46
47
    /**
48
     * We only instantiate the client once per task call
49
     *
50
     * @return Aws\S3\S3Client
51
     *
52
     * @throws \Phing\Exception\BuildException
53
     */
54
    public function getClient()
55
    {
56
        if ($this->client === null) {
57
            try {
58
                $s3Client = new Aws\S3\S3Client(
59
                    [
60
                        'key' => $this->getKey(),
61
                        'secret' => $this->getSecret(),
62
                    ]
63
                );
64
            } catch (InvalidArgumentException $e) {
65
                throw new BuildException($e);
66
            }
67
68
            $this->client = $s3Client;
69
        }
70
71
        return $this->client;
72
    }
73
74
    /**
75
     * @param string $bucket
76
     * @throws BuildException if $bucket is a empty string
77
     */
78
    public function setBucket($bucket)
79
    {
80
        if (empty($bucket) || !is_string($bucket)) {
81
            throw new BuildException('Bucket must be a non-empty string');
82
        }
83
84
        $this->bucket = (string) $bucket;
85
    }
86
87
    /**
88
     * @return string
89
     *
90
     * @throws BuildException if bucket is not set
91
     */
92
    public function getBucket()
93
    {
94
        if (!($bucket = $this->bucket)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $bucket is dead and can be removed.
Loading history...
95
            throw new BuildException('Bucket is not set');
96
        }
97
98
        return $this->bucket;
99
    }
100
101
    /**
102
     * Returns an instance of Services_Amazon_S3_Resource_Object
103
     *
104
     * @param mixed $object
105
     *
106
     * @return Aws\Result
107
     *
108
     * @throws \Phing\Exception\BuildException
109
     */
110
    public function getObjectInstance($object)
111
    {
112
        return $this->getClientInstance()->getObject($object);
113
    }
114
115
    /**
116
     * Returns an instance of Services_Amazon_S3_Resource_Bucket
117
     *
118
     * @return \Aws\S3\S3Client
119
     */
120
    public function getClientInstance()
121
    {
122
        return $this->getClient();
123
    }
124
125
    /**
126
     * Check if the current bucket is available
127
     *
128
     * @return bool
129
     *
130
     * @throws \Phing\Exception\BuildException
131
     */
132
    public function isBucketAvailable()
133
    {
134
        return $this->getClientInstance()->doesBucketExist($this->getBucket());
135
    }
136
137
    /**
138
     * Create a bucket
139
     *
140
     * @return bool
141
     *
142
     * @throws \Phing\Exception\BuildException
143
     */
144
    public function createBucket()
145
    {
146
        $client = $this->getClientInstance();
147
        $client->createBucket(['Bucket' => $this->getBucket()]);
148
149
        return $this->isBucketAvailable();
150
    }
151
152
    /**
153
     * Main entry point, doesn't do anything
154
     *
155
     * @return void
156
     */
157
    final public function main()
158
    {
159
        $this->execute();
160
    }
161
162
    /**
163
     * Entry point to children tasks
164
     *
165
     * @return void
166
     */
167
    abstract public function execute();
168
}
169