Passed
Push — master ( 745178...3deda8 )
by Alexey
03:44
created

HelperTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A formatSpentTime() 0 3 1
A convertDateTime() 0 3 1
A formatSizeUnits() 0 11 2
A getArrayObjectKeys() 0 11 3
1
<?php
2
/**
3
 * This file is part of the wow-apps/symfony-proxybonanza project
4
 * https://github.com/wow-apps/symfony-proxybonanza
5
 *
6
 * (c) 2016 WoW-Apps
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WowApps\ProxybonanzaBundle\Traits;
13
14
/**
15
 * Trait HelperTrait
16
 * @author Alexey Samara <[email protected]>
17
 * @package wow-apps/symfony-proxybonanza
18
 */
19
trait HelperTrait
20
{
21
    /**
22
     * @param string $date
23
     * @return \DateTime
24
     * @throws \InvalidArgumentException
25
     */
26
    public function convertDateTime(string $date): \DateTime
27
    {
28
        return new \DateTime($date);
29
    }
30
31
    /**
32
     * @param int $bytes
33
     * @param int $precision
34
     * @return string
35
     */
36
    public function formatSizeUnits(int $bytes, int $precision = 2): string
37
    {
38
        $units = ['B', 'Kb', 'Mb', 'Gb', 'Tb'];
39
40
        $bytes = max($bytes, 0);
41
        $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
42
        $pow = min($pow, count($units) - 1);
43
44
        $bytes /= pow(1024, $pow);
45
46
        return round($bytes, $precision) . $units[$pow];
47
    }
48
49
    /**
50
     * @param \ArrayObject $arrayObject
51
     * @return array
52
     */
53
    public function getArrayObjectKeys(\ArrayObject $arrayObject): array
54
    {
55
        $keys = [];
56
57
        if (!empty($arrayObject)) {
58
            foreach ($arrayObject as $key => $value) {
59
                $keys[] = $key;
60
            }
61
        }
62
63
        return $keys;
64
    }
65
66
    /**
67
     * @param float $timeStart
68
     * @return float
69
     */
70
    public function formatSpentTime(float $timeStart): float
71
    {
72
        return round(microtime(true) - $timeStart, 2);
73
    }
74
}
75