Skip to content

Instantly share code, notes, and snippets.

@adammbalogh
Last active October 12, 2016 08:42
Show Gist options
  • Save adammbalogh/10cf761f764e8b7122a4c2fbfb0bbcde to your computer and use it in GitHub Desktop.
Save adammbalogh/10cf761f764e8b7122a4c2fbfb0bbcde to your computer and use it in GitHub Desktop.
<?php
class ProjectMysqlRepository implements ProjectRepository
{
/**
* @param Project $project
*
* @return Project
*/
public function save(Project $project)
{
# Build an array from $project
# If $project->getId() is null then insert, else update based on the key(s)
$dbTasks = [];
try {
foreach ($project->getTaskCollection() as $task) {
$dbTasks[] = $this->taskRepsitory->save($task);
}
} catch (TasksAreNotAccessibleException $e) {
# nothing to do
}
# We could use UUID as well
# If we use regular id we can get the last inserted id ($id)
return new DbProject($id, $project->getName(), $dbTasks);
}
/**
* @param int $projectId
*
* @return Project
*/
public function findById($projectId)
{
# Project select statement
# If project is not found thrown an exception
return new DbProject($id, $name, null);
}
/**
* @param int $projectId
*
* @return Project
*/
public function findByIdWithTasks($projectId)
{
# Project select statement
# If project is not found thrown an exception
$dbTasks = $this->taskRepository->findByProjectId($projectId);
return new DbProject($id, $name, $dbTasks);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment