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