Passed
Push — master ( 56a869...1425a4 )
by Filippo
02:07 queued 15s
created

ChunksHelpers::hasProgressBar()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Jobtech\LaravelChunky\Concerns;
4
5
use Illuminate\Support\Str;
6
use Jobtech\LaravelChunky\Events\ChunkDeleted;
7
use Symfony\Component\Console\Helper\ProgressBar;
8
use Symfony\Component\Console\Style\OutputStyle;
9
10
trait ChunksHelpers
11
{
12
    /**
13
     * This method checks if the application is running in console and, if output style is not null, it creates a new
14
     * progress bar instance, otherwise returns null.
15
     *
16
     * @param \Symfony\Component\Console\Style\OutputStyle|null $output
17
     * @param int $count
18
     *
19
     * @return \Symfony\Component\Console\Helper\ProgressBar|null
20
     */
21
    public function hasProgressBar(?OutputStyle $output, int $count): ?ProgressBar
22
    {
23
        if ($output !== null && app()->runningInConsole()) {
0 ignored issues
show
introduced by
The method runningInConsole() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
        if ($output !== null && app()->/** @scrutinizer ignore-call */ runningInConsole()) {
Loading history...
24
            return $output->createProgressBar($count);
25
        }
26
27
        return null;
28
    }
29
30
    /**
31
     * Check if the given chunks folder exists.
32
     *
33
     * @param string $folder
34
     *
35
     * @return bool
36
     */
37
    public function chunksFolderExists($folder = ''): bool
38
    {
39
        if (! Str::startsWith($folder, $this->getChunksFolder())) {
0 ignored issues
show
Bug introduced by
It seems like getChunksFolder() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
        if (! Str::startsWith($folder, $this->/** @scrutinizer ignore-call */ getChunksFolder())) {
Loading history...
40
            $folder = $this->getChunksFolder().$folder;
41
        }
42
43
        return $this->chunksFilesystem()
0 ignored issues
show
Bug introduced by
It seems like chunksFilesystem() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        return $this->/** @scrutinizer ignore-call */ chunksFilesystem()
Loading history...
44
            ->exists($folder);
45
    }
46
47
    /**
48
     * Delete all chunks folders and their content.
49
     *
50
     * @param null $output
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $output is correct as it would always require null to be passed?
Loading history...
51
     * @return bool
52
     */
53
    public function deleteAllChunks($output = null): bool
54
    {
55
        $folders = $this->chunksFilesystem()
56
            ->directories(
57
                $this->getChunksFolder()
58
            );
59
60
        $progress_bar = $this->hasProgressBar($output, count($folders));
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $progress_bar is correct as $this->hasProgressBar($output, count($folders)) targeting Jobtech\LaravelChunky\Co...lpers::hasProgressBar() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
61
62
        foreach ($folders as $folder) {
63
            if (! $this->deleteChunks($folder)) {
64
                return false;
65
            }
66
67
            if ($progress_bar !== null) {
68
                $progress_bar->advance();
69
            }
70
        }
71
72
        return true;
73
    }
74
75
    /**
76
     * Delete all chunks and, once empty, delete the folder.
77
     *
78
     * @param string $folder
79
     *
80
     * @return bool
81
     */
82
    public function deleteChunks(string $folder): bool
83
    {
84
        if (! $this->chunksFolderExists($folder)) {
85
            return false;
86
        }
87
88
        $files = $this->chunks($folder);
0 ignored issues
show
Bug introduced by
The method chunks() does not exist on Jobtech\LaravelChunky\Concerns\ChunksHelpers. Did you maybe mean chunksFolderExists()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
        /** @scrutinizer ignore-call */ 
89
        $files = $this->chunks($folder);

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...
89
90
        foreach ($files as $chunk) {
91
            $deleted = $this->chunksFilesystem()
92
                ->delete($chunk->getPath());
93
94
            if (! $deleted) {
95
                return false;
96
            }
97
98
            event(new ChunkDeleted($chunk));
99
        }
100
101
        return $this->chunksFilesystem()
102
            ->deleteDirectory($folder);
103
    }
104
}
105