Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.48% covered (warning)
81.48%
44 / 54
75.00% covered (warning)
75.00%
6 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Index
81.48% covered (warning)
81.48%
44 / 54
75.00% covered (warning)
75.00%
6 / 8
22.54
0.00% covered (danger)
0.00%
0 / 1
 run
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getDescription
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOptions
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 listCommands
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
4
 showHeader
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 showDate
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 greet
80.00% covered (warning)
80.00%
8 / 10
0.00% covered (danger)
0.00%
0 / 1
5.20
 getUser
27.27% covered (danger)
27.27%
3 / 11
0.00% covered (danger)
0.00%
0 / 1
14.62
1<?php declare(strict_types=1);
2/*
3 * This file is part of Aplus Framework CLI Library.
4 *
5 * (c) Natan Felles <natanfelles@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10namespace Framework\CLI\Commands;
11
12use Framework\CLI\CLI;
13use Framework\CLI\Command;
14
15/**
16 * Class Index.
17 *
18 * @package cli
19 */
20class Index extends Command
21{
22    protected string $name = 'index';
23    protected string $description = 'Show commands list';
24    protected string $usage = 'index';
25    protected array $options = [
26        '-g' => 'Shows greeting.',
27    ];
28
29    public function run() : void
30    {
31        $this->showHeader();
32        $this->showDate();
33        if ($this->console->getOption('g')) {
34            $this->greet();
35        }
36        $this->listCommands();
37    }
38
39    public function getDescription() : string
40    {
41        return $this->console->getLanguage()->render('cli', 'index.description');
42    }
43
44    public function getOptions() : array
45    {
46        return [
47            '-g' => $this->console->getLanguage()->render('cli', 'index.option.greet'),
48        ];
49    }
50
51    protected function listCommands() : void
52    {
53        $width = 0;
54        $lengths = [];
55        foreach (\array_keys($this->console->getCommands()) as $name) {
56            $lengths[$name] = \mb_strlen($name);
57            if ($lengths[$name] > $width) {
58                $width = $lengths[$name];
59            }
60        }
61        CLI::write(
62            $this->console->getLanguage()->render('cli', 'commands') . ':',
63            CLI::FG_YELLOW
64        );
65        foreach ($this->console->getCommands() as $name => $command) {
66            CLI::write(
67                '  ' . CLI::style($name, CLI::FG_GREEN) . '  '
68                . \str_repeat(' ', $width - $lengths[$name])
69                . $command->getDescription()
70            );
71        }
72    }
73
74    protected function showHeader() : void
75    {
76        $text = <<<'EOL'
77                _          _              ____ _     ___
78               / \   _ __ | |_   _ ___   / ___| |   |_ _|
79              / _ \ | '_ \| | | | / __| | |   | |    | |
80             / ___ \| |_) | | |_| \__ \ | |___| |___ | |
81            /_/   \_\ .__/|_|\__,_|___/  \____|_____|___|
82                    |_|
83
84            EOL;
85        CLI::write($text, CLI::FG_GREEN);
86    }
87
88    protected function showDate() : void
89    {
90        $text = $this->console->getLanguage()->date(\time(), 'full');
91        $text = \ucfirst($text) . ' - '
92            . \date('H:i:s') . ' - '
93            . \date_default_timezone_get() . \PHP_EOL;
94        CLI::write($text);
95    }
96
97    protected function greet() : void
98    {
99        $hour = \date('H');
100        $timing = 'evening';
101        if ($hour > 4 && $hour < 12) {
102            $timing = 'morning';
103        } elseif ($hour > 4 && $hour < 18) {
104            $timing = 'afternoon';
105        }
106        $greeting = $this->console->getLanguage()
107            ->render('cli', 'greet.' . $timing, [$this->getUser()]);
108        CLI::write($greeting);
109        CLI::newLine();
110    }
111
112    protected function getUser() : string
113    {
114        $username = \posix_getlogin();
115        if ($username === false) {
116            return $this->console->getLanguage()->render('cli', 'friend');
117        }
118        $info = \posix_getpwnam($username);
119        if ( ! $info) {
120            return $username;
121        }
122        $gecos = $info['gecos'] ?? '';
123        if ( ! $gecos) {
124            return $username;
125        }
126        $length = \strpos($gecos, ',') ?: \strlen($gecos);
127        return \substr($gecos, 0, $length);
128    }
129}