Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions src/App/src/Fixture/AuthorLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Light\Blog\Entity\Author;
use Light\Blog\Entity\Post;
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 AuthorLoader extends Fixture
{
public function load(ObjectManager $manager): void
Expand All @@ -28,8 +32,9 @@ public function load(ObjectManager $manager): void

$categories = json_decode($contents, true);

$repository = $manager->getRepository(Author::class);
$seenAuthorIds = [];
$authorRepository = $manager->getRepository(Author::class);
$postRepository = $manager->getRepository(Post::class);
$seenAuthorIds = [];

foreach ($categories as $cat) {
foreach ($cat['articles'] as $article) {
Expand All @@ -38,16 +43,26 @@ public function load(ObjectManager $manager): void
continue;
}

$name = $authorData['display_name'];
if (isset($seenAuthorIds[$name])) {
$name = $authorData['display_name'];
$github = $authorData['github'] ?: null;

$identityKey = $github ?? ('name:' . $name);
if (isset($seenAuthorIds[$identityKey])) {
continue;
}
$seenAuthorIds[$name] = true;
$seenAuthorIds[$identityKey] = true;

$github = $authorData['github'] ?: null;
$slug = $this->slugify($name);
$slug = $this->slugify($name);

$author = $repository->findOneBy(['name' => $name]);
$postTitle = html_entity_decode($article['post_title'] ?? '', ENT_QUOTES, 'UTF-8');
$existingPost = $postTitle !== ''
? $postRepository->findOneBy(['slug' => $this->slugify($postTitle)])
: null;

$author = $existingPost?->getAuthor()
?? ($github !== null
? $authorRepository->findOneBy(['github' => $github])
: $authorRepository->findOneBy(['name' => $name]));

if ($author === null) {
$author = new Author();
Expand All @@ -58,6 +73,10 @@ public function load(ObjectManager $manager): void
echo "CREATE: {$name}\n";
} else {
$changed = false;
if ($author->getName() !== $name) {
$author->setName($name);
$changed = true;
}
if ($author->getSlug() !== $slug) {
$author->setSlug($slug);
$changed = true;
Expand Down
24 changes: 21 additions & 3 deletions src/App/src/Fixture/PostTagLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@
use RuntimeException;

use function file_get_contents;
use function in_array;
use function json_decode;

class PostTagLoader extends Fixture implements DependentFixtureInterface
{
public function __construct(
private readonly string $jsonFile = __DIR__ . '/articles_cleaned.json',
) {
}

public function load(ObjectManager $manager): void
{
$jsonFile = __DIR__ . '/articles_cleaned.json';
$contents = file_get_contents($jsonFile);
$contents = file_get_contents($this->jsonFile);

if ($contents === false) {
throw new RuntimeException("Unable to read file: {$jsonFile}");
throw new RuntimeException("Unable to read file: {$this->jsonFile}");
}

$categories = json_decode($contents, true);
Expand All @@ -42,7 +47,11 @@ public function load(ObjectManager $manager): void
/** @var Post $post */
$post = $this->getReference('post_' . $postIndex, Post::class);

$currentTagSlugs = [];

foreach ($articleData['tags'] ?? [] as $tagData) {
$currentTagSlugs[] = $tagData['slug'];

/** @var Tag $tag */
$tag = $this->getReference('tag_' . $tagData['slug'], Tag::class);

Expand All @@ -58,6 +67,15 @@ public function load(ObjectManager $manager): void
echo "UNCHANGED: {$post->getTitle()} - {$tag->getName()}\n";
}
}

foreach ($repository->findBy(['post' => $post]) as $existingPostTag) {
if (in_array($existingPostTag->getTag()->getSlug(), $currentTagSlugs, true)) {
continue;
}

echo "REMOVE: {$post->getTitle()} - {$existingPostTag->getTag()->getName()}\n";
$manager->remove($existingPostTag);
}
}
}

Expand Down
Loading