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.
110 lines
2.9 KiB
110 lines
2.9 KiB
<?php
|
|
|
|
namespace IQBall\Core\Model;
|
|
|
|
use IQBall\Core\Data\CourtType;
|
|
use IQBall\App\Session\SessionHandle;
|
|
use IQBall\Core\Data\TacticInfo;
|
|
use IQBall\Core\Gateway\TacticInfoGateway;
|
|
use IQBall\Core\Validation\ValidationFail;
|
|
|
|
class TacticModel {
|
|
public const TACTIC_DEFAULT_NAME = "Nouvelle tactique";
|
|
|
|
|
|
private TacticInfoGateway $tactics;
|
|
|
|
/**
|
|
* @param TacticInfoGateway $tactics
|
|
*/
|
|
public function __construct(TacticInfoGateway $tactics) {
|
|
$this->tactics = $tactics;
|
|
}
|
|
|
|
/**
|
|
* creates a new empty tactic, with given name
|
|
* @param string $name
|
|
* @param int $ownerId
|
|
* @param CourtType $type
|
|
* @return TacticInfo
|
|
*/
|
|
public function makeNew(string $name, int $ownerId, CourtType $type): TacticInfo {
|
|
$id = $this->tactics->insert($name, $ownerId, $type);
|
|
return $this->tactics->get($id);
|
|
}
|
|
|
|
/**
|
|
* creates a new empty tactic, with a default name
|
|
* @param int $ownerId
|
|
* @param CourtType $type
|
|
* @return TacticInfo|null
|
|
*/
|
|
public function makeNewDefault(int $ownerId, CourtType $type): ?TacticInfo {
|
|
return $this->makeNew(self::TACTIC_DEFAULT_NAME, $ownerId, $type);
|
|
}
|
|
|
|
/**
|
|
* Tries to retrieve information about a tactic
|
|
* @param int $id tactic identifier
|
|
* @return TacticInfo|null or null if the identifier did not match a tactic
|
|
*/
|
|
public function get(int $id): ?TacticInfo {
|
|
return $this->tactics->get($id);
|
|
}
|
|
|
|
/**
|
|
* Return the nb last tactics created
|
|
*
|
|
* @param integer $nb
|
|
* @return array<array<string,mixed>>
|
|
*/
|
|
|
|
/**
|
|
* Return the nb last tactics
|
|
*/
|
|
public function getLast(int $nb, int $ownerId): array {
|
|
return $this->tactics->getLast($nb, $ownerId);
|
|
}
|
|
|
|
/**
|
|
* Get all the tactics of the owner
|
|
*
|
|
* @param integer $ownerId
|
|
* @return array|null
|
|
*/
|
|
public function getAll(int $ownerId): ?array {
|
|
return $this->tactics->getAll($ownerId);
|
|
}
|
|
/**
|
|
* Update the name of a tactic
|
|
* @param int $id the tactic identifier
|
|
* @param string $name the new name to set
|
|
* @return ValidationFail[] failures, if any
|
|
*/
|
|
public function updateName(int $id, string $name, int $authId): array {
|
|
|
|
$tactic = $this->tactics->get($id);
|
|
|
|
if ($tactic == null) {
|
|
return [ValidationFail::notFound("Could not find tactic")];
|
|
}
|
|
|
|
if ($tactic->getOwnerId() != $authId) {
|
|
return [ValidationFail::unauthorized()];
|
|
}
|
|
|
|
if (!$this->tactics->updateName($id, $name)) {
|
|
return [ValidationFail::error("Could not update name")];
|
|
}
|
|
return [];
|
|
}
|
|
|
|
public function updateContent(int $id, string $json): ?ValidationFail {
|
|
if (!$this->tactics->updateContent($id, $json)) {
|
|
return ValidationFail::error("Could not update content");
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|