-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSRP_03.php
66 lines (55 loc) · 1.83 KB
/
SRP_03.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
<?php
/**
* In this example, we have two interfaces, UserRepositoryInterface and EmailSenderInterface,
* that define the methods required for creating and updating users in the database and sending emails respectively.
*
* The UserRepository and EmailSender classes implement the corresponding interfaces and contain the logic for their
* respective responsibilities.
*
* The UserRegistration class is responsible for registering a new user and sending a welcome email.
* It takes in objects that implement the UserRepositoryInterface and EmailSenderInterface interfaces as dependencies.
* This adheres to the srp, as each class has only one responsibility and each interface defines a single responsibility.
*
*/
interface UserRepositoryInterface
{
public function create($data);
public function update($id, $data);
}
interface EmailSenderInterface
{
public function sendEmail($to, $subject, $body);
}
class UserRepository implements UserRepositoryInterface
{
public function create($data)
{
// logic for creating a new user in the database
}
public function update($id, $data)
{
// logic for updating an existing user in the database
}
}
class EmailSender implements EmailSenderInterface
{
public function sendEmail($to, $subject, $body)
{
// logic for sending an email to the user
}
}
class UserRegistration
{
private $userRepository;
private $emailSender;
public function __construct(UserRepositoryInterface $userRepository, EmailSenderInterface $emailSender)
{
$this->userRepository = $userRepository;
$this->emailSender = $emailSender;
}
public function registerUser($data)
{
$this->userRepository->create($data);
$this->emailSender->sendEmail($data['email'], 'Welcome!', 'Thank you for registering!');
}
}