Repository navigation
Expand file tree
/
Copy pathPostLoader.php
More file actions
172 lines (145 loc) · 6.13 KB
/
Copy pathPostLoader.php
File metadata and controls
172 lines (145 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
declare(strict_types=1);
namespace Light\App\Fixture;
use DateTimeImmutable;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Light\Blog\Entity\Author;
use Light\Blog\Entity\Category;
use Light\Blog\Entity\Post;
use Light\Blog\Enum\PostStatusEnum;
use RuntimeException;
use function file_get_contents;
use function html_entity_decode;
use function json_decode;
use function preg_replace;
use function strtolower;
use function trim;
use const ENT_QUOTES;
class PostLoader extends Fixture implements DependentFixtureInterface
{
public function load(ObjectManager $manager): void
{
$jsonFile = __DIR__ . '/articles_cleaned.json';
$contents = file_get_contents($jsonFile);
if ($contents === false) {
throw new RuntimeException("Unable to read file: {$jsonFile}");
}
$categories = json_decode($contents, true);
$repository = $manager->getRepository(Post::class);
$usedSlugs = [];
$postIndex = 0;
foreach ($categories as $cat) {
/** @var Category $category */
$category = $this->getReference('category_' . $cat['slug'], Category::class);
foreach ($cat['articles'] as $articleData) {
$postIndex++;
$authorName = $articleData['author']['display_name'] ?? null;
$authorSlug = $authorName ? $this->slugify($authorName) : null;
if (! $authorSlug || ! $this->hasReference('author_' . $authorSlug, Author::class)) {
echo "SKIP (no author): {$articleData['post_title']}\n";
continue;
}
/** @var Author $author */
$author = $this->getReference('author_' . $authorSlug, Author::class);
$title = html_entity_decode($articleData['post_title'], ENT_QUOTES, 'UTF-8');
$slug = $this->slugify($title);
if (isset($usedSlugs[$slug])) {
$usedSlugs[$slug]++;
$slug .= '-' . $usedSlugs[$slug];
} else {
$usedSlugs[$slug] = 1;
}
$status = match ($articleData['post_status']) {
'published' => PostStatusEnum::Published,
'private' => PostStatusEnum::Private,
'archived' => PostStatusEnum::Archived,
default => PostStatusEnum::Draft,
};
$rawDate = $articleData['post_date'] ?? '';
$postDate = ! empty($rawDate) && $rawDate !== '0000-00-00 00:00:00'
? new DateTimeImmutable($rawDate)
: new DateTimeImmutable();
$excerpt = $articleData['excerpt'] ?? '';
$tlDr = $articleData['tl_dr'] ?? '';
$isObsolete = (bool) ($articleData['isObsolete'] ?? false);
$openGraphImg = $articleData['opengraph_img'] ?? null;
$article = $repository->findOneBy(['slug' => $slug]);
if ($article === null) {
$article = new Post();
$article->setSlug($slug);
$article->setTitle($title);
$article->setPostDate($postDate);
$article->setStatus($status);
$article->setCategory($category);
$article->setAuthor($author);
$article->setExcerpt($excerpt);
$article->setTldr($tlDr);
$article->setObsolete($isObsolete);
$article->setOpenGraphImage($openGraphImg);
$manager->persist($article);
echo "CREATE: {$title}\n";
} else {
$changed = false;
if ($article->getTitle() !== $title) {
$article->setTitle($title);
$changed = true;
}
if ($article->getPostDate()->format('Y-m-d H:i:s') !== $postDate->format('Y-m-d H:i:s')) {
$article->setPostDate($postDate);
$changed = true;
}
if ($article->getStatus() !== $status) {
$article->setStatus($status);
$changed = true;
}
if ($article->getCategory() !== $category) {
$article->setCategory($category);
$changed = true;
}
if ($article->getAuthor() !== $author) {
$article->setAuthor($author);
$changed = true;
}
if ($article->getExcerpt() !== $excerpt) {
$article->setExcerpt($excerpt);
$changed = true;
}
if ($article->getTlDr() !== $tlDr) {
$article->setTldr($tlDr);
$changed = true;
}
if ($article->isObsolete() !== $isObsolete) {
$article->setObsolete($isObsolete);
$changed = true;
}
if ($article->getOpenGraphImage() !== $openGraphImg) {
$article->setOpenGraphImage($openGraphImg);
$changed = true;
}
echo $changed ? "UPDATE: {$title}\n" : "UNCHANGED: {$title}\n";
}
$this->addReference('post_' . $postIndex, $article);
}
}
$manager->flush();
}
public function getDependencies(): array
{
return [
AuthorLoader::class,
CategoryLoader::class,
];
}
private function slugify(string $text): string
{
$text = strtolower(trim($text));
$text = preg_replace('/[^a-z0-9]+/', '-', $text) ?? '';
return trim($text, '-');
}
public function getOrder(): int
{
return 3;
}
}