Passed
Push — master ( afa2e3...eb32d2 )
by Siad
18:11
created

S3   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 143
ccs 0
cts 33
cp 0
rs 10
c 0
b 0
f 0
wmc 14

9 Methods

Rating   Name   Duplication   Size   Complexity  
A main() 0 3 1
A getObjectInstance() 0 3 1
A setBucket() 0 7 3
A isBucketAvailable() 0 3 1
A getClientInstance() 0 3 1
A isObjectAvailable() 0 3 1
A getClient() 0 18 3
A createBucket() 0 6 1
A getBucket() 0 7 2
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 <[email protected]>
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
    public function getClient()
48
    {
49
        if ($this->client === null) {
50
            try {
51
                $s3Client = new Aws\S3\S3Client(
52
                    [
53
                        'key' => $this->getKey(),
54
                        'secret' => $this->getSecret(),
55
                    ]
56
                );
57
            } catch (InvalidArgumentException $e) {
58
                throw new BuildException($e);
59
            }
60
61
            $this->client = $s3Client;
62
        }
63
64
        return $this->client;
65
    }
66
67
    /**
68
     * @param string $bucket
69
     * @throws BuildException if $bucket is a empty string
70
     */
71
    public function setBucket($bucket)
72
    {
73
        if (empty($bucket) || !is_string($bucket)) {
74
            throw new BuildException('Bucket must be a non-empty string');
75
        }
76
77
        $this->bucket = (string) $bucket;
0 ignored issues
show
Bug Best Practice introduced by
The property bucket does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
78
    }
79
80
    /**
81
     * @return string
82
     *
83
     * @throws BuildException if bucket is not set
84
     */
85
    public function getBucket()
86
    {
87
        if (!($bucket = $this->bucket)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $bucket is dead and can be removed.
Loading history...
88
            throw new BuildException('Bucket is not set');
89
        }
90
91
        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
    public function getObjectInstance($object)
104
    {
105
        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
    public function isObjectAvailable($object)
115
    {
116
        return (bool) $this->getObjectInstance($object)->load(Services_Amazon_S3_Resource_Object::LOAD_METADATA_ONLY);
0 ignored issues
show
Bug introduced by
The type Services_Amazon_S3_Resource_Object was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The method load() does not exist on Aws\Result. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

116
        return (bool) $this->getObjectInstance($object)->/** @scrutinizer ignore-call */ load(Services_Amazon_S3_Resource_Object::LOAD_METADATA_ONLY);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
    }
118
119
    /**
120
     * Returns an instance of Services_Amazon_S3_Resource_Bucket
121
     *
122
     * @return \Aws\S3\S3Client
123
     */
124
    public function getClientInstance()
125
    {
126
        return $this->getClient();
127
    }
128
129
    /**
130
     * Check if the current bucket is available
131
     *
132
     * @return bool
133
     *
134
     * @throws \BuildException
135
     */
136
    public function isBucketAvailable()
137
    {
138
        return $this->getClientInstance()->doesBucketExist($this->getBucket());
139
    }
140
141
    /**
142
     * Create a bucket
143
     *
144
     * @return bool
145
     *
146
     * @throws \BuildException
147
     */
148
    public function createBucket()
149
    {
150
        $client = $this->getClientInstance();
151
        $client->createBucket(['Bucket' => $this->getBucket()]);
152
153
        return $this->isBucketAvailable();
154
    }
155
156
    /**
157
     * Main entry point, doesn't do anything
158
     *
159
     * @return void
160
     */
161
    final public function main()
162
    {
163
        $this->execute();
164
    }
165
166
    /**
167
     * Entry point to children tasks
168
     *
169
     * @return void
170
     */
171
    abstract public function execute();
172
}
173