Completed
by Gabriel
12s
created

### Euro::centsFromString()   A

#### Complexity

 Conditions 2 Paths 2

#### Size

 Total Lines 8 Code Lines 4

#### Duplication

 Lines 0 Ratio 0 %

#### Code Coverage

 Tests 4 CRAP Score 2

#### Importance

 Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
 1 `` 12 ` */` 13 `final class Euro {` 14 15 ` private static \$DECIMAL_COUNT = 2;` 16 ` private static \$CENTS_PER_EURO = 100;` 17 18 ` private \$cents;` 19 20 ` /**` 21 ` * @param int \$cents` 22 ` * @throws InvalidArgumentException` 23 ` */` 24 52 ` private function __construct( int \$cents ) {` 25 52 ` if ( \$cents < 0 ) {` 26 5 ` throw new InvalidArgumentException( 'Amount needs to be positive' );` 27 ` }` 28 29 47 ` \$this->cents = \$cents;` 30 47 ` }` 31 32 ` /**` 33 ` * @param int \$cents` 34 ` * @return self` 35 ` * @throws InvalidArgumentException` 36 ` */` 37 30 ` public static function newFromCents( int \$cents ): self {` 38 30 ` return new self( \$cents );` 39 ` }` 40 41 ` /**` 42 ` * Constructs a Euro object from a string representation such as "13.37".` 43 ` *` 44 ` * This method takes into account the errors that can arise from floating` 45 ` * point number usage. Amounts with too many decimals are rounded to the` 46 ` * nearest whole euro cent amount.` 47 ` *` 48 ` * @param string \$euroAmount` 49 ` * @return self` 50 ` * @throws InvalidArgumentException` 51 ` */` 52 15 ` public static function newFromString( string \$euroAmount ): self {` 53 15 ` if ( !is_numeric( \$euroAmount ) ) {` 54 3 ` throw new InvalidArgumentException( 'Not a number' );` 55 ` }` 56 57 12 ` \$parts = explode( '.', \$euroAmount, 2 );` 58 59 12 ` \$euros = (int)\$parts[0];` 60 12 ` \$cents = self::centsFromString( \$parts[1] ?? '0' );` 61 62 12 ` return new self( \$euros * self::\$CENTS_PER_EURO + \$cents );` 63 ` }` 64 65 12 ` private static function centsFromString( string \$cents ): int {` 66 12 ` if ( strlen( \$cents ) > self::\$DECIMAL_COUNT ) {` 67 2 ` return self::roundCentsToInt( \$cents );` 68 ` }` 69 70 ` // Turn .1 into .10, so it ends up as 10 cents` 71 10 ` return (int)str_pad( \$cents, self::\$DECIMAL_COUNT, '0' );` 72 ` }` 73 74 2 ` private static function roundCentsToInt( string \$cents ): int {` 75 2 ` \$centsInt = (int)substr( \$cents, 0, self::\$DECIMAL_COUNT );` 76 77 2 ` if ( (int)\$cents[self::\$DECIMAL_COUNT] >= 5 ) {` 78 1 ` \$centsInt++;` 79 ` }` 80 81 2 ` return \$centsInt;` 82 ` }` 83 84 ` /**` 85 ` * This method takes into account the errors that can arise from floating` 86 ` * point number usage. Amounts with too many decimals are rounded to the` 87 ` * nearest whole euro cent amount.` 88 ` *` 89 ` * @param float \$euroAmount` 90 ` * @return self` 91 ` * @throws InvalidArgumentException` 92 ` */` 93 6 ` public static function newFromFloat( float \$euroAmount ) {` 94 6 ` return new self( intval(` 95 6 ` round(` 96 6 ` round( \$euroAmount, self::\$DECIMAL_COUNT ) * self::\$CENTS_PER_EURO,` 97 ` 0` 98 ` )` 99 ` ) );` 100 ` }` 101 102 ` /**` 103 ` * @param int \$euroAmount` 104 ` * @return self` 105 ` * @throws InvalidArgumentException` 106 ` */` 107 5 ` public static function newFromInt( int \$euroAmount ) {` 108 5 ` return new self( \$euroAmount * self::\$CENTS_PER_EURO );` 109 ` }` 110 111 32 ` public function getEuroCents(): int {` 112 32 ` return \$this->cents;` 113 ` }` 114 115 10 ` public function getEuroFloat(): float {` 116 10 ` return \$this->cents / self::\$CENTS_PER_EURO;` 117 ` }` 118 119 ` /**` 120 ` * Returns the euro amount as string with two decimals always present in format "42.00".` 121 ` */` 122 6 ` public function getEuroString(): string {` 123 6 ` return number_format( \$this->getEuroFloat(), self::\$DECIMAL_COUNT, '.', '' );` 124 ` }` 125 126 10 ` public function equals( Euro \$euro ): bool {` 127 10 ` return \$this->cents === \$euro->cents;` 128 ` }` 129 130 `}` 131