Skip to content

Commit c408925

Browse files
authored
Merge pull request #29 from AndreasElia/new/refactoring
More Refactoring
2 parents 65ae51b + 66e2ae2 commit c408925

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/.idea
12
/vendor
23
composer.lock
34
.phpunit.result.cache

src/Commands/ExportPostmanCommand.php

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,25 @@ class ExportPostmanCommand extends Command
3232
/** @var null */
3333
protected $filename;
3434

35+
/** @var string */
36+
private $bearer;
37+
3538
public function __construct(Router $router, Repository $config)
3639
{
3740
parent::__construct();
3841

3942
$this->router = $router;
4043
$this->config = $config['api-postman'];
41-
$this->filename = $this->formatFilename();
4244
}
4345

4446
public function handle(): void
4547
{
46-
$this->initStructure();
47-
48-
if ($bearer = $this->option('bearer') ?? false) {
49-
$this->structure['variable'][] = [
50-
'key' => 'token',
51-
'value' => $bearer,
52-
];
53-
}
48+
$this->setFilename();
49+
$this->setBearerToken();
50+
$this->initializeStructure();
5451

5552
foreach ($this->router->getRoutes() as $route) {
56-
$methods = collect($route->methods())->reject(fn ($method) => $method == 'HEAD');
53+
$methods = array_filter($route->methods(), fn ($value) => $value !== 'HEAD');
5754
$middlewares = $route->gatherMiddleware();
5855

5956
foreach ($methods as $method) {
@@ -103,7 +100,7 @@ public function handle(): void
103100

104101
$routeHeaders = $this->config['headers'];
105102

106-
if ($bearer && in_array($this->config['auth_middleware'], $middlewares)) {
103+
if ($this->bearer && in_array($this->config['auth_middleware'], $middlewares)) {
107104
$routeHeaders[] = [
108105
'key' => 'Authorization',
109106
'value' => 'Bearer {{token}}',
@@ -229,7 +226,7 @@ public function makeRequest($route, $method, $routeHeaders, $requestRules)
229226
return $data;
230227
}
231228

232-
protected function initStructure(): void
229+
protected function initializeStructure(): void
233230
{
234231
$this->structure = [
235232
'variable' => [
@@ -244,17 +241,29 @@ protected function initStructure(): void
244241
],
245242
'item' => [],
246243
];
244+
245+
if ($this->bearer) {
246+
$this->structure['variable'][] = [
247+
'key' => 'token',
248+
'value' => $this->bearer,
249+
];
250+
}
247251
}
248252

249-
protected function formatFilename()
253+
protected function setFilename()
250254
{
251-
return str_replace(
255+
$this->filename = str_replace(
252256
['{timestamp}', '{app}'],
253257
[date('Y_m_d_His'), Str::snake(config('app.name'))],
254258
$this->config['filename']
255259
);
256260
}
257261

262+
protected function setBearerToken()
263+
{
264+
$this->bearer = $this->option('bearer') ?? null;
265+
}
266+
258267
protected function isStructured()
259268
{
260269
return $this->config['structured'];

0 commit comments

Comments
 (0)