You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
143 lines
4.0 KiB
143 lines
4.0 KiB
<?php
|
|
|
|
namespace IQBall\Core\Model;
|
|
|
|
use IQBall\Core\Data\Team;
|
|
use IQBall\Core\Data\TeamInfo;
|
|
use IQBall\Core\Gateway\AccountGateway;
|
|
use IQBall\Core\Gateway\MemberGateway;
|
|
use IQBall\Core\Gateway\TeamGateway;
|
|
|
|
class TeamModel {
|
|
private AccountGateway $users;
|
|
private TeamGateway $teams;
|
|
private MemberGateway $members;
|
|
|
|
/**
|
|
* @param TeamGateway $gateway
|
|
* @param MemberGateway $members
|
|
* @param AccountGateway $users
|
|
*/
|
|
public function __construct(TeamGateway $gateway, MemberGateway $members, AccountGateway $users) {
|
|
$this->teams = $gateway;
|
|
$this->members = $members;
|
|
$this->users = $users;
|
|
}
|
|
|
|
/**
|
|
* Create a team
|
|
* @param string $name
|
|
* @param string $picture
|
|
* @param string $mainColor
|
|
* @param string $secondColor
|
|
* @return int
|
|
*/
|
|
public function createTeam(string $name, string $picture, string $mainColor, string $secondColor): int {
|
|
return $this->teams->insert($name, $picture, $mainColor, $secondColor);
|
|
}
|
|
|
|
/**
|
|
* add a member to a team
|
|
* @param string $mail
|
|
* @param int $teamId
|
|
* @param string $role
|
|
* @return int
|
|
*/
|
|
public function addMember(string $mail, int $teamId, string $role): int {
|
|
$user = $this->users->getAccountFromMail($mail);
|
|
if($user == null) {
|
|
return -1;
|
|
}
|
|
if(!$this->members->isMemberOfTeam($teamId, $user->getUser()->getId())) {
|
|
$this->members->insert($teamId, $user->getUser()->getId(), $role);
|
|
return 1;
|
|
}
|
|
return -2;
|
|
}
|
|
|
|
/**
|
|
* @param string $name
|
|
* @param int $id
|
|
* @return TeamInfo[]
|
|
*/
|
|
public function listByName(string $name, int $id): array {
|
|
return $this->teams->listByName($name, $id);
|
|
}
|
|
|
|
/**
|
|
* @param int $idTeam
|
|
* @param int $idCurrentUser
|
|
* @return Team|null
|
|
*/
|
|
public function getTeam(int $idTeam, int $idCurrentUser): ?Team {
|
|
if(!$this->members->isMemberOfTeam($idTeam, $idCurrentUser)) {
|
|
return null;
|
|
}
|
|
$teamInfo = $this->teams->getTeamById($idTeam);
|
|
$members = $this->members->getMembersOfTeam($idTeam);
|
|
return new Team($teamInfo, $members);
|
|
}
|
|
|
|
/**
|
|
* delete a member from given team identifier
|
|
* @param int $idMember
|
|
* @param int $teamId
|
|
* @return int
|
|
*/
|
|
public function deleteMember(int $idMember, int $teamId): int {
|
|
$this->members->remove($teamId, $idMember);
|
|
if(empty($this->members->getMembersOfTeam($teamId))) {
|
|
$this->teams->deleteTeam($teamId);
|
|
return -1;
|
|
}
|
|
return $teamId;
|
|
}
|
|
|
|
/**
|
|
* Delete a team
|
|
* @param string $email
|
|
* @param int $idTeam
|
|
* @return int
|
|
*/
|
|
public function deleteTeam(string $email, int $idTeam): int {
|
|
if($this->members->isCoach($email, $idTeam)) {
|
|
$this->teams->deleteTeam($idTeam);
|
|
return 0;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
/**
|
|
* Verify if the account associated to an email is in a specific team indicated with its id
|
|
* @param int $idTeam
|
|
* @param string $email
|
|
* @return bool
|
|
*/
|
|
public function isCoach(int $idTeam, string $email): bool {
|
|
return $this->members->isCoach($email, $idTeam);
|
|
}
|
|
|
|
/**
|
|
* Edit a team with its id, and replace the current attributes with the new ones
|
|
* @param int $idTeam
|
|
* @param string $newName
|
|
* @param string $newPicture
|
|
* @param string $newMainColor
|
|
* @param string $newSecondColor
|
|
* @return void
|
|
*/
|
|
public function editTeam(int $idTeam, string $newName, string $newPicture, string $newMainColor, string $newSecondColor) {
|
|
$this->teams->editTeam($idTeam, $newName, $newPicture, $newMainColor, $newSecondColor);
|
|
}
|
|
|
|
/**
|
|
* Get all user's teams
|
|
*
|
|
* @param integer $user
|
|
* @return array<array<string, mixed>>
|
|
*/
|
|
public function getAll(int $user): array {
|
|
return $this->teams->getAll($user);
|
|
}
|
|
}
|