This repository has been archived by the owner on Jun 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFroshEnvironmentNotice.php
97 lines (85 loc) · 2.63 KB
/
FroshEnvironmentNotice.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php declare(strict_types=1);
namespace FroshEnvironmentNotice;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Tools\SchemaTool;
use FroshEnvironmentNotice\Models\Notice;
use FroshEnvironmentNotice\Models\Slot;
use Shopware\Components\Model\ModelEntity;
use Shopware\Components\Model\ModelManager;
use Shopware\Components\Plugin;
use Shopware\Components\Plugin\Context\InstallContext;
use Shopware\Components\Plugin\Context\UninstallContext;
use Shopware\Components\Plugin\Context\UpdateContext;
class FroshEnvironmentNotice extends Plugin
{
/**
* @var string[]
*/
private $modelClasses = [
Notice::class,
Slot::class,
];
/**
* @param InstallContext $context
*/
public function install(InstallContext $context)
{
parent::install($context);
$this->installSchema();
$this->seed();
}
/**
* @param UpdateContext $context
*/
public function update(UpdateContext $context)
{
parent::update($context);
$this->installSchema();
$this->seed();
}
/**
* @param UninstallContext $context
*/
public function uninstall(UninstallContext $context)
{
parent::uninstall($context);
$this->uninstallSchema();
}
private function installSchema()
{
(new SchemaTool($this->getModelManager()))->updateSchema($this->getModelMetadata(), true);
}
private function uninstallSchema()
{
(new SchemaTool($this->getModelManager()))->dropSchema($this->getModelMetadata());
}
private function seed()
{
$amountOfSlots = $this->getModelManager()->getRepository(Slot::class)->createQueryBuilder('slot')->getMaxResults();
if (!$amountOfSlots) {
$datas = json_decode(file_get_contents($this->getPath() . '/Resources/seeds/slots.json'), true);
/** @var ModelEntity[] $models */
$models = array_map(function ($data) {
return (new Slot())->fromArray($data);
}, $datas);
array_walk($models, [$this->getModelManager(), 'persist']);
/** @noinspection PhpUnhandledExceptionInspection */
$this->getModelManager()->flush($models);
}
}
/**
* @return ClassMetadata[]
*/
private function getModelMetadata(): array
{
return array_map([$this->getModelManager(), 'getClassMetadata'], $this->modelClasses);
}
/**
* @return ModelManager
*/
private function getModelManager(): ModelManager
{
/* @noinspection PhpIncompatibleReturnTypeInspection */
return $this->container->get('models');
}
}