Cancelled
Push — master ( 706ba7...b281b4 )
by Paul
02:38
created

Module::hasEntries()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace GeminiLabs\BlackBar\Modules;
4
5
use GeminiLabs\BlackBar\Application;
6
7
abstract class Module
8
{
9
    /**
10
     * @var Application
11
     */
12
    protected $app;
13
    /**
14
     * @var array
15
     */
16
    protected $entries;
17
18
    public function __construct(Application $app)
19
    {
20
        $this->app = $app;
21
        $this->entries = [];
22
    }
23
24
    public function classes(): string
25
    {
26
        return $this->id();
27
    }
28
29
    abstract public function entries(): array;
30
31
    public function hasEntries(): bool
32
    {
33
        return !empty($this->entries);
34
    }
35
36
    public function id(): string
37
    {
38
        return sprintf('glbb-%s', $this->slug());
39
    }
40
41
    public function info(): string
42
    {
43
        return '';
44
    }
45
46
    public function isVisible(): bool
47
    {
48
        return true;
49
    }
50
51
    abstract public function label(): string;
52
53
    public function render(): void
54
    {
55
        $this->app->render('panels/'.$this->slug(), ['module' => $this]);
56
    }
57
58
    public function slug(): string
59
    {
60
        return strtolower((new \ReflectionClass($this))->getShortName());
61
    }
62
63
    protected function formatTime(int $nanoseconds): string
64
    {
65
        if ($nanoseconds >= 1e9) {
66
            return sprintf('%s s', $this->toDecimal(round($nanoseconds / 1e9, 2)));
67
        }
68
        if ($nanoseconds >= 1e6) {
69
            return sprintf('%s ms', $this->toDecimal(round($nanoseconds / 1e6, 2)));
70
        }
71
        if ($nanoseconds >= 1e3) {
72
            return sprintf('%s µs', round($nanoseconds / 1e3));
73
        }
74
        return sprintf('%s ns', $nanoseconds);
75
    }
76
77
    protected function toDecimal(float $number): string
78
    {
79
        $number = (string) $number;
80
        if (false !== strpos($number, '.')) {
81
            $number = rtrim(rtrim($number, '0'), '.');
82
        }
83
        return $number;
84
    }
85
}
86