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
/**
21
 * Abstract Service_Amazon_S3 class.
22
 *
23
 * Provides common methods and properties to all of the S3 tasks
24
 *
25
 * @version $ID$
26
 * @package phing.tasks.ext
27
 * @author  Andrei Serdeliuc <andrei@serdeliuc.ro>
28
 */
29
abstract class S3 extends Amazon
30
{
31
    /**
32
     * Services_Amazon_S3 client
33
     *
34
     * (default value: null)
35
     *
36
     * @var Aws\S3\S3Client
37
     */
38
    protected $client = null;
39

40
    /**
41
     * We only instantiate the client once per task call
42
     *
43
     * @return Aws\S3\S3Client
44
     *
45
     * @throws \BuildException
46
     */
47 0
    public function getClient()
48
    {
49 0
        if ($this->client === null) {
50
            try {
51 0
                $s3Client = new Aws\S3\S3Client(
52
                    [
53 0
                        'key' => $this->getKey(),
54 0
                        'secret' => $this->getSecret(),
55
                    ]
56
                );
57 0
            } catch (InvalidArgumentException $e) {
58 0
                throw new BuildException($e);
59
            }
60

61 0
            $this->client = $s3Client;
62
        }
63

64 0
        return $this->client;
65
    }
66

67
    /**
68
     * @param string $bucket
69
     * @throws BuildException if $bucket is a empty string
70
     */
71 0
    public function setBucket($bucket)
72
    {
73 0
        if (empty($bucket) || !is_string($bucket)) {
74 0
            throw new BuildException('Bucket must be a non-empty string');
75
        }
76

77 0
        $this->bucket = (string) $bucket;
78
    }
79

80
    /**
81
     * @return string
82
     *
83
     * @throws BuildException if bucket is not set
84
     */
85 0
    public function getBucket()
86
    {
87 0
        if (!($bucket = $this->bucket)) {
88 0
            throw new BuildException('Bucket is not set');
89
        }
90

91 0
        return $this->bucket;
92
    }
93

94
    /**
95
     * Returns an instance of Services_Amazon_S3_Resource_Object
96
     *
97
     * @param mixed $object
98
     *
99
     * @return Aws\Result
100
     *
101
     * @throws \BuildException
102
     */
103 0
    public function getObjectInstance($object)
104
    {
105 0
        return $this->getClientInstance()->getObject($object);
106
    }
107

108
    /**
109
     * Check if the object already exists in the current bucket
110
     *
111
     * @param  mixed $object
112
     * @return bool
113
     */
114 0
    public function isObjectAvailable($object)
115
    {
116 0
        return (bool) $this->getObjectInstance($object)->load(Services_Amazon_S3_Resource_Object::LOAD_METADATA_ONLY);
117
    }
118

119
    /**
120
     * Returns an instance of Services_Amazon_S3_Resource_Bucket
121
     *
122
     * @return \Aws\S3\S3Client
123
     */
124 0
    public function getClientInstance()
125
    {
126 0
        return $this->getClient();
127
    }
128

129
    /**
130
     * Check if the current bucket is available
131
     *
132
     * @return bool
133
     *
134
     * @throws \BuildException
135
     */
136 0
    public function isBucketAvailable()
137
    {
138 0
        return $this->getClientInstance()->doesBucketExist($this->getBucket());
139
    }
140

141
    /**
142
     * Create a bucket
143
     *
144
     * @return bool
145
     *
146
     * @throws \BuildException
147
     */
148 0
    public function createBucket()
149
    {
150 0
        $client = $this->getClientInstance();
151 0
        $client->createBucket(['Bucket' => $this->getBucket()]);
152

153 0
        return $this->isBucketAvailable();
154
    }
155

156
    /**
157
     * Main entry point, doesn't do anything
158
     *
159
     * @return void
160
     */
161 0
    final public function main()
162
    {
163 0
        $this->execute();
164
    }
165

166
    /**
167
     * Entry point to children tasks
168
     *
169
     * @return void
170
     */
171
    abstract public function execute();
172
}

Read our documentation on viewing source code .

Loading