Skip to content

Instantly share code, notes, and snippets.

@adirkuhn
Created September 14, 2018 20:51
Show Gist options
  • Save adirkuhn/e0486855649eec1faf78f769769745e2 to your computer and use it in GitHub Desktop.
Save adirkuhn/e0486855649eec1faf78f769769745e2 to your computer and use it in GitHub Desktop.
<?php
namespace School;
use School\Persistence\GroupStore;
use School\Persistence\PupilStore;
use School\Model\TooManyPupilsException;
class GroupService
{
public function add($id, $pupilId)
{
if (strlen($id) <= 5) {
$store = GroupStore::instance();
$group = $store->find($id);
$pupils = $group->getPupils();
if (count($pupils) <= 5) {
$addPupil = PupilStore::instance()->find($pupilId);
$tmp = false;
foreach ($pupils as $pupil) {
if ($pupil->getId() == $addPupil->getId()) {
$tmp = true;
}
}
if(!$tmp) {
$group->addPupil($addPupil);
$store->persist($group);
}
} else {
throw new TooManyPupilsException();
}
} else {
throw new \RuntimeException('Group ID invalid.');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment