Weight::pounds()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Jne;
4
5
use Jne\Contracts\Foundation\WeightInterface;
6
7
class Weight implements WeightInterface
8
{
9
    /**
10
     * Grams per kilogram.
11
     */
12
    const GRAMS_PER_KILOGRAM = 1000;
13
14
    /**
15
     * Grams per pound.
16
     */
17
    const GRAMS_PER_POUND = 453.592;
18
19
    /**
20
     * Weight in grams.
21
     *
22
     * @var float
23
     */
24
    protected $grams;
25
26
    /**
27
     * Create a new instance of Weight.
28
     *
29
     * @param float $grams
30
     */
31 10
    protected function __construct($grams)
32
    {
33 10
        $this->grams = $grams;
34 10
    }
35
36
    /**
37
     * Create weight from grams.
38
     *
39
     * @param float $grams
40
     *
41
     * @return \Jne\Contracts\Foudation\WeightInterface
42
     */
43 3
    public static function fromGrams($grams)
44
    {
45 3
        return new static($grams);
46
    }
47
48
    /**
49
     * Create weight from kilograms.
50
     *
51
     * @param float $kilograms
52
     *
53
     * @return \Jne\Contracts\Foudation\WeightInterface
54
     */
55 5
    public static function fromKilograms($kilograms)
56
    {
57 5
        return new static($kilograms * self::GRAMS_PER_KILOGRAM);
58
    }
59
60
    /**
61
     * Create weight from pounds.
62
     *
63
     * @param float $pounds
64
     *
65
     * @return \Jne\Contracts\Foudation\WeightInterface
66
     */
67 2
    public static function fromPounds($pounds)
68
    {
69 2
        return new static($pounds * self::GRAMS_PER_POUND);
70
    }
71
72
    /**
73
     * Get weight in grams.
74
     *
75
     * @return float
76
     */
77 7
    public function grams()
78
    {
79 7
        return $this->grams;
80
    }
81
82
    /**
83
     * Get weight in kilograms.
84
     *
85
     * @return float
86
     */
87 2
    public function kilograms()
88
    {
89 2
        return $this->grams() / self::GRAMS_PER_KILOGRAM;
90
    }
91
92
    /**
93
     * Get weight in pounds.
94
     *
95
     * @return float
96
     */
97 1
    public function pounds()
98
    {
99 1
        return $this->grams() / self::GRAMS_PER_POUND;
100
    }
101
}
102