To::__call()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 9
cts 10
cp 0.9
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 2
crap 4.016
1
<?php
2
3
namespace Clarkeash\Converter;
4
5
use Clarkeash\Converter\Contracts\Metric;
6
use Clarkeash\Converter\Exceptions\BadMethodCallException;
7
8
class To
9
{
10
    /**
11
     * @var \Clarkeash\Converter\Convert
12
     */
13
    private $convert;
14
15
    /**
16
     * @var \Clarkeash\Converter\Contracts\Metric
17
     */
18
    private $metric;
19
20 7
    public function __construct(Convert $convert, Metric $metric)
21
    {
22 7
        $this->convert = $convert;
23 7
        $this->metric = $metric;
24 7
    }
25
26 6
    public function __call($method, $args)
27
    {
28 6
        if (in_array($method, ['to', 'in', 'into', 'as'])) {
29 6
            return $this;
30
        }
31
32 6
        if ($method == 'and') {
33 1
            return new Add($this->convert, $this->metric, $args[0]);
34
        }
35
36 6
        if (method_exists($this->metric, $method)) {
37 5
            $rate = call_user_func([$this->metric, $method]);
38
39 5
            return $this->convert->getValue() / $rate;
40
        }
41
42 1
        BadMethodCallException::throw($method, $this->metric, $this);
43
    }
44
}
45