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.
63 lines
1.6 KiB
63 lines
1.6 KiB
<?php
|
|
|
|
namespace App\Model;
|
|
|
|
use App\Data\Account;
|
|
use App\Data\TacticInfo;
|
|
use App\Gateway\TacticInfoGateway;
|
|
use App\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;
|
|
}
|
|
|
|
public function makeNew(string $name, int $ownerId): TacticInfo {
|
|
return $this->tactics->insert($name, $ownerId);
|
|
}
|
|
|
|
public function makeNewDefault(int $ownerId): ?TacticInfo {
|
|
return $this->tactics->insert(self::TACTIC_DEFAULT_NAME, $ownerId);
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
|
|
/**
|
|
* 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()];
|
|
}
|
|
|
|
$this->tactics->updateName($id, $name);
|
|
return [];
|
|
}
|
|
|
|
}
|