Skip to content

Commit 9d220f6

Browse files
committed
Entity update for author and update fixtures to include github links for authors
1 parent aa5cdc1 commit 9d220f6

9 files changed

Lines changed: 276 additions & 220 deletions

File tree

bin/doctrine-fixtures

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ $executor = new ORMExecutor($entityManager, $purger);
3333

3434
echo "Loading fixtures from: {$fixturesPath}\n";
3535

36-
$executor->execute($loader->getFixtures());
36+
$executor->execute($loader->getFixtures(), true);
3737

3838
echo "Fixtures loaded successfully!\n";

src/App/src/Fixture/AuthorLoader.php

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function load(ObjectManager $manager): void
2828

2929
$categories = json_decode($contents, true);
3030

31+
$repository = $manager->getRepository(Author::class);
3132
$seenAuthorIds = [];
3233

3334
foreach ($categories as $cat) {
@@ -37,23 +38,39 @@ public function load(ObjectManager $manager): void
3738
continue;
3839
}
3940

40-
$wpAuthorId = $authorData['user_email'];
41+
$wpAuthorId = $authorData['github'];
4142
if (isset($seenAuthorIds[$wpAuthorId])) {
4243
continue;
4344
}
4445
$seenAuthorIds[$wpAuthorId] = true;
4546

46-
$name = $authorData['display_name'];
47-
$email = $authorData['user_email'];
48-
$slug = $this->slugify($name);
49-
50-
$author = new Author();
51-
$author->setName($name);
52-
$author->setSlug($slug);
53-
$author->setEmail($email);
54-
$author->setBio(null);
47+
$name = $authorData['display_name'];
48+
$github = $authorData['github'];
49+
$slug = $this->slugify($name);
50+
51+
$author = $repository->findOneBy(['name' => $name]);
52+
53+
if ($author === null) {
54+
$author = new Author();
55+
$author->setName($name);
56+
$author->setSlug($slug);
57+
$author->setGithub($github);
58+
$manager->persist($author);
59+
echo "CREATE: {$name}\n";
60+
} else {
61+
$changed = false;
62+
if ($author->getSlug() !== $slug) {
63+
$author->setSlug($slug);
64+
$changed = true;
65+
}
66+
if ($author->getGithub() !== $github) {
67+
$author->setGithub($github);
68+
$changed = true;
69+
}
70+
71+
echo ($changed ? "UPDATE: {$name}\n" : "UNCHANGED: {$name}\n");
72+
}
5573

56-
$manager->persist($author);
5774
$this->addReference('author_' . $wpAuthorId, $author);
5875
}
5976
}
@@ -72,4 +89,4 @@ public function getOrder(): int
7289
{
7390
return 1;
7491
}
75-
}
92+
}

src/App/src/Fixture/CategoryLoader.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,23 @@ public function load(ObjectManager $manager): void
2525

2626
$categories = json_decode($contents, true);
2727

28+
$repository = $manager->getRepository(Category::class);
29+
2830
foreach ($categories as $cat) {
29-
$category = new Category();
31+
$category = $repository->findOneBy(['slug' => $cat['slug']]);
32+
33+
if ($category === null) {
34+
$category = new Category();
35+
$category->setSlug($cat['slug']);
36+
$manager->persist($category);
37+
echo "CREATE: {$cat['name']}\n";
38+
} else {
39+
echo "UNCHANGED: {$cat['name']}\n";
40+
}
41+
3042
$category->setName($cat['name']);
31-
$category->setSlug($cat['slug']);
3243
$category->setVisibility($cat['isVisible'] ?? true);
3344

34-
$manager->persist($category);
3545
$this->addReference('category_' . $cat['slug'], $category);
3646
}
3747

src/App/src/Fixture/PostLoader.php

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ public function load(ObjectManager $manager): void
3636

3737
$categories = json_decode($contents, true);
3838

39-
$usedSlugs = [];
39+
$repository = $manager->getRepository(Post::class);
40+
$usedSlugs = [];
4041

4142
foreach ($categories as $cat) {
4243
/** @var Category $category */
4344
$category = $this->getReference('category_' . $cat['slug'], Category::class);
4445

4546
foreach ($cat['articles'] as $articleData) {
46-
$wpAuthorId = $articleData['author']['user_email'] ?? null;
47+
$wpAuthorId = $articleData['author']['github'] ?? null;
4748
if (! $wpAuthorId || ! $this->hasReference('author_' . $wpAuthorId, Author::class)) {
4849
echo "SKIP (no author): {$articleData['post_title']}\n";
4950
continue;
@@ -72,16 +73,52 @@ public function load(ObjectManager $manager): void
7273
? new DateTimeImmutable($rawDate)
7374
: new DateTimeImmutable();
7475

75-
$article = new Post();
76-
$article->setTitle($title);
77-
$article->setSlug($slug);
78-
$article->setPostDate($postDate);
79-
$article->setStatus($status);
80-
$article->setCategory($category);
81-
$article->setAuthor($author);
82-
$article->setExcerpt($articleData['excerpt'] ?? '');
76+
$excerpt = $articleData['excerpt'] ?? '';
8377

84-
$manager->persist($article);
78+
$article = $repository->findOneBy(['slug' => $slug]);
79+
80+
if ($article === null) {
81+
$article = new Post();
82+
$article->setSlug($slug);
83+
$article->setTitle($title);
84+
$article->setPostDate($postDate);
85+
$article->setStatus($status);
86+
$article->setCategory($category);
87+
$article->setAuthor($author);
88+
$article->setExcerpt($excerpt);
89+
90+
$manager->persist($article);
91+
echo "CREATE: {$title}\n";
92+
} else {
93+
$changed = false;
94+
95+
if ($article->getTitle() !== $title) {
96+
$article->setTitle($title);
97+
$changed = true;
98+
}
99+
if ($article->getPostDate()->format('Y-m-d H:i:s') !== $postDate->format('Y-m-d H:i:s')) {
100+
$article->setPostDate($postDate);
101+
$changed = true;
102+
}
103+
if ($article->getStatus() !== $status) {
104+
$article->setStatus($status);
105+
$changed = true;
106+
}
107+
if ($article->getCategory() !== $category) {
108+
$article->setCategory($category);
109+
$changed = true;
110+
}
111+
if ($article->getAuthor() !== $author) {
112+
$article->setAuthor($author);
113+
$changed = true;
114+
}
115+
if ($article->getExcerpt() !== $excerpt) {
116+
$article->setExcerpt($excerpt);
117+
$changed = true;
118+
}
119+
120+
echo ($changed ? "UPDATE: {$title}\n" : "UNCHANGED: {$title}\n");
121+
}
85122
}
86123
}
87124

@@ -107,4 +144,4 @@ public function getOrder(): int
107144
{
108145
return 3;
109146
}
110-
}
147+
}

0 commit comments

Comments
 (0)