Completed
Push — master ( 79372b...1f082f )
by recca
02:25
created

Tail::getArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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