Skip to content

Commit 3f93978

Browse files
committed
🚧 files are now inserted correctly albeit only their uid
1 parent f620719 commit 3f93978

5 files changed

Lines changed: 86 additions & 15 deletions

File tree

src/Entities/ContentType.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ public function getEnvironment()
126126
return $this->environment;
127127
}
128128

129+
/**
130+
* @param Environment $environment
131+
*/
129132
public function setEnvironment(Environment $environment)
130133
{
131134
$this->environment = $environment;

src/Entities/File.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,29 @@
88
*/
99
class File
1010
{
11-
/** @Id @Column(type="integer") @GeneratedValue */
11+
/**
12+
* @var int
13+
* @Id @Column(type="integer") @GeneratedValue */
1214
private $id;
1315

14-
/** @ManyToOne(targetEntity="Environment") */
16+
/**
17+
* @var Environment
18+
* @ManyToOne(targetEntity="Environment") */
1519
private $environment;
1620

17-
/** @Column(type="string") */
21+
/**
22+
* @var string
23+
* @Column(type="string") */
1824
private $uid;
1925

26+
public function hydrate(\Tapestry\Entities\File $file, Environment $environment = null) {
27+
$this->uid = $file->getUid();
28+
29+
if (!is_null($environment)) {
30+
$this->setEnvironment($environment);
31+
}
32+
}
33+
2034
/**
2135
* @return string
2236
*/
@@ -29,4 +43,20 @@ public function setUid($uid)
2943
{
3044
$this->uid = $uid;
3145
}
46+
47+
/**
48+
* @return Environment
49+
*/
50+
public function getEnvironment()
51+
{
52+
return $this->environment;
53+
}
54+
55+
/**
56+
* @param Environment $environment
57+
*/
58+
public function setEnvironment(Environment $environment)
59+
{
60+
$this->environment = $environment;
61+
}
3262
}

src/Exporter.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use TapestryCloud\Database\Entities\Environment;
1414
use TapestryCloud\Database\Entities\File;
1515
use TapestryCloud\Database\Synchronizes\ContentTypes;
16+
use TapestryCloud\Database\Synchronizes\Files;
1617

1718
class Exporter {
1819

@@ -70,15 +71,8 @@ public function export(Project $project) {
7071
$this->entityManager->flush();
7172
}
7273

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();
74+
$fileSync = new Files($this->entityManager);
75+
$fileSync->sync($files, $environment);
8276

8377
$contentTypeSync = new ContentTypes($this->entityManager);
8478
$contentTypeSync->sync($contentTypes, $environment);

src/Synchronizes/ContentTypes.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(EntityManagerInterface $em)
2929
public function sync(ContentTypeFactory $contentTypeFactory, Environment $environment) {
3030
foreach($contentTypeFactory->all() as $contentType) {
3131
if (!$record = $this->em->getRepository(ContentType::class)->findOneBy(['name' => $contentType->getName(), 'environment' => $environment->getId()])){
32-
// create and save
32+
// INSERT
3333
$record = new ContentType();
3434
$record->hydrate($contentType, $environment);
3535
$this->em->persist($record);
@@ -39,7 +39,7 @@ public function sync(ContentTypeFactory $contentTypeFactory, Environment $enviro
3939
continue;
4040
}
4141

42-
// update and save
42+
// UPDATE
4343
}
4444
}
4545

@@ -75,7 +75,6 @@ private function syncTaxonomyToContentType(ContentType $contentType, array $taxo
7575
$classificationRecord->addTaxonomy($record);
7676
$this->em->flush();
7777
}
78-
7978
}
8079
}
8180
}

src/Synchronizes/Files.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace TapestryCloud\Database\Synchronizes;
4+
5+
use Doctrine\ORM\EntityManagerInterface;
6+
use Tapestry\Entities\Collections\FlatCollection;
7+
use Tapestry\Entities\File as TapestryFile;
8+
use TapestryCloud\Database\Entities\Environment;
9+
use TapestryCloud\Database\Entities\File;
10+
11+
class Files
12+
{
13+
/**
14+
* @var EntityManagerInterface
15+
*/
16+
private $em;
17+
18+
/**
19+
* ContentTypes constructor.
20+
* @param EntityManagerInterface $em
21+
*/
22+
public function __construct(EntityManagerInterface $em)
23+
{
24+
$this->em = $em;
25+
}
26+
27+
public function sync(FlatCollection $files, Environment $environment) {
28+
29+
/** @var TapestryFile $file */
30+
foreach ($files as $file) {
31+
32+
if (!$record = $this->em->getRepository(File::class)->findOneBy(['uid' => $file->getuid(), 'environment' => $environment->getId()])) {
33+
// INSERT
34+
$record = new File();
35+
$record->hydrate($file, $environment);
36+
}
37+
38+
// UPDATE
39+
40+
$this->em->persist($record);
41+
}
42+
43+
$this->em->flush();
44+
}
45+
}

0 commit comments

Comments
 (0)