Completed
Push — master ( 72a657...653b9d )
by Andrii
02:10
created

AbstractUnit::getCalculator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * PHP Units of Measure Library
4
 *
5
 * @link      https://github.com/hiqdev/php-units
6
 * @package   php-units
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\units;
12
13
use hiqdev\php\units\exceptions\InvalidConfigException;
14
use hiqdev\php\units\tree\TreeConverter;
15
16
/**
17
 * Abstract Unit of measure. E.g.
18
 * Almost final.
19
 * Only findCoverter can be changed.
20
 *
21
 * @see UnitInterface
22
 * @author Andrii Vasyliev <[email protected]>
23
 */
24
abstract class AbstractUnit implements UnitInterface
25
{
26
    /**
27
     * @var string
28
     */
29
    private $name;
30
31
    /**
32
     * @var ConverterInterface
33
     */
34
    private $converter;
35 3
36
    private function __construct($name, ConverterInterface $converter)
37 3
    {
38 3
        $this->name = $name;
39 3
        $this->converter = $converter;
40
    }
41 8
42
    private function getConverter()
43 8
    {
44
        return $this->converter;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49 8
     */
50
    final public function getName()
51 8
    {
52
        return $this->name;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57 3
     */
58
    final public function getMeasure()
59 3
    {
60
        return $this->getConverter()->getMeasure($this);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65 2
     */
66
    final public function getCalculator()
67 2
    {
68
        return $this->getConverter()->getCalculator($this);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73 1
     */
74
    final public function equals(UnitInterface $other)
75 1
    {
76
        return $this->getConverter()->equals($this, $other);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81 2
     */
82
    final public function isConvertible(UnitInterface $other)
83 2
    {
84
        return $this->getConverter()->isConvertible($this, $other);
85
    }
86
87
    /**
88
     * {@inheritdoc}
89 4
     */
90
    final public function convert(UnitInterface $other, $quantity)
91 4
    {
92
        return $this->getConverter()->convert($this, $other, $quantity);
93
    }
94
95
    /**
96
     * @var Unit[]
97
     */
98
    protected static $units;
99 11
100
    final public static function __callStatic($name, $args)
101 11
    {
102 3
        if (!isset(static::$units[$name])) {
103
            static::$units[$name] = new static($name, static::findConverter($name));
104
        }
105 11
106
        return static::$units[$name];
107
    }
108
109
    /**
110
     * Returns converter for given unit name.
111
     * The only function to change in child classes.
112
     * XXX Should be defined as abstract but not supported in PHP5.
113
     * @param string $name
114
     * @return ConverterInterface
115
     * @throws InvalidConfigException
116
     */
117
    protected static function findConverter($name)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
118
    {
119
        throw new InvalidConfigException('findConverter method must be redefined');
120
    }
121
}
122