Mapping::getUnitClass()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 13
cts 13
cp 1
rs 9.552
c 0
b 0
f 0
cc 4
nc 4
nop 0
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecocode\SyliusBasePricePlugin\Entity;
6
7
use InvalidArgumentException;
8
use UnitConverter\Unit\UnitInterface;
9
10
/**
11
 * Class Mapping
12
 * @package Ecocode\SyliusBasePricePlugin\Entity
13
 */
14
class Mapping
15
{
16
    /** @var float */
17
    private $mod;
18
19
    /** @var float */
20
    private $ifMoreThan;
21
22
    /** @var string */
23
    private $unit;
24
25
    /** @var UnitInterface|null */
26
    private $unitClass = null;
27
28
    /**
29
     * Mapping constructor.
30
     *
31
     * @param array<string,float|string> $data
32
     */
33 17
    public function __construct(array $data)
34
    {
35 17
        $this->mod        = (float)$data['mod'];
36 17
        $this->unit       = (string)$data['unit'];
37 17
        $this->ifMoreThan = (float)$data['ifMoreThan'];
38 17
    }
39
40
    /**
41
     * @return float
42
     */
43 12
    public function getMod(): float
44
    {
45 12
        return $this->mod;
46
    }
47
48
    /**
49
     * @param float $mod
50
     *
51
     * @return $this
52
     */
53 1
    public function setMod(float $mod): self
54
    {
55 1
        $this->mod = $mod;
56
57 1
        return $this;
58
    }
59
60
    /**
61
     * @return float
62
     */
63 14
    public function getIfMoreThan(): float
64
    {
65 14
        return $this->ifMoreThan;
66
    }
67
68
    /**
69
     * @param float $ifMoreThan
70
     *
71
     * @return $this
72
     */
73 1
    public function setIfMoreThan(float $ifMoreThan): self
74
    {
75 1
        $this->ifMoreThan = $ifMoreThan;
76
77 1
        return $this;
78
    }
79
80
    /**
81
     * @return string
82
     */
83 2
    public function getUnit(): string
84
    {
85 2
        return $this->unit;
86
    }
87
88
    /**
89
     * @return UnitInterface
90
     */
91 15
    public function getUnitClass(): UnitInterface
92
    {
93 15
        if ($this->unitClass === null) {
94 15
            if (!class_exists($this->unit)) {
95 1
                $format = 'Unit class %s do not exist';
96 1
                $message = sprintf($format, $this->unit);
97 1
                throw new InvalidArgumentException($message);
98
            }
99
100
            /** @psalm-suppress MixedMethodCall */
101 14
            $unitObject = new $this->unit;
102
103 14
            if (!$unitObject instanceof UnitInterface) {
104 1
                $format = 'Unit class %s have no implemented UnitInterface';
105 1
                $message = sprintf($format, $this->unit);
106 1
                throw new InvalidArgumentException($message);
107
            }
108
109 13
            $this->unitClass = $unitObject;
110
        }
111
112 13
        return $this->unitClass;
113
    }
114
115
    /**
116
     * @param string $unit
117
     *
118
     * @return $this
119
     */
120 1
    public function setUnit(string $unit): self
121
    {
122 1
        $this->unit = $unit;
123
124 1
        return $this;
125
    }
126
}
127