Skip to content

Commit adc5320

Browse files
committed
fix: static analysis
1 parent ce7c05d commit adc5320

File tree

1 file changed

+53
-23
lines changed

1 file changed

+53
-23
lines changed

src/SQLLoader.php

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yajra\SQLLoader;
66

7+
use Exception;
78
use Illuminate\Contracts\Filesystem\Filesystem;
89
use Illuminate\Contracts\Process\ProcessResult;
910
use Illuminate\Support\Facades\File;
@@ -14,7 +15,7 @@
1415

1516
class SQLLoader
1617
{
17-
protected string $file;
18+
protected ?string $file = null;
1819

1920
protected Method $method = Method::INSERT;
2021

@@ -24,17 +25,17 @@ class SQLLoader
2425

2526
protected string $delimiter = ',';
2627

27-
protected string $controlFile = '';
28+
protected ?string $controlFile = null;
2829

29-
protected string $disk;
30+
protected ?string $disk = null;
3031

31-
protected string $logPath = '';
32+
protected ?string $logPath = null;
3233

33-
protected ProcessResult $output;
34+
protected ?ProcessResult $output = null;
3435

35-
protected string $badFile;
36+
protected ?string $badFile = null;
3637

37-
protected string $discardFile;
38+
protected ?string $discardFile = null;
3839

3940
public function __construct(
4041
protected array $options = []
@@ -91,25 +92,29 @@ public function execute(): ProcessResult
9192
throw new InvalidArgumentException('Input file is required.');
9293
}
9394

94-
return $this->output = Process::command($this->buildCommand())->run();
95+
$command = $this->buildCommand();
96+
97+
$this->output = Process::command($command)->run();
98+
99+
return $this->output;
95100
}
96101

97102
protected function buildCommand(): string
98103
{
99-
$file = ($this->controlFile ?: Str::uuid()).'.ctl';
100-
$this->getDisk()->put($file, $this->buildControlFile());
104+
$filesystem = $this->getDisk();
101105

106+
$file = ($this->controlFile ?: Str::uuid()).'.ctl';
107+
$filesystem->put($file, $this->buildControlFile());
102108
$tns = $this->buildTNS();
103109
$binary = $this->getSqlLoaderBinary();
104-
$filePath = $this->getDisk()->path($file);
110+
$filePath = $filesystem->path($file);
105111

106112
$command = "$binary userid=$tns control={$filePath}";
107-
if (empty($this->logPath)) {
108-
$this->logPath = str_replace('.ctl', '.log', (string) $this->getDisk()->path($file));
113+
if (! $this->logPath) {
114+
$this->logPath = str_replace('.ctl', '.log', $filePath);
115+
$command .= " log={$this->logPath}";
109116
}
110117

111-
$command .= " log={$this->logPath}";
112-
113118
return $command;
114119
}
115120

@@ -138,7 +143,7 @@ protected function buildControlFile(): string
138143
->replace('$BADFILE', $this->buildBadFile())
139144
->replace('$DISCARDFILE', $this->buildDiscardFile())
140145
->replace('$FILE', "INFILE '{$this->file}'")
141-
->replace('$METHOD', $this->method->value)
146+
->replace('$METHOD', $this->buildMethod())
142147
->replace('$DELIMITER', $this->delimiter)
143148
->replace('$ENCLOSURE', $this->enclosure)
144149
->replace('$INSERTS', $this->buildInserts())
@@ -224,28 +229,45 @@ public function logsTo(string $path): static
224229

225230
public function successful(): bool
226231
{
227-
return $this->output->exitCode() === 0;
232+
if (is_null($this->output)) {
233+
return false;
234+
}
235+
236+
return $this->output->successful();
228237
}
229238

230239
public function debug(): string
231240
{
232241
$debug = 'Command:'.PHP_EOL.$this->buildCommand().PHP_EOL.PHP_EOL;
233-
$debug .= 'Output:'.$this->output().PHP_EOL.PHP_EOL;
234-
$debug .= 'Error Output:'.PHP_EOL.$this->errorOutput().PHP_EOL;
235-
$debug .= 'Exit Code: '.$this->output->exitCode().PHP_EOL.PHP_EOL;
236242
$debug .= 'Control File:'.PHP_EOL.$this->buildControlFile().PHP_EOL;
237243

244+
if ($this->output) {
245+
$debug .= 'Output:'.$this->output->output().PHP_EOL.PHP_EOL;
246+
$debug .= 'Error Output:'.PHP_EOL.$this->output->errorOutput().PHP_EOL;
247+
$debug .= 'Exit Code: '.$this->output->exitCode().PHP_EOL.PHP_EOL;
248+
}
249+
238250
return $debug;
239251
}
240252

241-
public function output(): string
253+
/**
254+
* @throws \Exception
255+
*/
256+
public function output(): ProcessResult
242257
{
243-
return $this->output->output();
258+
if (is_null($this->output)) {
259+
throw new Exception('No output available');
260+
}
261+
262+
return $this->output;
244263
}
245264

265+
/**
266+
* @throws \Exception
267+
*/
246268
public function errorOutput(): string
247269
{
248-
return $this->output->errorOutput();
270+
return $this->output()->errorOutput();
249271
}
250272

251273
public function delimiter(string $delimiter): static
@@ -261,4 +283,12 @@ public function enclosure(string $enclosure): static
261283

262284
return $this;
263285
}
286+
287+
protected function buildMethod(): string
288+
{
289+
return in_array($this->method, [
290+
Method::INSERT,
291+
Method::TRUNCATE,
292+
]) ? Method::TRUNCATE->value : $this->method->value;
293+
}
264294
}

0 commit comments

Comments
 (0)