Skip to content

Commit 4184881

Browse files
authored
Merge pull request #339 from dotkernel/issue-325
Issue #325: Implemented enums in database
2 parents 5c998ce + 3d43912 commit 4184881

33 files changed

+311
-134
lines changed

config/autoload/doctrine.global.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
declare(strict_types=1);
44

5+
use Api\Admin\DBAL\Types\AdminStatusEnumType;
56
use Api\App\Entity\EntityListenerResolver;
7+
use Api\User\DBAL\Types\UserResetPasswordStatusEnumType;
8+
use Api\User\DBAL\Types\UserStatusEnumType;
69
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
710
use Dot\Cache\Adapter\ArrayAdapter;
811
use Dot\Cache\Adapter\FilesystemAdapter;
@@ -27,9 +30,12 @@
2730
],
2831
],
2932
'types' => [
30-
UuidType::NAME => UuidType::class,
31-
UuidBinaryType::NAME => UuidBinaryType::class,
32-
UuidBinaryOrderedTimeType::NAME => UuidBinaryOrderedTimeType::class,
33+
UuidType::NAME => UuidType::class,
34+
UuidBinaryType::NAME => UuidBinaryType::class,
35+
UuidBinaryOrderedTimeType::NAME => UuidBinaryOrderedTimeType::class,
36+
AdminStatusEnumType::NAME => AdminStatusEnumType::class,
37+
UserStatusEnumType::NAME => UserStatusEnumType::class,
38+
UserResetPasswordStatusEnumType::NAME => UserResetPasswordStatusEnumType::class,
3339
],
3440
'fixtures' => getcwd() . '/data/doctrine/fixtures',
3541
'configuration' => [

data/doctrine/fixtures/AdminLoader.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,35 @@
66

77
use Api\Admin\Entity\Admin;
88
use Api\Admin\Entity\AdminRole;
9-
use Api\User\Entity\User;
9+
use Api\Admin\Enum\AdminStatusEnum;
1010
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
1111
use Doctrine\Common\DataFixtures\FixtureInterface;
1212
use Doctrine\Persistence\ObjectManager;
1313

14-
/**
15-
* Class AdminLoader
16-
* @package Api\Fixtures
17-
*/
14+
use function assert;
15+
1816
class AdminLoader implements FixtureInterface, DependentFixtureInterface
1917
{
2018
public function load(ObjectManager $manager): void
2119
{
2220
$adminRoleRepository = $manager->getRepository(AdminRole::class);
2321

24-
/** @var AdminRole $adminRole */
2522
$adminRole = $adminRoleRepository->findOneBy([
2623
'name' => AdminRole::ROLE_ADMIN,
2724
]);
25+
assert($adminRole instanceof AdminRole);
2826

29-
/** @var AdminRole $superUserRole */
3027
$superUserRole = $adminRoleRepository->findOneBy([
3128
'name' => AdminRole::ROLE_SUPERUSER,
3229
]);
30+
assert($superUserRole instanceof AdminRole);
3331

3432
$admin = (new Admin())
3533
->setIdentity('admin')
3634
->usePassword('dotkernel')
3735
->setFirstName('DotKernel')
3836
->setLastName('Admin')
39-
->setStatus(User::STATUS_ACTIVE)
37+
->setStatus(AdminStatusEnum::Active)
4038
->addRole($adminRole)
4139
->addRole($superUserRole);
4240

data/doctrine/fixtures/AdminRoleLoader.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
use Doctrine\Common\DataFixtures\FixtureInterface;
99
use Doctrine\Persistence\ObjectManager;
1010

11-
/**
12-
* Class AdminRoleLoader
13-
* @package Api\Fixtures
14-
*/
1511
class AdminRoleLoader implements FixtureInterface
1612
{
1713
public function load(ObjectManager $manager): void

data/doctrine/fixtures/OAuthClientLoader.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
use Doctrine\Common\DataFixtures\FixtureInterface;
1010
use Doctrine\Persistence\ObjectManager;
1111

12-
/**
13-
* Class OAuthClientLoader
14-
* @package Api\Fixtures
15-
*/
1612
class OAuthClientLoader extends AbstractFixture implements FixtureInterface
1713
{
1814
public function load(ObjectManager $manager): void

data/doctrine/fixtures/OAuthScopeLoader.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
use Doctrine\Common\DataFixtures\FixtureInterface;
99
use Doctrine\Persistence\ObjectManager;
1010

11-
/**
12-
* Class OAuthScopeLoader
13-
* @package Api\Fixtures
14-
*/
1511
class OAuthScopeLoader implements FixtureInterface
1612
{
1713
public function load(ObjectManager $manager): void

data/doctrine/fixtures/UserLoader.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,33 @@
77
use Api\User\Entity\User;
88
use Api\User\Entity\UserDetail;
99
use Api\User\Entity\UserRole;
10+
use Api\User\Enum\UserStatusEnum;
1011
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
1112
use Doctrine\Common\DataFixtures\FixtureInterface;
1213
use Doctrine\Persistence\ObjectManager;
1314

14-
/**
15-
* Class UserLoader
16-
* @package Api\Fixtures
17-
*/
15+
use function assert;
16+
1817
class UserLoader implements FixtureInterface, DependentFixtureInterface
1918
{
2019
public function load(ObjectManager $manager): void
2120
{
2221
$userRoleRepository = $manager->getRepository(UserRole::class);
2322

24-
/** @var UserRole $guestRole */
2523
$guestRole = $userRoleRepository->findOneBy([
2624
'name' => UserRole::ROLE_GUEST,
2725
]);
26+
assert($guestRole instanceof UserRole);
2827

29-
/** @var UserRole $userRole */
3028
$userRole = $userRoleRepository->findOneBy([
3129
'name' => UserRole::ROLE_USER,
3230
]);
31+
assert($userRole instanceof UserRole);
3332

3433
$user = (new User())
3534
->setIdentity('[email protected]')
3635
->usePassword('dotkernel')
37-
->setStatus(User::STATUS_ACTIVE)
36+
->setStatus(UserStatusEnum::Active)
3837
->setIsDeleted(false)
3938
->setHash(User::generateHash())
4039
->addRole($guestRole)

data/doctrine/fixtures/UserRoleLoader.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
use Doctrine\Common\DataFixtures\FixtureInterface;
99
use Doctrine\Persistence\ObjectManager;
1010

11-
/**
12-
* Class UserRoleLoader
13-
* @package Api\Fixtures
14-
*/
1511
class UserRoleLoader implements FixtureInterface
1612
{
1713
public function load(ObjectManager $manager): void

data/doctrine/migrations/Version20240613153602.php renamed to data/doctrine/migrations/Version20241030082958.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* Auto-generated Migration: Please modify to your needs!
1212
*/
13-
final class Version20240613153602 extends AbstractMigration
13+
final class Version20241030082958 extends AbstractMigration
1414
{
1515
public function getDescription(): string
1616
{
@@ -20,7 +20,7 @@ public function getDescription(): string
2020
public function up(Schema $schema): void
2121
{
2222
// this up() migration is auto-generated, please modify it to your needs
23-
$this->addSql('CREATE TABLE admin (uuid BINARY(16) NOT NULL, identity VARCHAR(100) NOT NULL, firstName VARCHAR(191) NOT NULL, lastName VARCHAR(191) NOT NULL, password VARCHAR(100) NOT NULL, status VARCHAR(20) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_880E0D766A95E9C4 (identity), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
23+
$this->addSql('CREATE TABLE admin (uuid BINARY(16) NOT NULL, identity VARCHAR(100) NOT NULL, firstName VARCHAR(191) NOT NULL, lastName VARCHAR(191) NOT NULL, password VARCHAR(100) NOT NULL, status ENUM(\'active\', \'inactive\') DEFAULT \'active\' NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_880E0D766A95E9C4 (identity), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
2424
$this->addSql('CREATE TABLE admin_roles (userUuid BINARY(16) NOT NULL, roleUuid BINARY(16) NOT NULL, INDEX IDX_1614D53DD73087E9 (userUuid), INDEX IDX_1614D53D88446210 (roleUuid), PRIMARY KEY(userUuid, roleUuid)) DEFAULT CHARACTER SET utf8mb4');
2525
$this->addSql('CREATE TABLE admin_role (uuid BINARY(16) NOT NULL, name VARCHAR(30) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_7770088A5E237E06 (name), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
2626
$this->addSql('CREATE TABLE oauth_access_tokens (id INT UNSIGNED AUTO_INCREMENT NOT NULL, user_id VARCHAR(25) DEFAULT NULL, token VARCHAR(100) NOT NULL, revoked TINYINT(1) DEFAULT 0 NOT NULL, expires_at DATETIME NOT NULL, client_id INT UNSIGNED DEFAULT NULL, INDEX IDX_CA42527C19EB6921 (client_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4');
@@ -30,11 +30,11 @@ public function up(Schema $schema): void
3030
$this->addSql('CREATE TABLE oauth_clients (id INT UNSIGNED AUTO_INCREMENT NOT NULL, name VARCHAR(40) NOT NULL, secret VARCHAR(100) DEFAULT NULL, redirect VARCHAR(191) NOT NULL, revoked TINYINT(1) DEFAULT 0 NOT NULL, isConfidential TINYINT(1) DEFAULT 0 NOT NULL, user_id BINARY(16) DEFAULT NULL, INDEX IDX_13CE8101A76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4');
3131
$this->addSql('CREATE TABLE oauth_refresh_tokens (id INT UNSIGNED AUTO_INCREMENT NOT NULL, revoked TINYINT(1) DEFAULT 0 NOT NULL, expires_at DATETIME NOT NULL, access_token_id INT UNSIGNED DEFAULT NULL, INDEX IDX_5AB6872CCB2688 (access_token_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4');
3232
$this->addSql('CREATE TABLE oauth_scopes (id INT UNSIGNED AUTO_INCREMENT NOT NULL, scope VARCHAR(191) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4');
33-
$this->addSql('CREATE TABLE user (uuid BINARY(16) NOT NULL, identity VARCHAR(191) NOT NULL, password VARCHAR(191) NOT NULL, status VARCHAR(20) NOT NULL, isDeleted TINYINT(1) NOT NULL, hash VARCHAR(64) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_8D93D6496A95E9C4 (identity), UNIQUE INDEX UNIQ_8D93D649D1B862B8 (hash), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
33+
$this->addSql('CREATE TABLE user (uuid BINARY(16) NOT NULL, identity VARCHAR(191) NOT NULL, password VARCHAR(191) NOT NULL, status ENUM(\'active\', \'pending\') DEFAULT \'pending\' NOT NULL, isDeleted TINYINT(1) NOT NULL, hash VARCHAR(64) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_8D93D6496A95E9C4 (identity), UNIQUE INDEX UNIQ_8D93D649D1B862B8 (hash), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
3434
$this->addSql('CREATE TABLE user_roles (userUuid BINARY(16) NOT NULL, roleUuid BINARY(16) NOT NULL, INDEX IDX_54FCD59FD73087E9 (userUuid), INDEX IDX_54FCD59F88446210 (roleUuid), PRIMARY KEY(userUuid, roleUuid)) DEFAULT CHARACTER SET utf8mb4');
3535
$this->addSql('CREATE TABLE user_avatar (uuid BINARY(16) NOT NULL, name VARCHAR(191) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, userUuid BINARY(16) DEFAULT NULL, UNIQUE INDEX UNIQ_73256912D73087E9 (userUuid), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
3636
$this->addSql('CREATE TABLE user_detail (uuid BINARY(16) NOT NULL, firstName VARCHAR(191) DEFAULT NULL, lastName VARCHAR(191) DEFAULT NULL, email VARCHAR(191) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, userUuid BINARY(16) DEFAULT NULL, UNIQUE INDEX UNIQ_4B5464AED73087E9 (userUuid), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
37-
$this->addSql('CREATE TABLE user_reset_password (uuid BINARY(16) NOT NULL, expires DATETIME NOT NULL, hash VARCHAR(64) NOT NULL, status VARCHAR(20) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, userUuid BINARY(16) DEFAULT NULL, UNIQUE INDEX UNIQ_D21DE3BCD1B862B8 (hash), INDEX IDX_D21DE3BCD73087E9 (userUuid), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
37+
$this->addSql('CREATE TABLE user_reset_password (uuid BINARY(16) NOT NULL, expires DATETIME NOT NULL, hash VARCHAR(64) NOT NULL, status ENUM(\'completed\', \'requested\') DEFAULT \'requested\' NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, userUuid BINARY(16) DEFAULT NULL, UNIQUE INDEX UNIQ_D21DE3BCD1B862B8 (hash), INDEX IDX_D21DE3BCD73087E9 (userUuid), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
3838
$this->addSql('CREATE TABLE user_role (uuid BINARY(16) NOT NULL, name VARCHAR(20) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_2DE8C6A35E237E06 (name), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
3939
$this->addSql('ALTER TABLE admin_roles ADD CONSTRAINT FK_1614D53DD73087E9 FOREIGN KEY (userUuid) REFERENCES admin (uuid)');
4040
$this->addSql('ALTER TABLE admin_roles ADD CONSTRAINT FK_1614D53D88446210 FOREIGN KEY (roleUuid) REFERENCES admin_role (uuid)');
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Api\Admin\DBAL\Types;
6+
7+
use Api\Admin\Enum\AdminStatusEnum;
8+
use Api\App\DBAL\Types\AbstractEnumType;
9+
10+
class AdminStatusEnumType extends AbstractEnumType
11+
{
12+
public const NAME = 'admin_status_enum';
13+
14+
protected function getEnumClass(): string
15+
{
16+
return AdminStatusEnum::class;
17+
}
18+
19+
public function getName(): string
20+
{
21+
return self::NAME;
22+
}
23+
}

src/Admin/src/Entity/Admin.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Api\Admin\Entity;
66

7+
use Api\Admin\Enum\AdminStatusEnum;
78
use Api\Admin\Repository\AdminRepository;
89
use Api\App\Entity\AbstractEntity;
910
use Api\App\Entity\PasswordTrait;
@@ -22,13 +23,6 @@ class Admin extends AbstractEntity implements UserEntityInterface
2223
use PasswordTrait;
2324
use TimestampsTrait;
2425

25-
public const STATUS_ACTIVE = 'active';
26-
public const STATUS_INACTIVE = 'inactive';
27-
public const STATUSES = [
28-
self::STATUS_ACTIVE,
29-
self::STATUS_INACTIVE,
30-
];
31-
3226
#[ORM\Column(name: "identity", type: "string", length: 100, unique: true)]
3327
protected string $identity = '';
3428

@@ -41,8 +35,8 @@ class Admin extends AbstractEntity implements UserEntityInterface
4135
#[ORM\Column(name: "password", type: "string", length: 100)]
4236
protected string $password = '';
4337

44-
#[ORM\Column(name: "status", type: "string", length: 20)]
45-
protected string $status = self::STATUS_ACTIVE;
38+
#[ORM\Column(type: 'admin_status_enum', options: ['default' => AdminStatusEnum::Active])]
39+
protected AdminStatusEnum $status = AdminStatusEnum::Active;
4640

4741
#[ORM\ManyToMany(targetEntity: AdminRole::class)]
4842
#[ORM\JoinTable(name: "admin_roles")]
@@ -122,12 +116,12 @@ public function setPassword(string $password): self
122116
return $this;
123117
}
124118

125-
public function getStatus(): string
119+
public function getStatus(): AdminStatusEnum
126120
{
127121
return $this->status;
128122
}
129123

130-
public function setStatus(string $status): self
124+
public function setStatus(AdminStatusEnum $status): self
131125
{
132126
$this->status = $status;
133127

@@ -178,21 +172,21 @@ public function hasRoles(): bool
178172

179173
public function activate(): self
180174
{
181-
$this->status = self::STATUS_ACTIVE;
175+
$this->status = AdminStatusEnum::Active;
182176

183177
return $this;
184178
}
185179

186180
public function deactivate(): self
187181
{
188-
$this->status = self::STATUS_INACTIVE;
182+
$this->status = AdminStatusEnum::Inactive;
189183

190184
return $this;
191185
}
192186

193187
public function isActive(): bool
194188
{
195-
return $this->status === self::STATUS_ACTIVE;
189+
return $this->status === AdminStatusEnum::Active;
196190
}
197191

198192
public function getIdentifier(): string

0 commit comments

Comments
 (0)