Skip to content

Commit 1a67c3a

Browse files
Merge pull request #23 from TheDragonCode/1.x
Working with files is put into a separate class
2 parents e3e0465 + 3db8bdd commit 1a67c3a

File tree

11 files changed

+143
-52
lines changed

11 files changed

+143
-52
lines changed

src/Services/Filesystem.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\LaravelFeed\Services;
6+
7+
use Illuminate\Filesystem\Filesystem as File;
8+
9+
use function dirname;
10+
use function fclose;
11+
use function fopen;
12+
use function fwrite;
13+
14+
class Filesystem
15+
{
16+
public function __construct(
17+
protected File $file,
18+
) {}
19+
20+
/**
21+
* @return resource
22+
*/
23+
public function open(string $path)
24+
{
25+
$path = $this->draft($path);
26+
27+
$this->ensureFileDelete($path);
28+
$this->ensureDirectory($path);
29+
30+
return fopen($path, 'ab');
31+
}
32+
33+
public function append($resource, string $content): void
34+
{
35+
if (! empty($content)) {
36+
fwrite($resource, $content);
37+
}
38+
}
39+
40+
/**
41+
* @param resource $resource
42+
*/
43+
public function release($resource, string $path): void
44+
{
45+
$this->close($resource);
46+
47+
if ($this->file->exists($path)) {
48+
$this->file->delete($path);
49+
}
50+
51+
$this->file->move(
52+
$this->draft($path),
53+
$path
54+
);
55+
}
56+
57+
/**
58+
* @param resource $resource
59+
*/
60+
public function close($resource): void
61+
{
62+
fclose($resource);
63+
}
64+
65+
protected function ensureFileDelete(string $path): void
66+
{
67+
if ($this->file->exists($path)) {
68+
$this->file->delete($path);
69+
}
70+
}
71+
72+
protected function ensureDirectory(string $path): void
73+
{
74+
$this->file->ensureDirectoryExists(dirname($path));
75+
}
76+
77+
protected function draft(string $path): string
78+
{
79+
return $path . '.draft';
80+
}
81+
}

src/Services/Generator.php

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,8 @@
77
use DragonCode\LaravelFeed\Data\ElementData;
88
use DragonCode\LaravelFeed\Feeds\Feed;
99
use Illuminate\Database\Eloquent\Collection;
10-
use Illuminate\Filesystem\Filesystem;
1110

1211
use function collect;
13-
use function dirname;
14-
use function fclose;
15-
use function fopen;
16-
use function fwrite;
1712
use function implode;
1813
use function sprintf;
1914

@@ -27,15 +22,14 @@ public function __construct(
2722
public function feed(Feed $feed): void
2823
{
2924
$file = $this->openFile(
30-
$filename = $this->draft($feed)
25+
$path = $feed->path()
3126
);
3227

3328
$this->performHeader($file, $feed);
3429
$this->performItem($file, $feed);
3530
$this->performFooter($file, $feed);
3631

37-
$this->closeFile($file);
38-
$this->release($feed, $filename);
32+
$this->release($file, $path);
3933
}
4034

4135
protected function performItem($file, Feed $feed): void
@@ -86,39 +80,16 @@ protected function makeRootAttributes(ElementData $item): string
8680

8781
protected function append($file, string $content): void
8882
{
89-
if (! empty($content)) {
90-
fwrite($file, $content);
91-
}
92-
}
93-
94-
protected function release(Feed $feed, string $draft): void
95-
{
96-
if ($this->filesystem->exists($feed->path())) {
97-
$this->filesystem->delete($feed->path());
98-
}
99-
100-
$this->filesystem->move($draft, $feed->path());
101-
}
102-
103-
protected function openFile(string $filename)
104-
{
105-
$this->ensureDirectory($filename);
106-
107-
return fopen($filename, 'ab');
108-
}
109-
110-
protected function closeFile($file): void
111-
{
112-
fclose($file);
83+
$this->filesystem->append($file, $content);
11384
}
11485

115-
protected function ensureDirectory(string $filename): void
86+
protected function release($file, string $path): void
11687
{
117-
$this->filesystem->ensureDirectoryExists(dirname($filename));
88+
$this->filesystem->release($file, $path);
11889
}
11990

120-
protected function draft(Feed $feed): string
91+
protected function openFile(string $path)
12192
{
122-
return $feed->path() . '.draft';
93+
return $this->filesystem->open($path);
12394
}
12495
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<FilledFeed>
2+
<FullFeed>
33
<news><record_title>[NEWS]:Some 1</record_title><record_content>Some content 1</record_content><extra>Some extra data</extra><with_attributes><Good_guy my-key-1="my value 1" my-key-2="my value 2"><name>Luke Skywalker</name><weapon>Lightsaber</weapon></Good_guy><Bad_guy><name><![CDATA[<h1>Sauron</h1>]]></name><weapon>Evil Eye</weapon></Bad_guy></with_attributes></news>
44
<news><record_title>[NEWS]:Some 2</record_title><record_content>Some content 2</record_content><extra>Some extra data</extra><with_attributes><Good_guy my-key-1="my value 1" my-key-2="my value 2"><name>Luke Skywalker</name><weapon>Lightsaber</weapon></Good_guy><Bad_guy><name><![CDATA[<h1>Sauron</h1>]]></name><weapon>Evil Eye</weapon></Bad_guy></with_attributes></news>
55
<news><record_title>[NEWS]:Some 3</record_title><record_content>Some content 3</record_content><extra>Some extra data</extra><with_attributes><Good_guy my-key-1="my value 1" my-key-2="my value 2"><name>Luke Skywalker</name><weapon>Lightsaber</weapon></Good_guy><Bad_guy><name><![CDATA[<h1>Sauron</h1>]]></name><weapon>Evil Eye</weapon></Bad_guy></with_attributes></news>
6-
</FilledFeed>
6+
</FullFeed>

tests/.pest/snapshots/Feature/FullTest/export_with_data_set____true__.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<FilledFeed>
2+
<FullFeed>
33
<news>
44
<record_title>[NEWS]:Some 1</record_title>
55
<record_content>Some content 1</record_content>
@@ -45,4 +45,4 @@
4545
</Bad_guy>
4646
</with_attributes>
4747
</news>
48-
</FilledFeed>
48+
</FullFeed>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<FilledFeed>
2+
<PartialFeed>
33
<news><record_title>[NEWS]:Some 1</record_title><record_content>Some content 1</record_content><extra>Some extra data</extra><with_attributes><Good_guy my-key-1="my value 1" my-key-2="my value 2"><name>Luke Skywalker</name><weapon>Lightsaber</weapon></Good_guy><Bad_guy><name><![CDATA[<h1>Sauron</h1>]]></name><weapon>Evil Eye</weapon></Bad_guy></with_attributes></news>
44
<news><record_title>[NEWS]:Some 2</record_title><record_content>Some content 2</record_content><extra>Some extra data</extra><with_attributes><Good_guy my-key-1="my value 1" my-key-2="my value 2"><name>Luke Skywalker</name><weapon>Lightsaber</weapon></Good_guy><Bad_guy><name><![CDATA[<h1>Sauron</h1>]]></name><weapon>Evil Eye</weapon></Bad_guy></with_attributes></news>
55
<news><record_title>[NEWS]:Some 3</record_title><record_content>Some content 3</record_content><extra>Some extra data</extra><with_attributes><Good_guy my-key-1="my value 1" my-key-2="my value 2"><name>Luke Skywalker</name><weapon>Lightsaber</weapon></Good_guy><Bad_guy><name><![CDATA[<h1>Sauron</h1>]]></name><weapon>Evil Eye</weapon></Bad_guy></with_attributes></news>
6-
</FilledFeed>
6+
</PartialFeed>

tests/.pest/snapshots/Feature/PartialTest/export_with_data_set____true__.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<FilledFeed>
2+
<PartialFeed>
33
<news>
44
<record_title>[NEWS]:Some 1</record_title>
55
<record_content>Some content 1</record_content>
@@ -45,4 +45,4 @@
4545
</Bad_guy>
4646
</with_attributes>
4747
</news>
48-
</FilledFeed>
48+
</PartialFeed>

tests/Feature/FullTest.php

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

55
use DragonCode\LaravelFeed\Console\Commands\FeedGenerateCommand;
66
use Workbench\App\Data\NewsFakeData;
7-
use Workbench\App\Feeds\FilledFeed;
7+
use Workbench\App\Feeds\FullFeed;
88

99
use function Pest\Laravel\artisan;
1010

@@ -13,7 +13,7 @@
1313

1414
createNews(...NewsFakeData::toArray());
1515

16-
$feed = app()->make(FilledFeed::class);
16+
$feed = app()->make(FullFeed::class);
1717

1818
artisan(FeedGenerateCommand::class)->run();
1919

tests/Feature/PartialTest.php

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

55
use DragonCode\LaravelFeed\Console\Commands\FeedGenerateCommand;
66
use Workbench\App\Data\NewsFakeData;
7-
use Workbench\App\Feeds\FilledFeed;
7+
use Workbench\App\Feeds\PartialFeed;
88

99
use function Pest\Laravel\artisan;
1010

@@ -17,7 +17,7 @@
1717

1818
createNews(...NewsFakeData::toArray());
1919

20-
$feed = app()->make(FilledFeed::class);
20+
$feed = app()->make(PartialFeed::class);
2121

2222
artisan(FeedGenerateCommand::class)->run();
2323

workbench/app/Feeds/FilledFeed.php renamed to workbench/app/Feeds/FullFeed.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use function class_basename;
1616
use function now;
1717

18-
class FilledFeed extends Feed
18+
class FullFeed extends Feed
1919
{
2020
public function builder(): Builder
2121
{
@@ -31,7 +31,7 @@ class_basename($this)
3131

3232
public function filename(): string
3333
{
34-
return 'nested/filled.xml';
34+
return 'nested/full.xml';
3535
}
3636

3737
public function item(Model $model): FeedItem
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Workbench\App\Feeds;
6+
7+
use DragonCode\LaravelFeed\Data\ElementData;
8+
use DragonCode\LaravelFeed\Feeds\Feed;
9+
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
10+
use Illuminate\Database\Eloquent\Builder;
11+
use Illuminate\Database\Eloquent\Model;
12+
use Workbench\App\Feeds\Items\NewsFeedItem;
13+
use Workbench\App\Models\News;
14+
15+
use function class_basename;
16+
use function now;
17+
18+
class PartialFeed extends Feed
19+
{
20+
public function builder(): Builder
21+
{
22+
return News::query()->where('updated_at', '>', now()->subDay());
23+
}
24+
25+
public function root(): ElementData
26+
{
27+
return new ElementData(
28+
class_basename($this)
29+
);
30+
}
31+
32+
public function item(Model $model): FeedItem
33+
{
34+
return new NewsFeedItem($model);
35+
}
36+
}

0 commit comments

Comments
 (0)