Skip to content

Commit f8fd9d0

Browse files
committed
🚧 file entities now get their classifications added
1 parent 2f02c93 commit f8fd9d0

4 files changed

Lines changed: 107 additions & 14 deletions

File tree

src/Entities/Classification.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Classification
2121
*
2222
* @ManyToMany(targetEntity="Taxonomy", inversedBy="classification")
2323
* @JoinTable(
24-
* name="taxonomy_classifications",
24+
* name="classifications_taxonomy",
2525
* joinColumns={
2626
* @JoinColumn(name="classification_id", referencedColumnName="id")
2727
* },
@@ -35,11 +35,7 @@ class Classification
3535
/**
3636
* @var \Doctrine\Common\Collections\Collection|File[]
3737
*
38-
* @ManyToMany(targetEntity="Classification")
39-
* @JoinTable(name="file_classifications",
40-
* joinColumns={@JoinColumn(name="classification_id", referencedColumnName="id")},
41-
* inverseJoinColumns={@JoinColumn(name="file_id", referencedColumnName="id")}
42-
* )
38+
* @ManyToMany(targetEntity="File", mappedBy="classifications")
4339
*/
4440
private $files;
4541

src/Entities/File.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

33
namespace TapestryCloud\Database\Entities;
4+
use Doctrine\Common\Collections\ArrayCollection;
5+
use Doctrine\Common\Collections\Collection;
46

57
/**
68
* @Entity
@@ -18,11 +20,41 @@ class File
1820
* @ManyToOne(targetEntity="Environment") */
1921
private $environment;
2022

23+
/**
24+
* @var Collection|Classification[]
25+
*
26+
* @ManyToMany(targetEntity="Classification", inversedBy="files")
27+
* @JoinTable(
28+
* name="classifications_files",
29+
* joinColumns={
30+
* @JoinColumn(name="file_id", referencedColumnName="id")
31+
* },
32+
* inverseJoinColumns={
33+
* @JoinColumn(name="classification_id", referencedColumnName="id")
34+
* }
35+
* )
36+
*/
37+
private $classifications;
38+
2139
/**
2240
* @var string
2341
* @Column(type="string") */
2442
private $uid;
2543

44+
/**
45+
* File constructor.
46+
*/
47+
public function __construct()
48+
{
49+
$this->classifications = new ArrayCollection();
50+
}
51+
52+
/**
53+
* File Hydration.
54+
*
55+
* @param \Tapestry\Entities\File $file
56+
* @param Environment|null $environment
57+
*/
2658
public function hydrate(\Tapestry\Entities\File $file, Environment $environment = null) {
2759
$this->uid = $file->getUid();
2860

@@ -59,4 +91,28 @@ public function setEnvironment(Environment $environment)
5991
{
6092
$this->environment = $environment;
6193
}
94+
95+
/**
96+
* @param Classification $classification
97+
*/
98+
public function addClassification(Classification $classification)
99+
{
100+
if ($this->classifications->contains($classification)) {
101+
return;
102+
}
103+
104+
$this->classifications->add($classification);
105+
}
106+
107+
/**
108+
* @param Classification $classification
109+
*/
110+
public function removeClassification(Classification $classification)
111+
{
112+
if (!$this->classifications->contains($classification)) {
113+
return;
114+
}
115+
116+
$this->classifications->removeElement($classification);
117+
}
62118
}

src/Synchronizes/ContentTypes.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use TapestryCloud\Database\Entities\Classification;
99
use TapestryCloud\Database\Entities\ContentType;
1010
use TapestryCloud\Database\Entities\Environment;
11+
use TapestryCloud\Database\Entities\File;
1112
use TapestryCloud\Database\Entities\Taxonomy;
1213

1314
class ContentTypes
@@ -35,7 +36,7 @@ public function sync(ContentTypeFactory $contentTypeFactory, Environment $enviro
3536
$this->em->persist($record);
3637
$this->em->flush();
3738

38-
$this->syncTaxonomyToContentType($record, $contentType->getTaxonomies());
39+
$this->syncTaxonomyToContentType($record, $contentType->getTaxonomies(), $environment);
3940
continue;
4041
}
4142

@@ -46,11 +47,9 @@ public function sync(ContentTypeFactory $contentTypeFactory, Environment $enviro
4647
/**
4748
* @param ContentType $contentType
4849
* @param TapestryTaxonomy[] $taxonomies
50+
* @param Environment $environment
4951
*/
50-
private function syncTaxonomyToContentType(ContentType $contentType, array $taxonomies) {
51-
52-
$n = $contentType->getTaxonomy();
53-
$p = 1;
52+
private function syncTaxonomyToContentType(ContentType $contentType, array $taxonomies, Environment $environment) {
5453

5554
/** @var TapestryTaxonomy $taxonomy */
5655
foreach($taxonomies as $taxonomy) {
@@ -65,7 +64,6 @@ private function syncTaxonomyToContentType(ContentType $contentType, array $taxo
6564
$this->em->flush();
6665

6766
foreach ($taxonomy->getFileList() as $classification => $files) {
68-
6967
if (! $classificationRecord = $this->em->getRepository(Classification::class)->findOneBy(['name' => $classification])){
7068
$classificationRecord = new Classification();
7169
$classificationRecord->setName($classification);
@@ -74,6 +72,15 @@ private function syncTaxonomyToContentType(ContentType $contentType, array $taxo
7472

7573
$classificationRecord->addTaxonomy($record);
7674
$this->em->flush();
75+
76+
foreach(array_keys($files) as $filename) {
77+
/** @var File $file */
78+
if ($file = $this->em->getRepository(File::class)->findOneBy(['uid' => $filename, 'environment' => $environment->getId()])){
79+
$file->addClassification($classificationRecord);
80+
$this->em->flush();
81+
}
82+
}
83+
7784
}
7885
}
7986
}

tests/PluginTest.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@
1010
use Tapestry\Entities\Project;
1111
use Tapestry\Generator;
1212
use Tapestry\Tapestry;
13+
use TapestryCloud\Database\Entities\Classification;
14+
use TapestryCloud\Database\Entities\ContentType;
15+
use TapestryCloud\Database\Entities\File;
16+
use TapestryCloud\Database\Entities\Taxonomy;
1317

1418
class PluginTest extends \PHPUnit_Framework_TestCase
1519
{
1620

21+
/**
22+
* @var EntityManager
23+
*/
1724
protected static $em;
1825

1926
/**
@@ -35,6 +42,7 @@ public static function setUpBeforeClass()
3542
)
3643
);
3744
$tool = new SchemaTool($em);
45+
$tool->dropDatabase();
3846
$tool->createSchema($em->getMetadataFactory()->getAllMetadata());
3947

4048
self::$em = $em;
@@ -66,8 +74,34 @@ public function testPlugin(){
6674
$project = $tapestry->getContainer()->get(Project::class);
6775
$generator->generate($project, new NullOutput());
6876

69-
/** @var Project $project */
70-
//$project = $tapestry->getContainer()->get(Project::class);
77+
$contentTypes = self::$em->getRepository(ContentType::class)->findAll();
78+
79+
$this->assertCount(2, $contentTypes);
80+
81+
/** @var ContentType $contentType */
82+
foreach ($contentTypes as $contentType) {
83+
$env = $contentType->getEnvironment();
84+
$this->assertEquals('testing', $env->getName());
85+
86+
if ($contentType->getName() === 'blog'){
87+
$taxonomies = $contentType->getTaxonomy();
88+
$this->assertCount(2, $taxonomies);
89+
90+
/** @var Taxonomy $taxonomy */
91+
foreach ($taxonomies as $taxonomy) {
92+
if ($taxonomy->getName() === 'tag') {
93+
$classifications = $taxonomy->getClassifications();
94+
$this->assertCount(2, $classifications);
95+
}
96+
}
97+
}
98+
}
99+
100+
$files = self::$em->getRepository(File::class)->findAll();
101+
$this->assertCount(3, $files);
102+
103+
$classifications = self::$em->getRepository(Classification::class)->findAll();
104+
$this->assertCount(4, $classifications);
71105

72106
$n = 1;
73107
}

0 commit comments

Comments
 (0)