UnitsManager::distance()   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 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace Arcanedev\Units;
2
3
use Arcanedev\Units\Contracts\UnitsManager as UnitsManagerContract;
4
use Illuminate\Support\Arr;
5
use Illuminate\Support\Manager;
6
use InvalidArgumentException;
7
8
/**
9
 * Class     UnitsManager
10
 *
11
 * @package  Arcanedev\Units
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class UnitsManager extends Manager implements UnitsManagerContract
15
{
16
    /* -----------------------------------------------------------------
17
     |  Main Methods
18
     | -----------------------------------------------------------------
19
     */
20
21
    /**
22
     * Create the distance unit driver.
23
     *
24
     * @return \Arcanedev\Units\Bases\UnitMeasure
25
     */
26 6
    protected function createDistanceDriver()
27
    {
28 6
        return $this->distance();
29
    }
30
31
    /**
32
     * Create the distance unit instance.
33
     *
34
     * @return \Arcanedev\Units\Bases\UnitMeasure
35
     */
36 6
    public function distance()
37
    {
38 6
        return $this->buildUnit('distance', Measures\Distance::class);
39
    }
40
41
    /**
42
     * Create the file size unit driver.
43
     *
44
     * @return \Arcanedev\Units\Bases\UnitMeasure
45
     */
46 3
    protected function createFileSizeDriver()
47
    {
48 3
        return $this->fileSize();
49
    }
50
51
    /**
52
     * Create the file size unit instance.
53
     *
54
     * @return \Arcanedev\Units\Bases\UnitMeasure
55
     */
56 3
    public function fileSize()
57
    {
58 3
        return $this->buildUnit('file-size', Measures\FileSize::class);
59
    }
60
61
    /**
62
     * Create the liquid volume unit driver.
63
     *
64
     * @return \Arcanedev\Units\Bases\UnitMeasure
65
     */
66 3
    protected function createLiquidVolumeDriver()
67
    {
68 3
        return $this->liquidVolume();
69
    }
70
71
    /**
72
     * Create the liquid volume unit instance.
73
     *
74
     * @return \Arcanedev\Units\Bases\UnitMeasure
75
     */
76 3
    public function liquidVolume()
77
    {
78 3
        return $this->buildUnit('liquid-volume', Measures\LiquidVolume::class);
79
    }
80
81
    /**
82
     * Create the weight unit driver.
83
     *
84
     * @return \Arcanedev\Units\Bases\UnitMeasure
85
     */
86 3
    protected function createWeightDriver()
87
    {
88 3
        return $this->weight();
89
    }
90
91
    /**
92
     * Create the weight unit instance.
93
     *
94
     * @return \Arcanedev\Units\Bases\UnitMeasure
95
     */
96 3
    public function weight()
97
    {
98 3
        return $this->buildUnit('weight', Measures\Weight::class);
99
    }
100
101
    /**
102
     * Get the default driver name.
103
     *
104
     * @return string
105
     */
106 3
    public function getDefaultDriver()
107
    {
108 3
        throw new InvalidArgumentException('No unit of measurement was specified.');
109
    }
110
111
    /* -----------------------------------------------------------------
112
     |  Other Methods
113
     | -----------------------------------------------------------------
114
     */
115
116
    /**
117
     * Build the unit of measurement.
118
     *
119
     * @param  string  $key
120
     * @param  string  $unitClass
121
     *
122
     * @return \Arcanedev\Units\Bases\UnitMeasure
123
     */
124 15
    protected function buildUnit($key, $unitClass)
125
    {
126 15
        $configs = $this->app['config']->get("units.$key", []);
127
128 15
        return new $unitClass(
129 15
            0, Arr::get($configs, 'default'), Arr::except($configs, ['default'])
130
        );
131
    }
132
}
133