Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
40 / 40
Timer
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
4 / 4
14
100.00% covered (success)
100.00%
40 / 40
 createCollection
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 startTimer
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
7 / 7
 getTimer
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
7 / 7
 secondsToHumanFromStart
100.00% covered (success)
100.00%
1 / 1
7
100.00% covered (success)
100.00%
24 / 24
1<?php
2
3namespace Qmp\Laravel\ToolsLaravel\Traits;
4
5use DateTime;
6
7trait Timer
8{
9    /**
10     * The timer collection
11     *
12     * @var \Illuminate\Support\Collection
13     */
14    protected $timerCollection;
15
16    /**
17     * Create the timer collection
18     *
19     * @return void
20     */
21    protected function createCollection(): void
22    {
23        $this->timerCollection = collect();
24    }
25
26    /**
27     * Start a new timer
28     *
29     * @param string ...$name
30     * @return void
31     */
32    protected function startTimer(...$name): void
33    {
34        if (!$this->timerCollection) {
35            $this->createCollection();
36        }
37
38        $now = microtime(true);
39
40        collect($name)->each(function ($name) use ($now) {
41            $this->timerCollection->put($name, $now);
42        });
43    }
44    /**
45     * Return timer to string
46     *
47     * @param string $name
48     * @param boolean $reset
49     * @return string
50     */
51    protected function getTimer($name, $reset = false): string
52    {
53        if($this->timerCollection) {
54            if ($start = $this->timerCollection->get($name)) {
55
56                if ($reset) {
57                    $this->startTimer($name);
58                }
59    
60                return  $this->secondsToHumanFromStart($start);
61            }
62            return "";
63        }
64        return "";
65    }
66
67    /**
68     * Return seconds to well formated string
69     *
70     * @param string seconds
71     * @return string
72     */
73    protected function secondsToHumanFromStart($start): string
74    {
75        $formats = ['hour' => 'h', 'minute' => 'i', 'second' => ['s', 'f']];
76
77        $dtF = DateTime::createFromFormat('U.u', sprintf("%.4f", microtime(true)));
78        $dtT = DateTime::createFromFormat('U.u', sprintf("%.4f", $start));
79        $diff = $dtF->diff($dtT);
80        $index = -1;
81
82        $humanReadableString = collect($formats)
83            ->map(function ($format, $key) use ($diff) {
84                $format = is_array($format) ? $format : [$format];
85                $reduced = array_reduce($format, function ($carry, $prop) use ($diff) {
86                    $duration = $diff->$prop !== 0 ? round($diff->$prop, 2) : 0;
87                    return $carry + $duration;
88                }, 0);
89                $pluralKey = $reduced > 1 ? $key . 's' : $key;
90                return ['name' =>  $pluralKey, 'duration' => $reduced];
91            })
92            ->values()
93            ->map(function ($val, $key) use ($index) {
94                if ($val['duration'] !== 0 || $index === $key) {
95                    $index = $key + 1;
96                    $baseLen = strlen(explode('.', $val['duration'])[0]);
97                    $padLen = $baseLen === 1 ? strlen($val['duration']) + 1 : 0;
98                    return str_pad($val['duration'], $padLen, '0', STR_PAD_LEFT) . ' ' . $val['name'];
99                }
100            });
101
102        return $humanReadableString->filter()->implode(' ');
103    }
104}