Passed
Push — master ( c87112...7f9ae9 )
by Joschi
01:54
created

LengthNormalizerService   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 41.94%

Importance

Changes 0
Metric Value
dl 0
loc 81
ccs 13
cts 31
cp 0.4194
rs 10
c 0
b 0
f 0
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
D normalize() 0 35 9
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 3
        $normalized = null;
97
98
        switch ($unit) {
99 3
            case UnitLengthInterface::UNIT_PIXEL:
100 2
                $normalized = $value;
101 2
                break;
102 2
            case UnitLengthInterface::UNIT_REM:
103 2
            case UnitLengthInterface::UNIT_EM:
104 2
                $normalized = $value * $this->emPixel;
105 2
                break;
106
            case UnitLengthInterface::UNIT_CM:
107
                $normalized = round($value * static::DEFAULT_DPI / static::INCH_IN_CM);
108
                break;
109
            case UnitLengthInterface::UNIT_MM:
110
                $normalized = round($value * static::DEFAULT_DPI / (static::INCH_IN_CM * 10));
111
                break;
112
            case UnitLengthInterface::UNIT_IN:
113
                $normalized = $value * static::DEFAULT_DPI;
114
                break;
115
            case UnitLengthInterface::UNIT_PT:
116
                $normalized = round($value / static::PX_IN_PT);
117
                break;
118
            case UnitLengthInterface::UNIT_PC:
119
                $normalized = round($value * 12 / static::PX_IN_PT);
120
                break;
121
            default:
122
                throw new InvalidArgumentException(
123
                    sprintf(InvalidArgumentException::INVALID_UNIT_STR, $unit),
124
                    InvalidArgumentException::INVALID_UNIT
125
                );
126
        }
127
128 3
        return $normalized;
129
    }
130
}