Completed
Push — master ( 5ffc4a...ec3284 )
by recca
01:20
created

Tail   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 94.87%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
dl 0
loc 118
ccs 37
cts 39
cp 0.9487
rs 10
c 2
b 1
f 1
wmc 12
lcom 1
cbo 3

5 Methods

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