Tail   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
dl 0
loc 117
ccs 38
cts 40
cp 0.95
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 22 5
A readLine() 0 23 4
A getArguments() 0 6 1
A getOptions() 0 6 1
1
<?php
2
3
namespace Recca0120\Terminal\Console\Commands;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Support\Collection;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputOption;
9
10
class Tail extends Command
11
{
12
    /**
13
     * The console command name.
14
     *
15
     * @var string
16
     */
17
    protected $name = 'tail';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'tail command';
25
26
    /**
27
     * $files.
28
     *
29
     * @var Filesystem
30
     */
31
    protected $files;
32
33
    /**
34
     * __construct.
35
     *
36
     * @param Filesystem $files
37
     */
38 2
    public function __construct(Filesystem $files)
39
    {
40 2
        parent::__construct();
41
42 2
        $this->files = $files;
43 2
    }
44
45
    /**
46
     * Handle the command.
47
     *
48
     * @throws \InvalidArgumentException
49
     */
50 2
    public function handle()
51
    {
52 2
        $path = $this->argument('path');
53 2
        $lines = (int) $this->option('lines');
54
55 2
        if (empty($path) === false) {
56 1
            $root = function_exists('base_path') === true ? base_path() : getcwd();
57 1
            $file = rtrim($root, '/').'/'.$path;
58
        } else {
59 1
            $path = function_exists('storage_path') === true ? storage_path() : getcwd();
60 1
            $path = rtrim($path, '/').'/';
61
62 1
            $file = (new Collection($this->files->glob($path.'logs/*.log')))
63 1
                ->map(function ($file) {
64 1
                    return is_file($file) === true ? $file : false;
65 1
                })->sortByDesc(function ($file) {
66 1
                    return filectime($file);
67 1
                })->first();
68
        }
69
70 2
        $this->readLine($file, $lines);
71 2
    }
72
73
    /**
74
     * readLine.
75
     *
76
     * @param string $file
77
     * @param int $lines
78
     */
79
    protected function readLine($file, $lines = 50)
80 2
    {
81
        if (is_file($file) === false) {
82 2
            $this->error('tail: cannot open ‘'.$file.'’ for reading: No such file or directory');
83
84
            return;
85
        }
86
87
        $fp = fopen($file, 'rb');
88 2
        $i = 1;
89 2
        $result = [];
90 2
        while (! feof($fp)) {
91 2
            if ($i > $lines) {
92 2
                break;
93 2
            }
94
            $content = fgets($fp);
95 2
            $result[] = $content;
96 2
            $i++;
97 2
        }
98
        fclose($fp);
99 2
100
        $this->line(implode('', $result));
101 2
    }
102 2
103
    /**
104
     * Get the console command arguments.
105
     *
106
     * @return array
107
     */
108
    protected function getArguments()
109 2
    {
110
        return [
111
            ['path', InputArgument::OPTIONAL, 'path'],
112 2
        ];
113
    }
114
115
    /**
116
     * Get the console command options.
117
     *
118
     * @return array
119
     */
120
    protected function getOptions()
121 2
    {
122
        return [
123
            ['lines', null, InputOption::VALUE_OPTIONAL, 'output the last K lines, instead of the last 50', 50],
124 2
        ];
125
    }
126
}
127