GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( c10aeb...1c7f7f )
by Sebastian
07:20
created

Exporter::exportPaths()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Spatie\Export;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Contracts\Filesystem\Filesystem;
7
use App\Http\Kernel;
8
use Illuminate\Http\Request;
9
use RecursiveDirectoryIterator;
10
use RecursiveIteratorIterator;
11
use Spatie\Export\Concerns\Messenger;
12
13
class Exporter
14
{
15
    use Messenger;
16
17
    /** @var \App\Http\Kernel */
18
    protected $kernel;
19
20
    /** @var \Illuminate\Contracts\Filesystem\Filesystem */
21
    protected $filesystem;
22
23
    /** @var string[] */
24
    protected $paths = [];
25
26
    /** @var string[] */
27
    protected $include = [];
28
29
    /** @var string[] */
30
    protected $exclude = [];
31
32
    /** @var \Illuminate\Console\Command */
33
    protected $cli;
34
35
    public function __construct(Kernel $kernel, Filesystem $filesystem)
36
    {
37
        $this->kernel = $kernel;
38
        $this->filesystem = $filesystem;
39
    }
40
41
    public function paths(array $paths): Exporter
42
    {
43
        $this->paths = array_merge($this->paths, $paths);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->paths, $paths) of type array is incompatible with the declared type array<integer,string> of property $paths.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
44
45
        return $this;
46
    }
47
48
    public function include(array $include): Exporter
49
    {
50
        $this->include = array_merge($this->include, $include);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->include, $include) of type array is incompatible with the declared type array<integer,string> of property $include.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
51
52
        return $this;
53
    }
54
55
    public function exclude(array $exclude): Exporter
56
    {
57
        $this->exclude = array_merge($this->exclude, $exclude);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->exclude, $exclude) of type array is incompatible with the declared type array<integer,string> of property $exclude.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
58
59
        return $this;
60
    }
61
62
    public function export(): void
63
    {
64
        $this->exportPaths();
65
66
        $this->exportIncludedFiles();
67
    }
68
69
    protected function exportPaths(): void
70
    {
71
        $this->cli->comment("Exporting paths...");
72
73
        $progressBar = $this->cli->getOutput()->createProgressBar(count($this->paths));
74
75
        $progressBar->start();
76
77
        foreach ($this->paths as $path) {
78
            $response = $this->kernel->handle(
79
                Request::create($path, 'GET')
80
            );
81
82
            $targetPath = '/'.ltrim($path . '/index.html', '/');
83
84
            $progressBar->advance();
85
86
            $contents = str_replace('http://localhost/', '/', $response->content());
87
            $contents = str_replace('http://localhost', '/', $contents);
88
89
            $this->filesystem->put($targetPath, $contents);
90
        }
91
92
        $progressBar->finish();
93
    }
94
95
    protected function exportIncludedFiles(): void
96
    {
97
        $this->cli->comment("\nExporting files...");
98
99
        foreach ($this->include as ['source' => $source, 'target' => $target]) {
100
            if (is_file($source)) {
101
                $this->exportIncludedFile($source, $target);
0 ignored issues
show
Bug introduced by
The variable $source does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $target does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
102
            } else {
103
                $this->exportIncludedDirectory($source, $target);
104
            }
105
        }
106
    }
107
108
    protected function exportIncludedFile(string $source, string $target): void
109
    {
110
        if ($this->excludes($source)) {
111
            return;
112
        }
113
114
        $target = '/'.ltrim($target, '/');
115
116
        $this->filesystem->put($target, file_get_contents($source));
117
    }
118
119
    protected function exportIncludedDirectory(string $source, string $target): void
120
    {
121
        $iterator = new RecursiveIteratorIterator(
122
            new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
123
            RecursiveIteratorIterator::SELF_FIRST
124
        );
125
126
        foreach ($iterator as $item) {
127
            if ($item->isDir()) {
128
                continue;
129
            }
130
131
            $this->exportIncludedFile($item->getPathname(), $target.'/'.$iterator->getSubPathName());
132
        }
133
    }
134
135
    protected function excludes(string $source): bool
136
    {
137
        foreach ($this->exclude as $pattern) {
138
            if (preg_match($pattern, $source)) {
139
                return true;
140
            }
141
        }
142
143
        return false;
144
    }
145
146
    public function setCli(Command $cli)
147
    {
148
        $this->cli = $cli;
149
    }
150
}
151