Skip to content

Commit 36d5868

Browse files
refactor: identify entity functions
1 parent 1207ad5 commit 36d5868

File tree

4 files changed

+43
-34
lines changed

4 files changed

+43
-34
lines changed

src/api/category/Category.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
namespace RestJS\Api\Category;
44

55
use Doctrine\ORM\Mapping as ORM;
6-
use RestJS\Class\GetterAndSetter;
6+
use RestJS\Class\Entity;
77

88
#[ORM\Entity]
99
#[ORM\Table('category')]
10-
class Category extends GetterAndSetter {
10+
class Category extends Entity {
1111

1212
#[ORM\Id]
1313
#[ORM\Column, ORM\GeneratedValue]

src/api/user/User.php

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
namespace RestJS\Api\User;
44

55
use Doctrine\ORM\Mapping as ORM;
6-
use Doctrine\ORM\Event as Event;
7-
use Firebase\JWT\JWT;
8-
use RestJS\Class\GetterAndSetter;
6+
use RestJS\Class\AuthEntity;
97

108
#[ORM\Entity]
119
#[ORM\Table('user')]
1210
#[ORM\HasLifecycleCallbacks]
13-
class User extends GetterAndSetter {
11+
class User extends AuthEntity {
1412

1513
#[ORM\Id]
1614
#[ORM\Column, ORM\GeneratedValue]
@@ -36,30 +34,4 @@ class User extends GetterAndSetter {
3634

3735
#[ORM\Column(name: "updated_at", insertable: false, updatable: false)]
3836
public string $updatedAt;
39-
40-
#[ORM\PrePersist]
41-
public function prePersist() {
42-
$this->password = password_hash($this->password, PASSWORD_BCRYPT);
43-
}
44-
45-
#[ORM\PreUpdate]
46-
public function preUpdate(Event\PreUpdateEventArgs $event) {
47-
$passwordModify = $event->hasChangedField('password') ?? false;
48-
49-
if ($passwordModify)
50-
$this->password = password_hash($this->password, PASSWORD_BCRYPT);
51-
}
52-
53-
/** Verify Password Function */
54-
public function verifyPassword($password) {
55-
return password_verify($password, $this->password);
56-
}
57-
58-
/** Generate Access Token Function */
59-
public function generateAccessToken() {
60-
return JWT::encode([
61-
'id' => $this->id,
62-
'username' => $this->username
63-
], $_ENV['ACCESS_TOKEN_SECRET'], 'HS256');
64-
}
6537
}

src/class/AuthEntity.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
declare(strict_types=1);
3+
namespace RestJS\Class;
4+
5+
use Firebase\JWT\JWT;
6+
use Doctrine\ORM\Mapping as ORM;
7+
use Doctrine\ORM\Event as Event;
8+
9+
/** Authorization Entity Functions */
10+
class AuthEntity extends Entity {
11+
12+
#[ORM\PrePersist]
13+
public function prePersist() {
14+
$this->password = password_hash($this->password, PASSWORD_BCRYPT);
15+
}
16+
17+
#[ORM\PreUpdate]
18+
public function preUpdate(Event\PreUpdateEventArgs $event) {
19+
$passwordModify = $event->hasChangedField('password') ?? false;
20+
21+
if ($passwordModify)
22+
$this->password = password_hash($this->password, PASSWORD_BCRYPT);
23+
}
24+
25+
/** Verify Password Function */
26+
public function verifyPassword($password) {
27+
return password_verify($password, $this->password);
28+
}
29+
30+
/** Generate Access Token Function */
31+
public function generateAccessToken() {
32+
return JWT::encode([
33+
'id' => $this->id,
34+
'username' => $this->username
35+
], $_ENV['ACCESS_TOKEN_SECRET'], 'HS256');
36+
}
37+
}

src/class/getterandsetter.php renamed to src/class/Entity.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
declare(strict_types=1);
33
namespace RestJS\Class;
44

5-
/** Getter and Setter Functions */
6-
class GetterAndSetter {
5+
/** Entity Functions */
6+
class Entity {
77

88
/** Get Value by Column */
99
public function __get($key) {

0 commit comments

Comments
 (0)