Completed
Pull Request — master (#21)
by Michal
02:30
created

Formatter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace UniMan\Core\Helper;
4
5
use Nette\Localization\ITranslator;
6
7
class Formatter
8
{
9
    private $translator;
10
11 6
    public function __construct(ITranslator $translator)
12
    {
13 6
        $this->translator = $translator;
14 6
    }
15
16 4
    public function formatNumber($number, $decimals = 0)
17
    {
18 4
        return number_format($number, $decimals, $this->translator->translate('core.formatter.number_format.decimal_point'), $this->translator->translate('core.formatter.number_format.thousands_separator'));
19
    }
20
21 2
    public function formatSize($number)
22
    {
23 2
        if ($number < 1024) {
24 2
            return $this->formatNumber($number) . ' B';
25
        }
26 2 View Code Duplication
        if ($number < pow(1024, 2)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27 2
            return $this->formatNumber($number / 1024, 1) . ' kB';
28
        }
29 2 View Code Duplication
        if ($number < pow(1024, 3)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30 2
            return $this->formatNumber($number / pow(1024, 2), 1) . ' MB';
31
        }
32 2 View Code Duplication
        if ($number < pow(1024, 4)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33 2
            return $this->formatNumber($number / pow(1024, 3), 1) . ' GB';
34
        }
35 2
        return $this->formatNumber($number / pow(1024, 4), 1) . ' TB';
36
    }
37
38 2
    public function formatTime($number)
39
    {
40 2
        if ($number < 60) {
41 2
            return $number . ' s';
42
        }
43 2
        if ($number < 3600) {
44 2
            return date('i:s', $number);
45
        }
46 2
        return floor($number / 3600) . date('\h i\m s\s', $number);
47
    }
48
}
49