Passed
Pull Request — master (#18)
by Cesar
10:54
created

RotateFile::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 3
eloc 11
c 4
b 1
f 0
nc 3
nop 0
dl 0
loc 16
rs 9.9
1
<?php
2
3
namespace Cesargb\LaravelLog\Commands;
4
5
use Cesargb\LaravelLog\Rotate;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Cesargb\LaravelLog\Commands\Rotate. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use Illuminate\Console\Command;
7
8
class RotateFile extends Command
9
{
10
    protected $signature = 'rotate:files
11
                                        {--f|file=* : Files to rotate}
12
                                        {--c|compress=true : Compress the file rotated}
13
                                        {--m|max-files=5 : Max files rotated}
14
                                        {--d|dir= : Dir where archive the file rotated}';
15
16
    protected $description = 'Rotate files';
17
18
    public function handle()
19
    {
20
        foreach ($this->option('file') as $filename) {
21
            $this->line('Rotate file '.basename($filename).': ');
22
23
            $rotate = new Rotate();
24
25
            $rotate->file($filename, [
26
                'files' => config('rotate.log_max_files', 366),
27
                'compress' => config('rotate.log_compress_files', true),
28
                // 'then' => function () {
29
                //     $this->line('<info>ok</>');
30
                // },
31
                // 'catch' => function ($error) {
32
                //     $this->error('<comment>failed: '.$error->getMessage().'</>');
33
                // },
34
            ]);
35
        }
36
    }
37
}
38