Completed
Push — master ( 71c78b...6eeaca )
by recca
10:24 queued 05:45
created

Tail::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
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
     * $filesystem.
29
     *
30
     * @var \Illuminate\Filesystem\Filesystem
31
     */
32
    protected $filesystem;
33
34
    /**
35
     * __construct.
36
     *
37
     * @param \Illuminate\Filesystem\Filesystem $filesystem
38
     */
39 2
    public function __construct(Filesystem $filesystem)
40
    {
41 2
        parent::__construct();
42 2
        $this->filesystem = $filesystem;
43 2
    }
44
45
    /**
46
     * fire.
47
     */
48 2
    public function fire()
49
    {
50 2
        $path = $this->argument('path');
51 2
        $lines = $this->option('lines');
52
53 2
        if (empty($path) === false) {
54 1
            $root = is_null($this->getLaravel()) === false ?
55 1
                $this->getLaravel()->basePath() : getcwd();
56 1
            $file = rtrim($root, '/').'/'.$path;
57 1
        } else {
58 1
            $path = is_null($this->getLaravel()) === false ?
59 1
                $this->getLaravel()->storagePath() : getcwd();
1 ignored issue
show
Bug introduced by
The method storagePath() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60 1
            $path = rtrim($path, '/').'/';
61
62 1
            $file = (new Collection($this->filesystem->glob($path.'logs/*.log')))
63
                ->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);
0 ignored issues
show
Documentation introduced by
$lines is of type string|array, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71 2
    }
72
73
    /**
74
     * readLine.
75
     *
76
     * @param string $file
77
     * @param int    $lines
78
     * @return string
79
     */
80 2
    protected function readLine($file, $lines = 50)
81
    {
82 2
        if (is_file($file) === false) {
83
            $this->error('tail: cannot open ‘'.$file.'’ for reading: No such file or directory');
84
85
            return;
86
        }
87
88 2
        $fp = fopen($file, 'r');
89 2
        $i = 1;
90 2
        $result = [];
91 2
        while (! feof($fp)) {
92 2
            if ($i > $lines) {
93 2
                break;
94
            }
95 2
            $content = fgets($fp);
96 2
            $result[] = $content;
97 2
            ++$i;
98 2
        }
99 2
        fclose($fp);
100
101 2
        $this->line(implode('', $result));
102 2
    }
103
104
    /**
105
     * Get the console command arguments.
106
     *
107
     * @return array
108
     */
109 2
    protected function getArguments()
110
    {
111
        return [
112 2
            ['path', InputArgument::OPTIONAL, 'path'],
113 2
        ];
114
    }
115
116
    /**
117
     * Get the console command options.
118
     *
119
     * @return array
120
     */
121 2
    protected function getOptions()
122
    {
123
        return [
124 2
            ['lines', null, InputOption::VALUE_OPTIONAL, 'output the last K lines, instead of the last 50', 50],
125 2
        ];
126
    }
127
}
128