forked from moodlehq/refactoring-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TASK-1: Major refactor to implement design patterns of Repository, Bu…
…ilder, Composite
- Loading branch information
Andrew Gosali
committed
Oct 2, 2024
1 parent
275eeea
commit ba5c784
Showing
40 changed files
with
2,832 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea | ||
vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace Builder; | ||
|
||
use Model\Course; | ||
use Model\School; | ||
use Model\Student; | ||
use Model\Teacher; | ||
|
||
class SchoolBuilder | ||
{ | ||
private School $school; | ||
|
||
public function __construct() | ||
{ | ||
$this->school = new School(); | ||
} | ||
|
||
/** | ||
* @param array $data | ||
* @return $this | ||
*/ | ||
public function addCoursesFromData(array $data): self | ||
{ | ||
$classes = $data['classes']; | ||
|
||
foreach ($classes as $class) { | ||
$this->school->addCourse( | ||
new Course( | ||
$class['id'], | ||
$class['name'], | ||
$class['location'] ?? '' | ||
) | ||
); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param array $data | ||
* @return $this | ||
*/ | ||
public function addUsersFromData(array $data): self | ||
{ | ||
$users = $data['users']; | ||
$courses = $this->school->getCourses(); | ||
|
||
foreach ($users as $user) { | ||
$role = $user['role'] ?? ''; | ||
|
||
foreach ($user['classes'] as $classEnrolment) { | ||
$courseId = $classEnrolment['id']; | ||
|
||
if (!$courses[$courseId]) { | ||
continue; | ||
} | ||
|
||
if ($role === 'Teacher') { | ||
$courses[$courseId]->addTeacher(new Teacher($user['name'], $user['email'] ?? '')); | ||
} else { | ||
$courses[$courseId]->addStudent(new Student($user['name'], $user['email'] ?? '')); | ||
} | ||
} | ||
} | ||
|
||
$this->school->setCourses($courses); | ||
return $this; | ||
} | ||
|
||
public function build(): School | ||
{ | ||
return $this->school; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
namespace Model; | ||
|
||
class Course implements SchoolComponent | ||
{ | ||
private string $id; | ||
private string $name; | ||
private string $location; | ||
|
||
/** @var Student[] */ | ||
private array $students = []; | ||
|
||
/** @var Teacher[] */ | ||
private array $teachers = []; | ||
|
||
/** | ||
* @param string $id | ||
* @param string $name | ||
* @param string $location | ||
*/ | ||
public function __construct(string $id, string $name, string $location = '') | ||
{ | ||
$this->id = $id; | ||
$this->name = $name; | ||
$this->location = $location; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getId(): string | ||
{ | ||
return $this->id; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getLocation(): string | ||
{ | ||
return $this->location; | ||
} | ||
|
||
/** | ||
* @return Student[] | ||
*/ | ||
public function getStudents(): array | ||
{ | ||
return $this->students; | ||
} | ||
|
||
/** | ||
* @return Teacher[] | ||
*/ | ||
public function getTeachers(): array | ||
{ | ||
return $this->teachers; | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @return $this | ||
*/ | ||
public function setName(string $name): self | ||
{ | ||
$this->name = $name; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $location | ||
* @return $this | ||
*/ | ||
public function setLocation(string $location): self | ||
{ | ||
$this->location = $location; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param Student $student | ||
* @return $this | ||
*/ | ||
public function addStudent(Student $student): self | ||
{ | ||
$this->students[] = $student; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param Teacher $teacher | ||
* @return $this | ||
*/ | ||
public function addTeacher(Teacher $teacher): self | ||
{ | ||
$this->teachers[] = $teacher; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function formatForPrint(): array | ||
{ | ||
return [ | ||
'id' => $this->id, | ||
'name' => $this->name, | ||
'location' => $this->location, | ||
]; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function toString(): string | ||
{ | ||
return $this->name . ': ' . $this->location; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
namespace Model; | ||
|
||
class School implements SchoolComponent | ||
{ | ||
/** @var Course[] */ | ||
private array $courses = []; | ||
|
||
/** | ||
* @return Course[] | ||
*/ | ||
public function getCourses(): array | ||
{ | ||
return $this->courses; | ||
} | ||
|
||
/** | ||
* @param array $courses | ||
* @return $this | ||
*/ | ||
public function setCourses(array $courses): self | ||
{ | ||
$this->courses = $courses; | ||
return $this; | ||
} | ||
|
||
public function addCourse(Course $course): self | ||
{ | ||
$this->courses[$course->getId()] = $course; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function formatForPrint(): array | ||
{ | ||
return array_map(function ($course) { | ||
$students = array_map(function ($student) { | ||
return $student->toString(); | ||
}, $course->getStudents()); | ||
|
||
$teachers = array_map(function ($student) { | ||
return $student->toString(); | ||
}, $course->getTeachers()); | ||
|
||
return [ | ||
'name' => $course->getName(), | ||
'students' => $students, | ||
'teachers' => $teachers, | ||
]; | ||
}, $this->courses); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function toString(): string | ||
{ | ||
// TODO: Implement toString() method. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Model; | ||
|
||
interface SchoolComponent | ||
{ | ||
/** | ||
* @return array | ||
*/ | ||
public function formatForPrint(): array; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function toString(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
namespace Model; | ||
|
||
class Student extends User | ||
{ | ||
public function __construct($name, $email) | ||
{ | ||
parent::__construct($name, $email); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
namespace Model; | ||
|
||
class Teacher extends User | ||
{ | ||
public function __construct($name, $email) | ||
{ | ||
parent::__construct($name, $email, 'Teacher'); | ||
} | ||
} |
Oops, something went wrong.