Move the cloud.code generation to the DataBase class
This commit is contained in:
parent
0980d0a82f
commit
3ff96924fb
3 changed files with 58 additions and 19 deletions
51
db.php
51
db.php
|
@ -12,6 +12,7 @@ class DataBase
|
|||
'month' => '+1 month'
|
||||
);
|
||||
const DEFAULT_SIZE = 3;
|
||||
const MAX_SIZE = 9;
|
||||
const CLOUD_CODE_LENGTH = 6;
|
||||
|
||||
private $db;
|
||||
|
@ -162,29 +163,63 @@ class DataBase
|
|||
array_multisort($values, SORT_DESC, $words);
|
||||
return $words;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new cloud
|
||||
*
|
||||
* The function will try to generate a new code for the cloud,
|
||||
* if it fails, the function will return false, otherwise the
|
||||
* cloud local variable will save the new cloud.
|
||||
* @param string $text Text associated to the cloud.
|
||||
* @param int $size The number of words asked each time,
|
||||
* must be between 1 and MAX_SIZE.
|
||||
* @param string $duration Duration code, from OPTIONS_DURATION.
|
||||
* @return False if error, the cloud if success
|
||||
*/
|
||||
public function createCloud(
|
||||
string $ref,
|
||||
string $text = '',
|
||||
int $size = null,
|
||||
int $size = null,
|
||||
string $duration = null)
|
||||
{
|
||||
if (!isset($size)) {
|
||||
$size = 3;
|
||||
} elseif ($size < 1) {
|
||||
$size = 1;
|
||||
} elseif ($size > self::MAX_SIZE) {
|
||||
$size = self::MAX_SIZE;
|
||||
}
|
||||
if (!isset($duration)) {
|
||||
$duration = self::DEFAULT_DURATION;
|
||||
} elseif (!key_exists($duration, self::OPTIONS_DURATION)) {
|
||||
$duration = self::DEFAULT_DURATION;
|
||||
}
|
||||
|
||||
$cpt = 0;
|
||||
$codeIsUsed = true;
|
||||
while ($codeIsUsed && $cpt < 0) {
|
||||
$code = bin2hex(random_bytes(self::CLOUD_CODE_LENGTH));
|
||||
$stmt = $this->db->prepare("
|
||||
SELECT *
|
||||
FROM clouds
|
||||
WHERE code = :code;
|
||||
");
|
||||
$stmt->bindValue(':code', $code);
|
||||
$stmt->execute();
|
||||
if (!$data = $stmt->fetch()) {
|
||||
$codeIsUsed = false;
|
||||
}
|
||||
$cpt++;
|
||||
}
|
||||
if ($codeIsUsed) {
|
||||
return false;
|
||||
}
|
||||
$duration = date('Y-m-d H:i:s', strtotime(self::OPTIONS_DURATION[$duration]));
|
||||
$stmt = $this->db->prepare("
|
||||
INSERT INTO clouds(code, text, size, delete_t)
|
||||
VALUES (:code, :text, :size, :duration);
|
||||
");
|
||||
$stmt->bindValue(':code', $ref);
|
||||
$stmt->bindValue(':text', $text);
|
||||
$stmt->bindValue(':size', $size);
|
||||
$stmt->bindValue(':code', $code, PDO::PARAM_STR);
|
||||
$stmt->bindValue(':text', $text, PDO::PARAM_STR);
|
||||
$stmt->bindValue(':size', $size, PDO::PARAM_INT);
|
||||
$stmt->bindValue(':duration', $duration, PDO::PARAM_STR);
|
||||
try {
|
||||
$stmt->execute();
|
||||
|
@ -193,12 +228,12 @@ class DataBase
|
|||
}
|
||||
$this->cloud = array(
|
||||
'id' => $this->db->lastInsertId(),
|
||||
'code' => $ref,
|
||||
'code' => $code,
|
||||
'size' => $size,
|
||||
'delete_t' => $duration,
|
||||
'text' => $text,
|
||||
);
|
||||
return true;
|
||||
return $this->cloud;
|
||||
}
|
||||
public function loadCloud($ref)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue