S3BucketNameValidator::validate()   B
last analyzed

Complexity

Conditions 9
Paths 192

Size

Total Lines 41
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 9

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 41
ccs 19
cts 19
cp 1
rs 7.2888
c 0
b 0
f 0
cc 9
nc 192
nop 1
crap 9
1
<?php
2
/**
3
 *  This file is part of the Simple S3 package.
4
 *
5
 * (c) Mauro Cassani<https://github.com/mauretto78>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 */
11
12
namespace Matecat\SimpleS3\Components\Validators;
13
14
/**
15
 * This class check if a string is a valid S3 Bucket Name
16
 *
17
 * Amazon S3 Bucket Naming Requirements
18
 * -------------------------------------------------------------------------
19
 * The Amazon S3 bucket that you use to store CloudTrail log files must have a name that conforms with naming requirements for non-US Standard regions. Amazon S3 defines a bucket name as a series of one or more labels, separated by periods, that adhere to the following rules:
20
 *
21
 * - The bucket name can be between 3 and 63 characters long, and can contain only lower-case characters, numbers, periods, and dashes.
22
 * - Each label in the bucket name must start with a lowercase letter or number.
23
 * - The bucket name cannot contain underscores, end with a dash, have consecutive periods, or use dashes adjacent to periods.
24
 * - The bucket name cannot be formatted as an IP address (198.51.100.24).
25
 *
26
 * Complete reference:
27
 *
28
 * https://docs.aws.amazon.com/en_us/awscloudtrail/latest/userguide/cloudtrail-s3-bucket-naming-requirements.html
29
 *
30
 * @package SimpleS3
31
 */
32
final class S3BucketNameValidator extends S3NameValidator
33
{
34
    /**
35
     * @param string $string
36
     *
37
     * @return array
38
     */
39 4
    public static function validate($string)
40
    {
41 4
        $errors = [];
42
43 4
        if (strlen($string) < 3) {
44 1
            $errors[] = 'The string is too short';
45
        }
46
47 4
        if (strlen($string) > 64) {
48 1
            $errors[] = 'The string is too long';
49
        }
50
51 4
        if (filter_var($string, FILTER_VALIDATE_IP)) {
52 2
            $errors[] = 'The string is an IP address';
53
        }
54
55 4
        if (strtolower($string) !== $string) {
56 1
            $errors[] = 'The string contains capital letters';
57
        }
58
59 4
        if (preg_match('/[^a-z.\-0-9]/i', $string)) {
60 1
            $errors[] = 'The string contains a not allowed character';
61
        }
62
63 4
        if (substr($string, -1) === '-') {
64 1
            $errors[] = 'The string ends with a -';
65
        }
66
67
        $notAllowedCombos = [
68 4
                '..',
69
                '-.',
70
                '.-'
71
        ];
72
73 4
        foreach ($notAllowedCombos as $notAllowedCombo) {
74 4
            if (strpos($string, $notAllowedCombo)) {
75 1
                $errors[] = 'The string contains a not allowed character combination';
76
            }
77
        }
78
79 4
        return $errors;
80
    }
81
}
82