Test Failed
Branch develop (6f7954)
by Andreas
05:13 queued 02:18
created

SKU::validateSKU()   C

Complexity

Conditions 9
Paths 9

Size

Total Lines 53
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 53
ccs 29
cts 29
cp 1
rs 6.8963
cc 9
eloc 29
nc 9
nop 1
crap 9

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Wambo\Catalog\Model;
4
5
use Wambo\Catalog\Exception\SKUException;
6
7
/**
8
 * Class SKU represents an unique identifier (stock keeping unit) for a product (e.g. "fancy-short-1")
9
 *
10
 * @package Wambo\Catalog\Model
11
 */
12
class SKU
13
{
14
    /**
15
     * @var string $whiteSpacePattern A regular expression that matches white-space characters
16
     */
17
    private $whiteSpacePattern = '/[\s]/';
18
19
    /**
20
     * @var string $invalidCharactersPattern A regular expression that matches all characters that are invalid for a SKU
21
     */
22
    private $invalidCharactersPattern = '/[^a-z0-9-]/';
23
24
    /**
25
     * @var string $uppercaseCharactersPattern A regular expression that matches uppercase characters
26
     */
27
    private $uppercaseCharactersPattern = '/[A-Z]/';
28
29
    /**
30
     * @var string $invalidPrefixCharacters A regular expression that matches characters that cannot be used as a prefix
31
     *      for SKUs
32
     */
33
    private $invalidPrefixCharacters = '/^[-]/';
34
35
    /**
36
     * @var string $invalidSuffixCharacters A regular expression that matches characters that cannot be used as a prefix
37
     *      for SKUs
38
     */
39
    private $invalidSuffixCharacters = '/[-]$/';
40
41
    /**
42
     * @var int $minLength Defines the minimum length for a SKU
43
     */
44
    private $minLength = 2;
45
46
    /**
47
     * @var int $maxLength Defines the maximum length of a SKU
48
     */
49
    private $maxLength = 32;
50
    /**
51
     * @var string
52
     */
53
    private $sku;
54
55
    /**
56
     * Create a new SKU instance
57
     *
58
     * @param string $sku A unique identifier for a product (e.g. "fancy-short-1")
59
     *
60
     * @throws SKUException If the given $sku is invalid
61
     */
62 47
    public function __construct(string $sku)
63
    {
64 47
        $this->validateSKU($sku);
65 16
        $this->sku = $sku;
66 16
    }
67
68
    /**
69
     * Get a string representation of the current SKU.
70
     * 
71
     * @return string
72
     */
73 1
    public function __toString()
74
    {
75 1
        return $this->sku;
76
    }
77
78
    /**
79
     * Validate the given SKU
80
     *
81
     * @param string $sku A unique identifier for a product (e.g. "fancy-short-1")
82
     *
83
     * @return void
84
     *
85
     * @throws SKUException If the given $sku is invalid
86
     */
87 47
    private function validateSKU(string $sku)
88
    {
89 47
        if (strlen($sku) == 0) {
90 1
            throw new SKUException("A SKU cannot be empty");
91
        }
92
93
        // check for white-space
94 46
        $containsWhitespace = preg_match($this->whiteSpacePattern, $sku) == 1;
95 46
        if ($containsWhitespace) {
96 4
            throw new SKUException(sprintf("A SKU cannot contain white space characters: \"%s\"", $sku));
97
        }
98
99
        // uppercase
100 42
        $containsUppercaseCharacters = preg_match($this->uppercaseCharactersPattern, $sku) == 1;
101 42
        if ($containsUppercaseCharacters) {
102 6
            throw new SKUException(sprintf("A SKU cannot contain uppercase characters: \"%s\"", $sku));
103
        }
104
105
        // check for invalid characters
106 36
        $containsInvalidCharacters = preg_match($this->invalidCharactersPattern, $sku) == 1;
107 36
        if ($containsInvalidCharacters) {
108 13
            throw new SKUException(sprintf("The SKU \"%s\" contains invalid characters. A SKU can only contain the following characters: a-z, 0-9 and -",
109
                $sku));
110
        }
111
112
        // check prefix
113 23
        $prefixMatches = [];
114 23
        $prefixContainsInvalidCharacters = preg_match($this->invalidPrefixCharacters, $sku, $prefixMatches) == 1;
115 23
        if ($prefixContainsInvalidCharacters) {
116 1
            throw new SKUException(sprintf("A SKU cannot start with the given characters: \"%s\"",
117 1
                implode("", $prefixMatches)));
118
        }
119
120
        // check suffix
121 22
        $suffixMatches = [];
122 22
        $suffixContainsInvalidCharacters = preg_match($this->invalidSuffixCharacters, $sku, $suffixMatches) == 1;
123 22
        if ($suffixContainsInvalidCharacters) {
124 1
            throw new SKUException(sprintf("A SKU cannot end with the given characters: \"%s\"",
125 1
                implode("", $suffixMatches)));
126
        }
127
128
        // check minimum length
129 21
        if (strlen($sku) < $this->minLength) {
130 4
            throw new SKUException(sprintf("The given SKU \"%s\" is too short. The minimum length for a SKU is: %s",
131 4
                $sku, $this->minLength));
132
        }
133
134
        // check maximum length
135 17
        if (strlen($sku) > $this->maxLength) {
136 1
            throw new SKUException(sprintf("The given SKU \"%s\" is too long (%s character). The maximum length for a SKU is: %s",
137 1
                strlen($sku), $sku, $this->maxLength));
138
        }
139
    }
140
}