Skip to content

Commit

Permalink
TASK-1: Major refactor to implement design patterns of Repository, Bu…
Browse files Browse the repository at this point in the history
…ilder, Composite
  • Loading branch information
Andrew Gosali committed Oct 2, 2024
1 parent 275eeea commit ba5c784
Show file tree
Hide file tree
Showing 40 changed files with 2,832 additions and 231 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
vendor
75 changes: 75 additions & 0 deletions Builders/SchoolBuilder.php
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;
}
}
127 changes: 127 additions & 0 deletions Models/Course.php
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;
}
}
62 changes: 62 additions & 0 deletions Models/School.php
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.
}
}
16 changes: 16 additions & 0 deletions Models/SchoolComponent.php
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;
}
10 changes: 10 additions & 0 deletions Models/Student.php
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);
}
}
10 changes: 10 additions & 0 deletions Models/Teacher.php
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');
}
}
Loading

0 comments on commit ba5c784

Please sign in to comment.