File tree Expand file tree Collapse file tree 3 files changed +126
-0
lines changed Expand file tree Collapse file tree 3 files changed +126
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace App \Controller ;
4
+
5
+ use App \Observable \User ;
6
+ use App \Observers \UserObserver ;
7
+ use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
8
+ use Symfony \Component \HttpFoundation \Response ;
9
+ use Symfony \Component \Routing \Annotation \Route ;
10
+
11
+ class ObserverDesignPattern extends AbstractController
12
+ {
13
+
14
+ #[Route(path: "/observerDesignPattern " , name: 'observerDesignPattern ' , methods: ['GET ' ])]
15
+ public function welcomeUser (): Response
16
+ {
17
+ $ newUser = new User ();
18
+
19
+ $ newUserObserver = new UserObserver ();
20
+ $ newUser ->attach ($ newUserObserver );
21
+
22
+ $ newUser ->setName ('rahul ' );
23
+
24
+ $ newUser ->detach ($ newUserObserver ); // It won't notify further updates..
25
+
26
+
27
+ return new Response ("Welcome {$ newUser ->getName ()}!, your user id is {$ newUser ->getUserId ()}" );
28
+ }
29
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace App \Observable ;
4
+
5
+ use SplObserver ;
6
+
7
+ class User implements \SplSubject
8
+ {
9
+
10
+ private array $ observers = [];
11
+ private string $ name ;
12
+ private string $ userId ;
13
+ public function __construct ()
14
+ {
15
+ }
16
+
17
+ public function attach (SplObserver $ observer ): void
18
+ {
19
+ $ observerId = spl_object_hash ($ observer );
20
+ $ this ->observers [$ observerId ] = $ observer ;
21
+ }
22
+
23
+ public function detach (SplObserver $ observer ): void
24
+ {
25
+ $ observerId = spl_object_hash ($ observer );
26
+ unset($ this ->observers [$ observerId ]);
27
+ }
28
+
29
+ public function notify (): void
30
+ {
31
+ foreach ($ this ->observers as $ observer ) {
32
+ $ observer ->update ($ this );
33
+ }
34
+ }
35
+
36
+ public function setName ($ name ) {
37
+ $ this ->name = $ name ;
38
+ $ this ->notify ();
39
+ }
40
+
41
+ public function getName ():string
42
+ {
43
+ return $ this ->name ;
44
+ }
45
+
46
+ /**
47
+ * @return string
48
+ */
49
+ public function getUserId (): string
50
+ {
51
+ return $ this ->userId ;
52
+ }
53
+
54
+ /**
55
+ * @param string $userId
56
+ */
57
+ public function setUserId (string $ userId ): void
58
+ {
59
+ $ this ->userId = $ userId ;
60
+ }
61
+
62
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace App \Observers ;
4
+
5
+ use App \Observable \User ;
6
+ use SplSubject ;
7
+
8
+ class UserObserver implements \SplObserver
9
+ {
10
+
11
+ public function update (SplSubject $ subject ): void
12
+ {
13
+ if (!$ subject instanceof User) {
14
+ return ;
15
+ }
16
+
17
+ $ userId = $ this ->generateRandomUserID ();
18
+
19
+ $ subject ->setUserId ($ userId );
20
+ }
21
+
22
+ private function generateRandomUserID (int $ length = 8 ): string
23
+ {
24
+ $ characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ' ;
25
+ $ userID = '' ;
26
+ $ charCount = strlen ($ characters );
27
+
28
+ for ($ i = 0 ; $ i < $ length ; $ i ++) {
29
+ $ randomChar = $ characters [random_int (0 , $ charCount - 1 )];
30
+ $ userID .= $ randomChar ;
31
+ }
32
+
33
+ return $ userID ;
34
+ }
35
+ }
You can’t perform that action at this time.
0 commit comments