Skip to content

Commit d1f62f3

Browse files
committed
🚧 working on entities
1 parent 2b92ad5 commit d1f62f3

5 files changed

Lines changed: 136 additions & 8 deletions

File tree

src/Entities/ContentType.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace TapestryCloud\Database\Entities;
44

5+
use Tapestry\Entities\ContentType as TapestryContentType;
6+
57
/**
68
* @Entity
79
* @Table(name="content_types")
@@ -32,6 +34,22 @@ class ContentType
3234
/** @OnetoMany(targetEntity="Taxonomy", mappedBy="content_type_id") */
3335
private $taxonomy;
3436

37+
/**
38+
* ContentType Hydration
39+
*
40+
* @param TapestryContentType $contentType
41+
* @param Environment $environment
42+
*/
43+
public function hydrate(TapestryContentType $contentType, Environment $environment)
44+
{
45+
$this->name = $contentType->getName();
46+
$this->path = $contentType->getPath();
47+
$this->template = $contentType->getTemplate();
48+
$this->permalink = $contentType->getPermalink();
49+
$this->enabled = $contentType->isEnabled();
50+
$this->setEnvironment($environment);
51+
}
52+
3553
/**
3654
* @return string
3755
*/
@@ -79,4 +97,22 @@ public function getEnvironment()
7997
{
8098
return $this->environment;
8199
}
100+
101+
public function setEnvironment(Environment $environment)
102+
{
103+
$this->environment = $environment;
104+
}
105+
106+
/**
107+
* @return Taxonomy[]
108+
*/
109+
public function getTaxonomy()
110+
{
111+
return $this->taxonomy;
112+
}
113+
114+
public function addTaxonomy(Taxonomy $taxonomy)
115+
{
116+
$this->taxonomy[] = $taxonomy;
117+
}
82118
}

src/Entities/Environment.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ class Environment
2020
/** @OneToMany(targetEntity="File", mappedBy="environment_id") */
2121
private $files;
2222

23+
/**
24+
* @return int
25+
*/
26+
public function getId()
27+
{
28+
return $this->id;
29+
}
30+
2331
public function getName()
2432
{
2533
return $this->name;

src/Entities/Taxonomy.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace TapestryCloud\Database\Entities;
44

5+
use Tapestry\Entities\Taxonomy as TapestryTaxonomy;
6+
57
/**
68
* @Entity
79
* @Table(name="taxonomies")
@@ -17,6 +19,15 @@ class Taxonomy
1719
/** @OnetoMany(targetEntity="Classification", mappedBy="taxonomy_id") */
1820
private $classifications;
1921

22+
/**
23+
* Taxonomy Hydration.
24+
* @param TapestryTaxonomy $taxonomy
25+
*/
26+
public function hydrate(TapestryTaxonomy $taxonomy)
27+
{
28+
$this->name = $taxonomy->getName();
29+
}
30+
2031
/**
2132
* @return string
2233
*/

src/Exporter.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
use Tapestry\Entities\Project;
1010
use Doctrine\DBAL\Connection;
1111
use Tapestry\Modules\ContentTypes\ContentTypeFactory;
12+
use TapestryCloud\Database\Entities\ContentType;
1213
use TapestryCloud\Database\Entities\Environment;
1314
use TapestryCloud\Database\Entities\File;
15+
use TapestryCloud\Database\Synchronizes\ContentTypes;
1416

1517
class Exporter {
1618

@@ -68,15 +70,18 @@ public function export(Project $project) {
6870
$this->entityManager->flush();
6971
}
7072

71-
/** @var \Tapestry\Entities\File $file */
72-
foreach ($files as $file) {
73-
$fileRecord = new File();
74-
$fileRecord->setUid($file->getUid());
75-
$environment->addFile($fileRecord);
76-
$this->entityManager->persist($fileRecord);
77-
}
73+
// /** @var \Tapestry\Entities\File $file */
74+
// foreach ($files as $file) {
75+
// $fileRecord = new File();
76+
// $fileRecord->setUid($file->getUid());
77+
// $environment->addFile($fileRecord);
78+
// $this->entityManager->persist($fileRecord);
79+
// }
80+
81+
// $this->entityManager->flush();
7882

79-
$this->entityManager->flush();
83+
$contentTypeSync = new ContentTypes($this->entityManager);
84+
$contentTypeSync->sync($contentTypes, $environment);
8085

8186
}
8287
}

src/Synchronizes/ContentTypes.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace TapestryCloud\Database\Synchronizes;
4+
5+
use Doctrine\ORM\EntityManagerInterface;
6+
use Tapestry\Entities\Taxonomy as TapestryTaxonomy;
7+
use Tapestry\Modules\ContentTypes\ContentTypeFactory;
8+
use TapestryCloud\Database\Entities\ContentType;
9+
use TapestryCloud\Database\Entities\Environment;
10+
use TapestryCloud\Database\Entities\Taxonomy;
11+
12+
class ContentTypes
13+
{
14+
/**
15+
* @var EntityManagerInterface
16+
*/
17+
private $em;
18+
19+
/**
20+
* ContentTypes constructor.
21+
* @param EntityManagerInterface $em
22+
*/
23+
public function __construct(EntityManagerInterface $em)
24+
{
25+
$this->em = $em;
26+
}
27+
28+
public function sync(ContentTypeFactory $contentTypeFactory, Environment $environment) {
29+
foreach($contentTypeFactory->all() as $contentType) {
30+
if (!$record = $this->em->getRepository(ContentType::class)->findOneBy(['name' => $contentType->getName(), 'environment' => $environment->getId()])){
31+
// create and save
32+
$record = new ContentType();
33+
$record->hydrate($contentType, $environment);
34+
$this->em->persist($record);
35+
$this->em->flush();
36+
37+
$this->syncTaxonomyToContentType($record, $contentType->getTaxonomies());
38+
continue;
39+
}
40+
41+
// update and save
42+
}
43+
}
44+
45+
/**
46+
* @param ContentType $contentType
47+
* @param TapestryTaxonomy[] $taxonomies
48+
*/
49+
private function syncTaxonomyToContentType(ContentType $contentType, array $taxonomies) {
50+
51+
$n = $contentType->getTaxonomy();
52+
$p = 1;
53+
54+
/** @var TapestryTaxonomy $taxonomy */
55+
foreach($taxonomies as $taxonomy) {
56+
if (!$record = $this->em->getRepository(Taxonomy::class)->findOneBy(['name' => $taxonomy->getName()])){
57+
$record = new Taxonomy();
58+
$record->hydrate($taxonomy);
59+
$this->em->persist($record);
60+
$this->em->flush();
61+
62+
$contentType->addTaxonomy($record);
63+
$this->em->persist($contentType);
64+
$this->em->flush();
65+
}
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)