Skip to content

Commit

Permalink
🎨 updated project structure and tidied code execution order
Browse files Browse the repository at this point in the history
  • Loading branch information
carbontwelve committed Nov 24, 2017
1 parent 0bc4888 commit 146969c
Show file tree
Hide file tree
Showing 12 changed files with 292 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/Entities/Classification.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Classification
/**
* @var \Doctrine\Common\Collections\Collection|Taxonomy[]
*
* @ManyToMany(targetEntity="Taxonomy", inversedBy="classification")
* @ManyToMany(targetEntity="Taxonomy", inversedBy="classification",cascade={"persist"})
* @JoinTable(
* name="classifications_taxonomy",
* joinColumns={
Expand Down
66 changes: 65 additions & 1 deletion src/Entities/ContentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,24 @@ class ContentType
/**
* @var Collection|Taxonomy[]
*
* @OneToMany(targetEntity="Taxonomy", mappedBy="contentType")
* @OneToMany(targetEntity="Taxonomy", mappedBy="contentType", cascade={"persist"})
*/
private $taxonomy;

/**
* @var \Doctrine\Common\Collections\Collection|File[]
*
* @@OneToMany(targetEntity="File", mappedBy="contentType")
*/
private $files;

/**
* ContentType constructor.
*/
public function __construct()
{
$this->taxonomy = new ArrayCollection();
$this->files = new ArrayCollection();
}

/**
Expand Down Expand Up @@ -101,6 +109,11 @@ public function getName()
return $this->name;
}

public function setName($name)
{
$this->name = $name;
}

/**
* @return string
*/
Expand All @@ -109,6 +122,11 @@ public function getPath()
return $this->path;
}

public function setPath($path)
{
$this->path = $path;
}

/**
* @return string
*/
Expand All @@ -117,6 +135,11 @@ public function getTemplate()
return $this->template;
}

public function setTemplate($template)
{
$this->template = $template;
}

/**
* @return string
*/
Expand All @@ -125,6 +148,11 @@ public function getPermalink()
return $this->permalink;
}

public function setPermalink($permalink)
{
$this->permalink = $permalink;
}

/**
* @return boolean
*/
Expand All @@ -133,6 +161,11 @@ public function getEnabled()
return $this->enabled;
}

public function setEnabled($enabled)
{
$this->enabled = $enabled;
}

/**
* @return Environment
*/
Expand Down Expand Up @@ -181,4 +214,35 @@ public function removeTaxonomy(Taxonomy $taxonomy)

$this->taxonomy->removeElement($taxonomy);
}

public function getFiles()
{
return $this->files;
}

/**
* @param File $file
*/
public function addFile(File $file)
{
if ($this->files->contains($file)) {
return;
}

$this->files->add($file);
//$file->setContentType($this);
}

/**
* @param File $file
*/
public function removeFile(File $file)
{
if (!$this->files->contains($file)) {
return;
}

$this->files->removeElement($file);
}

}
21 changes: 20 additions & 1 deletion src/Entities/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,16 @@ class File
*/
private $environment;

/**
* @var ContentType
* @ManyToOne(targetEntity="ContentType")
*/
private $contentType;

/**
* @var Collection|Classification[]
*
* @ManyToMany(targetEntity="Classification", inversedBy="files")
* @ManyToMany(targetEntity="Classification", inversedBy="files", cascade={"persist"})
* @JoinTable(
* name="classifications_files",
* joinColumns={
Expand Down Expand Up @@ -116,6 +122,19 @@ public function setEnvironment(Environment $environment)
$this->environment = $environment;
}

public function getContentType()
{
return $this->contentType;
}

/**
* @param ContentType $contentType
*/
public function setContentType(ContentType $contentType)
{
$this->contentType = $contentType;
}

public function addFrontMatter(FrontMatter $frontMatter)
{
if ($this->frontMatter->contains($frontMatter)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Entities/Taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ class Taxonomy
/**
* @var ContentType
*
* @ManyToOne(targetEntity="ContentType", inversedBy="taxonomy")
* @ManyToOne(targetEntity="ContentType", inversedBy="taxonomy", cascade={"persist"})
*/
private $contentType;

/**
* @var Collection|Classification[]
*
* @ManyToMany(targetEntity="Classification", mappedBy="taxonomy")
* @ManyToMany(targetEntity="Classification", mappedBy="taxonomy", cascade={"persist"})
*/
private $classifications;

Expand Down
33 changes: 28 additions & 5 deletions src/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
use Tapestry\Modules\ContentTypes\ContentTypeFactory;
use Tapestry\Tapestry;
use TapestryCloud\Database\Entities\Environment;
use TapestryCloud\Database\Synchronizes\ContentTypes;
use TapestryCloud\Database\Synchronizes\Files;
use TapestryCloud\Database\Synchronizes\ContentTypeSync;
use TapestryCloud\Database\Synchronizes\FileSync;
use TapestryCloud\Database\Hydrators\File as FileHydrator;
use TapestryCloud\Database\Hydrators\ContentType as ContentTypeHydrator;
use TapestryCloud\Database\Hydrators\Taxonomy as TaxonomyHydrator;
use TapestryCloud\Database\Synchronizes\TaxonomySync;

class Exporter
{
Expand Down Expand Up @@ -58,10 +61,30 @@ public function export(Project $project)
$this->entityManager->flush();
}

$fileSync = new Files($this->entityManager, new FileHydrator($this->entityManager));
// 1. Sync Content Types Base
$contentTypeSync = new ContentTypeSync(
$this->entityManager,
new ContentTypeHydrator($this->entityManager),
new TaxonomyHydrator($this->entityManager)
);
$contentTypeSync->sync($contentTypes, $environment);

// 2. Sync Files
$fileSync = new FileSync(
$this->entityManager,
new FileHydrator($this->entityManager)
);
$fileSync->sync($files, $environment);

$contentTypeSync = new ContentTypes($this->entityManager);
$contentTypeSync->sync($contentTypes, $environment);
// 3. Sync Taxonomy foreach Content Type
// 4. Sync Classifications foreach Taxonomy - attaching Files

$taxonomySync = new TaxonomySync(
$this->entityManager,
new ContentTypeHydrator($this->entityManager),
new TaxonomyHydrator($this->entityManager)
);
$taxonomySync->sync($contentTypes, $environment);

}
}
29 changes: 29 additions & 0 deletions src/Hydrators/ContentType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace TapestryCloud\Database\Hydrators;

use TapestryCloud\Database\Entities\Environment;
use TapestryCloud\Database\Entities\ContentType as Model;

class ContentType extends Hydrator
{
/**
* ContentType Hydration.
*
* @param Model $model
* @param \Tapestry\Entities\ContentType $contentType
* @param Environment|null $environment
*/
public function hydrate(Model $model, \Tapestry\Entities\ContentType $contentType, Environment $environment = null)
{
$model->setName($contentType->getName());
$model->setPath($contentType->getPath());
$model->setTemplate($contentType->getTemplate());
$model->setPermalink($contentType->getPermalink());
$model->setEnabled($contentType->isEnabled());

if (!is_null($environment)) {
$model->setEnvironment($environment);
}
}
}
10 changes: 7 additions & 3 deletions src/Hydrators/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

class File extends Hydrator
{

/**
* File Hydration.
*
* @param Model $model
* @param \Tapestry\Entities\File $file
* @param Environment|null $environment
* @param \TapestryCloud\Database\Entities\ContentType $contentType
* @param null|Environment $environment
*/
public function hydrate(Model $model, \Tapestry\Entities\File $file, Environment $environment = null)
public function hydrate(Model $model, \Tapestry\Entities\File $file, \TapestryCloud\Database\Entities\ContentType $contentType = null, Environment $environment = null)
{
$model->setUid($file->getUid());
$model->setLastModified($file->getLastModified());
Expand All @@ -40,6 +40,10 @@ public function hydrate(Model $model, \Tapestry\Entities\File $file, Environment
}
}

if (!is_null($contentType)) {
$model->setContentType($contentType);
}

if (!is_null($environment)) {
$model->setEnvironment($environment);
}
Expand Down
19 changes: 19 additions & 0 deletions src/Hydrators/Taxonomy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace TapestryCloud\Database\Hydrators;

use TapestryCloud\Database\Entities\Taxonomy as Model;

class Taxonomy extends Hydrator
{
/**
* Taxonomy Hydration.
*
* @param Model $model
* @param \Tapestry\Entities\Taxonomy $taxonomy
*/
public function hydrate(Model $model, \Tapestry\Entities\Taxonomy $taxonomy)
{
$model->setName($taxonomy->getName());
}
}
60 changes: 60 additions & 0 deletions src/Synchronizes/ContentTypeSync.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace TapestryCloud\Database\Synchronizes;

use Doctrine\ORM\EntityManagerInterface;
use Tapestry\Entities\Taxonomy as TapestryTaxonomy;
use Tapestry\Modules\ContentTypes\ContentTypeFactory;
use TapestryCloud\Database\Entities\Classification;
use TapestryCloud\Database\Entities\ContentType;
use TapestryCloud\Database\Entities\Environment;
use TapestryCloud\Database\Entities\File;
use TapestryCloud\Database\Entities\Taxonomy;
use TapestryCloud\Database\Hydrators\ContentType as ContentTypeHydrator;
use TapestryCloud\Database\Hydrators\Taxonomy as TaxonomyHydrator;

class ContentTypeSync
{
/**
* @var EntityManagerInterface
*/
private $em;

/**
* @var ContentTypeHydrator
*/
private $contentTypeHydrator;
/**
* @var TaxonomyHydrator
*/
private $taxonomyHydrator;

/**
* ContentTypes constructor.
* @param EntityManagerInterface $em
* @param ContentTypeHydrator $contentTypeHydrator
* @param TaxonomyHydrator $taxonomyHydrator
*/
public function __construct(
EntityManagerInterface $em,
ContentTypeHydrator $contentTypeHydrator,
TaxonomyHydrator $taxonomyHydrator
){
$this->em = $em;
$this->contentTypeHydrator = $contentTypeHydrator;
$this->taxonomyHydrator = $taxonomyHydrator;
}

public function sync(ContentTypeFactory $contentTypeFactory, Environment $environment)
{
foreach ($contentTypeFactory->all() as $contentType) {
if (!$record = $this->em->getRepository(ContentType::class)->findOneBy(['name' => $contentType->getName(), 'environment' => $environment->getId()])) {
$record = new ContentType();
}

$this->contentTypeHydrator->hydrate($record, $contentType, $environment);
$this->em->persist($record);
}
$this->em->flush();
}
}
Loading

0 comments on commit 146969c

Please sign in to comment.