Skip to content

Commit 2aeef14

Browse files
committed
#26 -php. Extra
Ejercicio extra sin SRP
1 parent 4f62418 commit 2aeef14

File tree

1 file changed

+254
-1
lines changed

1 file changed

+254
-1
lines changed

Roadmap/26 - SOLID SRP/php/miguelex.php

+254-1
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,257 @@ public function saveUser(Users $users){
7878

7979
// Extra
8080

81-
81+
class Library {
82+
private $books = [];
83+
private $users = [];
84+
private $loans = [];
85+
86+
public function addBook ($title, $author, $copies){
87+
$this->books[] = ['title' => $title, 'author' => $author, 'copies' => $copies];
88+
}
89+
90+
public function addUser ($name, $id, $email){
91+
$this->users[] = ['name' => $name, 'id' => $id, 'email' => $email];
92+
}
93+
94+
public function loanBook($userId, $bookTitle) {
95+
foreach ($this->books as &$book) {
96+
if ($book['title'] === $bookTitle && $book['copies'] > 0) {
97+
$this->loans[] = [
98+
'userId' => $userId,
99+
'bookTitle' => $bookTitle,
100+
'date' => date('Y-m-d H:i:s')
101+
];
102+
$book['copies']--;
103+
return "El libro ha sido prestado.";
104+
}
105+
}
106+
return "El libro no está disponible.";
107+
}
108+
109+
public function returnBook($userId, $bookTitle) {
110+
foreach ($this->loans as $key => $loan) {
111+
if ($loan['userId'] === $userId && $loan['bookTitle'] === $bookTitle) {
112+
unset($this->loans[$key]);
113+
foreach ($this->books as &$book) {
114+
if ($book['title'] === $bookTitle) {
115+
$book['copies']++;
116+
return "El libro ha sido devuelto.";
117+
}
118+
}
119+
}
120+
}
121+
return "No se encontró el préstamo.";
122+
}
123+
124+
public function getBooks(){
125+
return $this->books;
126+
}
127+
128+
public function getUsers(){
129+
return $this->users;
130+
}
131+
132+
public function getLoans(){
133+
return $this->loans;
134+
}
135+
}
136+
137+
$myLibrary = new Library();
138+
139+
do {
140+
echo "\n\nVamos a mostrar un ejemplo de una clase que no cumple el SRP. En este caso es una clase que gestiona una biblioteca\n\n";
141+
echo "";
142+
echo "1. Añadir libro\n";
143+
echo "2. Añadir usuario\n";
144+
echo "3. Prestar libro\n";
145+
echo "4. Devolver libro\n";
146+
echo "5. Mostrar libros\n";
147+
echo "6. Mostrar usuarios\n";
148+
echo "7. Mostrar préstamos\n";
149+
echo "8. Salir\n";
150+
echo "";
151+
echo "Elija una opción: ";
152+
153+
$option = readline();
154+
echo "";
155+
156+
switch ($option) {
157+
case 1:
158+
echo "Título: ";
159+
$title = readline();
160+
echo "Autor: ";
161+
$author = readline();
162+
echo "Copias: ";
163+
$copies = readline();
164+
$myLibrary->addBook($title, $author, $copies);
165+
break;
166+
case 2:
167+
echo "Nombre: ";
168+
$name = readline();
169+
echo "ID: ";
170+
$id = readline();
171+
echo "Email: ";
172+
$email = readline();
173+
$myLibrary->addUser($name, $id, $email);
174+
break;
175+
case 3:
176+
echo "ID del usuario: ";
177+
$userId = readline();
178+
echo "Título del libro: ";
179+
$bookTitle = readline();
180+
echo $myLibrary->loanBook($userId, $bookTitle) . "\n";
181+
break;
182+
case 4:
183+
echo "ID del usuario: ";
184+
$userId = readline();
185+
echo "Título del libro: ";
186+
$bookTitle = readline();
187+
echo $myLibrary->returnBook($userId, $bookTitle) . "\n";
188+
break;
189+
case 5:
190+
print_r($myLibrary->getBooks());
191+
break;
192+
case 6:
193+
print_r($myLibrary->getUsers());
194+
break;
195+
case 7:
196+
print_r($myLibrary->getLoans());
197+
break;
198+
}
199+
200+
} while (($option != 8));
201+
202+
203+
// Vamos ahora a refactorizar para aplicar el SRP. Ahora tnedremos una clase para los libros, otra para los autores y otra para los prestamos, asi como sus respectivos managers
204+
205+
class Books {
206+
private $title;
207+
private $author;
208+
private $copies;
209+
210+
public function __construct($title, $author, $copies){
211+
$this->title = $title;
212+
$this->author = $author;
213+
$this->copies = $copies;
214+
}
215+
216+
public function getTitle(){
217+
return $this->title;
218+
}
219+
220+
public function getAuthor(){
221+
return $this->author;
222+
}
223+
224+
public function getCopies(){
225+
return $this->copies;
226+
}
227+
228+
public function loanBook(){
229+
$this->copies--;
230+
}
231+
232+
public function returnBook(){
233+
$this->copies++;
234+
}
235+
}
236+
237+
class UsersLibrary {
238+
private $name;
239+
private $id;
240+
private $email;
241+
242+
public function __construct($name, $id, $email){
243+
$this->name = $name;
244+
$this->id = $id;
245+
$this->email = $email;
246+
}
247+
248+
public function getName(){
249+
return $this->name;
250+
}
251+
252+
public function getId(){
253+
return $this->id;
254+
}
255+
256+
public function getEmail(){
257+
return $this->email;
258+
}
259+
260+
}
261+
262+
class Loan {
263+
private $userId;
264+
private $bookTitle;
265+
private $date;
266+
267+
public function __construct($userId, $bookTitle){
268+
$this->userId = $userId;
269+
$this->bookTitle = $bookTitle;
270+
$this->date = date('Y-m-d H:i:s');
271+
}
272+
273+
public function getUserId(){
274+
return $this->userId;
275+
}
276+
277+
public function getBookTitle(){
278+
return $this->bookTitle;
279+
}
280+
281+
public function getDate(){
282+
return $this->date;
283+
}
284+
}
285+
286+
class BookManager {
287+
private $books = [];
288+
289+
public function addBook(Books $book){
290+
$this->books[] = $book;
291+
}
292+
293+
public function getBooks(){
294+
return $this->books;
295+
}
296+
}
297+
298+
class UserManager{
299+
private $users = [];
300+
301+
public function addUser(UsersLibrary $user){
302+
$this->users[] = $user;
303+
}
304+
305+
public function getUsers(){
306+
return $this->users;
307+
}
308+
}
309+
310+
class LoanManager{
311+
private $loans = [];
312+
313+
public function loanBook(UsersLibrary $user, Books $book){
314+
$book->loanBook();
315+
$this->loans[] = new Loan($user->getId(), $book->getTitle());
316+
}
317+
318+
public function returnBook(UsersLibrary $user, Books $book){
319+
$book->returnBook();
320+
foreach ($this->loans as $key => $loan) {
321+
if ($loan->getUserId() === $user->getId() && $loan->getBookTitle() === $book->getTitle()) {
322+
unset($this->loans[$key]);
323+
}
324+
}
325+
}
326+
327+
public function getLoans(){
328+
return $this->loans;
329+
}
330+
}
331+
332+
$myBookManager = new BookManager();
333+
$myUserManager = new UserManager();
334+
$myLoanManager = new LoanManager();

0 commit comments

Comments
 (0)