Skip to content

Commit 146969c

Browse files
committed
🎨 updated project structure and tidied code execution order
1 parent 0bc4888 commit 146969c

12 files changed

Lines changed: 292 additions & 50 deletions

File tree

src/Entities/Classification.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Classification
1919
/**
2020
* @var \Doctrine\Common\Collections\Collection|Taxonomy[]
2121
*
22-
* @ManyToMany(targetEntity="Taxonomy", inversedBy="classification")
22+
* @ManyToMany(targetEntity="Taxonomy", inversedBy="classification",cascade={"persist"})
2323
* @JoinTable(
2424
* name="classifications_taxonomy",
2525
* joinColumns={

src/Entities/ContentType.php

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,24 @@ class ContentType
5757
/**
5858
* @var Collection|Taxonomy[]
5959
*
60-
* @OneToMany(targetEntity="Taxonomy", mappedBy="contentType")
60+
* @OneToMany(targetEntity="Taxonomy", mappedBy="contentType", cascade={"persist"})
6161
*/
6262
private $taxonomy;
6363

64+
/**
65+
* @var \Doctrine\Common\Collections\Collection|File[]
66+
*
67+
* @@OneToMany(targetEntity="File", mappedBy="contentType")
68+
*/
69+
private $files;
70+
6471
/**
6572
* ContentType constructor.
6673
*/
6774
public function __construct()
6875
{
6976
$this->taxonomy = new ArrayCollection();
77+
$this->files = new ArrayCollection();
7078
}
7179

7280
/**
@@ -101,6 +109,11 @@ public function getName()
101109
return $this->name;
102110
}
103111

112+
public function setName($name)
113+
{
114+
$this->name = $name;
115+
}
116+
104117
/**
105118
* @return string
106119
*/
@@ -109,6 +122,11 @@ public function getPath()
109122
return $this->path;
110123
}
111124

125+
public function setPath($path)
126+
{
127+
$this->path = $path;
128+
}
129+
112130
/**
113131
* @return string
114132
*/
@@ -117,6 +135,11 @@ public function getTemplate()
117135
return $this->template;
118136
}
119137

138+
public function setTemplate($template)
139+
{
140+
$this->template = $template;
141+
}
142+
120143
/**
121144
* @return string
122145
*/
@@ -125,6 +148,11 @@ public function getPermalink()
125148
return $this->permalink;
126149
}
127150

151+
public function setPermalink($permalink)
152+
{
153+
$this->permalink = $permalink;
154+
}
155+
128156
/**
129157
* @return boolean
130158
*/
@@ -133,6 +161,11 @@ public function getEnabled()
133161
return $this->enabled;
134162
}
135163

164+
public function setEnabled($enabled)
165+
{
166+
$this->enabled = $enabled;
167+
}
168+
136169
/**
137170
* @return Environment
138171
*/
@@ -181,4 +214,35 @@ public function removeTaxonomy(Taxonomy $taxonomy)
181214

182215
$this->taxonomy->removeElement($taxonomy);
183216
}
217+
218+
public function getFiles()
219+
{
220+
return $this->files;
221+
}
222+
223+
/**
224+
* @param File $file
225+
*/
226+
public function addFile(File $file)
227+
{
228+
if ($this->files->contains($file)) {
229+
return;
230+
}
231+
232+
$this->files->add($file);
233+
//$file->setContentType($this);
234+
}
235+
236+
/**
237+
* @param File $file
238+
*/
239+
public function removeFile(File $file)
240+
{
241+
if (!$this->files->contains($file)) {
242+
return;
243+
}
244+
245+
$this->files->removeElement($file);
246+
}
247+
184248
}

src/Entities/File.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,16 @@ class File
6565
*/
6666
private $environment;
6767

68+
/**
69+
* @var ContentType
70+
* @ManyToOne(targetEntity="ContentType")
71+
*/
72+
private $contentType;
73+
6874
/**
6975
* @var Collection|Classification[]
7076
*
71-
* @ManyToMany(targetEntity="Classification", inversedBy="files")
77+
* @ManyToMany(targetEntity="Classification", inversedBy="files", cascade={"persist"})
7278
* @JoinTable(
7379
* name="classifications_files",
7480
* joinColumns={
@@ -116,6 +122,19 @@ public function setEnvironment(Environment $environment)
116122
$this->environment = $environment;
117123
}
118124

125+
public function getContentType()
126+
{
127+
return $this->contentType;
128+
}
129+
130+
/**
131+
* @param ContentType $contentType
132+
*/
133+
public function setContentType(ContentType $contentType)
134+
{
135+
$this->contentType = $contentType;
136+
}
137+
119138
public function addFrontMatter(FrontMatter $frontMatter)
120139
{
121140
if ($this->frontMatter->contains($frontMatter)) {

src/Entities/Taxonomy.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ class Taxonomy
3030
/**
3131
* @var ContentType
3232
*
33-
* @ManyToOne(targetEntity="ContentType", inversedBy="taxonomy")
33+
* @ManyToOne(targetEntity="ContentType", inversedBy="taxonomy", cascade={"persist"})
3434
*/
3535
private $contentType;
3636

3737
/**
3838
* @var Collection|Classification[]
3939
*
40-
* @ManyToMany(targetEntity="Classification", mappedBy="taxonomy")
40+
* @ManyToMany(targetEntity="Classification", mappedBy="taxonomy", cascade={"persist"})
4141
*/
4242
private $classifications;
4343

src/Exporter.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
use Tapestry\Modules\ContentTypes\ContentTypeFactory;
1111
use Tapestry\Tapestry;
1212
use TapestryCloud\Database\Entities\Environment;
13-
use TapestryCloud\Database\Synchronizes\ContentTypes;
14-
use TapestryCloud\Database\Synchronizes\Files;
13+
use TapestryCloud\Database\Synchronizes\ContentTypeSync;
14+
use TapestryCloud\Database\Synchronizes\FileSync;
1515
use TapestryCloud\Database\Hydrators\File as FileHydrator;
16+
use TapestryCloud\Database\Hydrators\ContentType as ContentTypeHydrator;
17+
use TapestryCloud\Database\Hydrators\Taxonomy as TaxonomyHydrator;
18+
use TapestryCloud\Database\Synchronizes\TaxonomySync;
1619

1720
class Exporter
1821
{
@@ -58,10 +61,30 @@ public function export(Project $project)
5861
$this->entityManager->flush();
5962
}
6063

61-
$fileSync = new Files($this->entityManager, new FileHydrator($this->entityManager));
64+
// 1. Sync Content Types Base
65+
$contentTypeSync = new ContentTypeSync(
66+
$this->entityManager,
67+
new ContentTypeHydrator($this->entityManager),
68+
new TaxonomyHydrator($this->entityManager)
69+
);
70+
$contentTypeSync->sync($contentTypes, $environment);
71+
72+
// 2. Sync Files
73+
$fileSync = new FileSync(
74+
$this->entityManager,
75+
new FileHydrator($this->entityManager)
76+
);
6277
$fileSync->sync($files, $environment);
6378

64-
$contentTypeSync = new ContentTypes($this->entityManager);
65-
$contentTypeSync->sync($contentTypes, $environment);
79+
// 3. Sync Taxonomy foreach Content Type
80+
// 4. Sync Classifications foreach Taxonomy - attaching Files
81+
82+
$taxonomySync = new TaxonomySync(
83+
$this->entityManager,
84+
new ContentTypeHydrator($this->entityManager),
85+
new TaxonomyHydrator($this->entityManager)
86+
);
87+
$taxonomySync->sync($contentTypes, $environment);
88+
6689
}
6790
}

src/Hydrators/ContentType.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace TapestryCloud\Database\Hydrators;
4+
5+
use TapestryCloud\Database\Entities\Environment;
6+
use TapestryCloud\Database\Entities\ContentType as Model;
7+
8+
class ContentType extends Hydrator
9+
{
10+
/**
11+
* ContentType Hydration.
12+
*
13+
* @param Model $model
14+
* @param \Tapestry\Entities\ContentType $contentType
15+
* @param Environment|null $environment
16+
*/
17+
public function hydrate(Model $model, \Tapestry\Entities\ContentType $contentType, Environment $environment = null)
18+
{
19+
$model->setName($contentType->getName());
20+
$model->setPath($contentType->getPath());
21+
$model->setTemplate($contentType->getTemplate());
22+
$model->setPermalink($contentType->getPermalink());
23+
$model->setEnabled($contentType->isEnabled());
24+
25+
if (!is_null($environment)) {
26+
$model->setEnvironment($environment);
27+
}
28+
}
29+
}

src/Hydrators/File.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010

1111
class File extends Hydrator
1212
{
13-
1413
/**
1514
* File Hydration.
1615
*
1716
* @param Model $model
1817
* @param \Tapestry\Entities\File $file
19-
* @param Environment|null $environment
18+
* @param \TapestryCloud\Database\Entities\ContentType $contentType
19+
* @param null|Environment $environment
2020
*/
21-
public function hydrate(Model $model, \Tapestry\Entities\File $file, Environment $environment = null)
21+
public function hydrate(Model $model, \Tapestry\Entities\File $file, \TapestryCloud\Database\Entities\ContentType $contentType = null, Environment $environment = null)
2222
{
2323
$model->setUid($file->getUid());
2424
$model->setLastModified($file->getLastModified());
@@ -40,6 +40,10 @@ public function hydrate(Model $model, \Tapestry\Entities\File $file, Environment
4040
}
4141
}
4242

43+
if (!is_null($contentType)) {
44+
$model->setContentType($contentType);
45+
}
46+
4347
if (!is_null($environment)) {
4448
$model->setEnvironment($environment);
4549
}

src/Hydrators/Taxonomy.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace TapestryCloud\Database\Hydrators;
4+
5+
use TapestryCloud\Database\Entities\Taxonomy as Model;
6+
7+
class Taxonomy extends Hydrator
8+
{
9+
/**
10+
* Taxonomy Hydration.
11+
*
12+
* @param Model $model
13+
* @param \Tapestry\Entities\Taxonomy $taxonomy
14+
*/
15+
public function hydrate(Model $model, \Tapestry\Entities\Taxonomy $taxonomy)
16+
{
17+
$model->setName($taxonomy->getName());
18+
}
19+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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\Classification;
9+
use TapestryCloud\Database\Entities\ContentType;
10+
use TapestryCloud\Database\Entities\Environment;
11+
use TapestryCloud\Database\Entities\File;
12+
use TapestryCloud\Database\Entities\Taxonomy;
13+
use TapestryCloud\Database\Hydrators\ContentType as ContentTypeHydrator;
14+
use TapestryCloud\Database\Hydrators\Taxonomy as TaxonomyHydrator;
15+
16+
class ContentTypeSync
17+
{
18+
/**
19+
* @var EntityManagerInterface
20+
*/
21+
private $em;
22+
23+
/**
24+
* @var ContentTypeHydrator
25+
*/
26+
private $contentTypeHydrator;
27+
/**
28+
* @var TaxonomyHydrator
29+
*/
30+
private $taxonomyHydrator;
31+
32+
/**
33+
* ContentTypes constructor.
34+
* @param EntityManagerInterface $em
35+
* @param ContentTypeHydrator $contentTypeHydrator
36+
* @param TaxonomyHydrator $taxonomyHydrator
37+
*/
38+
public function __construct(
39+
EntityManagerInterface $em,
40+
ContentTypeHydrator $contentTypeHydrator,
41+
TaxonomyHydrator $taxonomyHydrator
42+
){
43+
$this->em = $em;
44+
$this->contentTypeHydrator = $contentTypeHydrator;
45+
$this->taxonomyHydrator = $taxonomyHydrator;
46+
}
47+
48+
public function sync(ContentTypeFactory $contentTypeFactory, Environment $environment)
49+
{
50+
foreach ($contentTypeFactory->all() as $contentType) {
51+
if (!$record = $this->em->getRepository(ContentType::class)->findOneBy(['name' => $contentType->getName(), 'environment' => $environment->getId()])) {
52+
$record = new ContentType();
53+
}
54+
55+
$this->contentTypeHydrator->hydrate($record, $contentType, $environment);
56+
$this->em->persist($record);
57+
}
58+
$this->em->flush();
59+
}
60+
}

0 commit comments

Comments
 (0)