|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/** |
|
4
|
|
|
* /src/DataFixtures/ORM/LoadApiKeyData.php |
|
5
|
|
|
* |
|
6
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace App\DataFixtures\ORM; |
|
9
|
|
|
|
|
10
|
|
|
use App\Entity\ApiKey; |
|
11
|
|
|
use App\Entity\UserGroup; |
|
12
|
|
|
use App\Security\RolesService; |
|
13
|
|
|
use App\Security\RolesServiceInterface; |
|
14
|
|
|
use Doctrine\Common\DataFixtures\AbstractFixture; |
|
15
|
|
|
use Doctrine\Common\DataFixtures\OrderedFixtureInterface; |
|
16
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class LoadApiKeyData |
|
22
|
|
|
* |
|
23
|
|
|
* @package App\DataFixtures\ORM |
|
24
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class LoadApiKeyData extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var ContainerInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $container; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var ObjectManager |
|
35
|
|
|
*/ |
|
36
|
|
|
private $manager; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var RolesServiceInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $roles; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param ContainerInterface|null $container |
|
45
|
|
|
*/ |
|
46
|
|
|
public function setContainer(ContainerInterface $container = null): void |
|
47
|
|
|
{ |
|
48
|
|
|
$this->container = $container; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Load data fixtures with the passed EntityManager |
|
53
|
|
|
* |
|
54
|
|
|
* @param ObjectManager $manager |
|
55
|
|
|
* |
|
56
|
|
|
* @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException |
|
57
|
|
|
* @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException |
|
58
|
|
|
*/ |
|
59
|
|
|
public function load(ObjectManager $manager): void |
|
60
|
|
|
{ |
|
61
|
|
|
$this->manager = $manager; |
|
62
|
|
|
$this->roles = $this->container->get(RolesService::class); |
|
63
|
|
|
|
|
64
|
|
|
// Create entities |
|
65
|
|
|
\array_map([$this, 'createApiKey'], $this->roles->getRoles()); |
|
66
|
|
|
|
|
67
|
|
|
$this->createApiKey(); |
|
68
|
|
|
|
|
69
|
|
|
// Flush database changes |
|
70
|
|
|
$this->manager->flush(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get the order of this fixture |
|
75
|
|
|
* |
|
76
|
|
|
* @return integer |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getOrder(): int |
|
79
|
|
|
{ |
|
80
|
|
|
return 4; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $role |
|
85
|
|
|
*/ |
|
86
|
|
|
private function createApiKey(string $role = null): void |
|
87
|
|
|
{ |
|
88
|
|
|
// Create new entity |
|
89
|
|
|
$entity = new ApiKey(); |
|
90
|
|
|
$entity->setDescription('ApiKey Description: ' . ($role === null ? '' : $this->roles->getShort($role))); |
|
91
|
|
|
|
|
92
|
|
|
$suffix = ''; |
|
93
|
|
|
|
|
94
|
|
|
if ($role !== null) { |
|
95
|
|
|
/** @var UserGroup $userGroup */ |
|
96
|
|
|
$userGroup = $this->getReference('UserGroup-' . $this->roles->getShort($role)); |
|
97
|
|
|
|
|
98
|
|
|
$entity->addUserGroup($userGroup); |
|
99
|
|
|
|
|
100
|
|
|
$suffix = '-' . $this->roles->getShort($role); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// Persist entity |
|
104
|
|
|
$this->manager->persist($entity); |
|
105
|
|
|
|
|
106
|
|
|
// Create reference for later usage |
|
107
|
|
|
$this->addReference('ApiKey' . $suffix, $entity); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|