Completed
Push — master ( ae45c2...c87112 )
by Joschi
03:28
created

LengthNormalizerService::normalize()   C

Complexity

Conditions 9
Paths 9

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 34.9438

Importance

Changes 0
Metric Value
cc 9
eloc 19
nc 9
nop 2
dl 0
loc 23
rs 5.8541
c 0
b 0
f 0
ccs 6
cts 19
cp 0.3158
crap 34.9438
1
<?php
2
3
/**
4
 * responsive-images-css
5
 *
6
 * @category   Jkphl
7
 * @package    Jkphl\Respimgcss
8
 * @subpackage Jkphl\Respimgcss\Application\Service
9
 * @author     Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright  Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license    http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Jkphl\Respimgcss\Application\Service;
38
39
use Jkphl\Respimgcss\Application\Contract\UnitLengthInterface;
40
use Jkphl\Respimgcss\Application\Exceptions\InvalidArgumentException;
41
42
/**
43
 * Length normalizer
44
 *
45
 * @package    Jkphl\Respimgcss
46
 * @subpackage Jkphl\Respimgcss\Application
47
 */
48
class LengthNormalizerService
49
{
50
    /**
51
     * EM to pixel ratio
52
     *
53
     * @var int
54
     */
55
    protected $emPixel;
56
    /**
57
     * Inch to Centimeter ratio
58
     *
59
     * @var float
60
     */
61
    CONST INCH_IN_CM = 2.54;
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected const, but found CONST.
Loading history...
62
    /**
63
     * Default display density
64
     *
65
     * @var int
66
     */
67
    CONST DEFAULT_DPI = 72;
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected const, but found CONST.
Loading history...
68
    /**
69
     * Pixel to point ratio
70
     *
71
     * @var float
72
     */
73
    CONST PX_IN_PT = .75;
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected const, but found CONST.
Loading history...
74
75
    /**
76
     * Length normalizer constructor
77
     *
78
     * @param int $emPixel EM to pixel ratio
79
     */
80 3
    public function __construct(int $emPixel)
81
    {
82 3
        $this->emPixel = $emPixel;
83 3
    }
84
85
    /**
86
     * Normalize and return a value with unit
87
     *
88
     * @param float $value Value
89
     * @param string $unit Unit
90
     *
91
     * @return float Normalized value
92
     * @throws InvalidArgumentException If the unit is invalid
93
     */
94 3
    public function normalize(float $value, string $unit)
95
    {
96
        switch ($unit) {
97 3
            case UnitLengthInterface::UNIT_PIXEL:
98 2
                return $value;
99 2
            case UnitLengthInterface::UNIT_REM:
100 2
            case UnitLengthInterface::UNIT_EM:
101 2
                return $value * $this->emPixel;
102
            case UnitLengthInterface::UNIT_CM:
103
                return round($value * static::DEFAULT_DPI / static::INCH_IN_CM);
104
            case UnitLengthInterface::UNIT_MM:
105
                return round($value * static::DEFAULT_DPI / (static::INCH_IN_CM * 10));
106
            case UnitLengthInterface::UNIT_IN:
107
                return $value * static::DEFAULT_DPI;
108
            case UnitLengthInterface::UNIT_PT:
109
                return round($value / static::PX_IN_PT);
110
            case UnitLengthInterface::UNIT_PC:
111
                return round($value * 12 / static::PX_IN_PT);
112
        }
113
114
        throw new InvalidArgumentException(
115
            sprintf(InvalidArgumentException::INVALID_UNIT_STR, $unit),
116
            InvalidArgumentException::INVALID_UNIT
117
        );
118
    }
119
}