Skip to content

Commit

Permalink
Small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
neneone committed Jul 10, 2019
1 parent 448b51a commit c8e6dcc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/DatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,28 @@ trait DatabaseManager
public function connectToDatabase($host, $databaseName, $username, $password)
{
try {
$this->db = new \PDO('mysql:host='.$host.';dbname='.$databaseName, $username, $password);
$db = new \PDO('mysql:host='.$host.';dbname='.$databaseName, $username, $password);
} catch (\PDOException $e) {
die($e->getMessage());
}
return $db;
}

public function checkUserInDatabase($userID, $name, $username = '')
{
$q = $this->db->prepare('SELECT * FROM '.$this->settings['database']['tableName'].' WHERE userID = :userID');
$q->bindParam(':userID', $userID);
$q->execute();
if (0 == $q->rowCount()) {
if ($q->rowCount() == 0) {
$this->addUserToDatabase($userID, $name, $username);
$u = $this->checkUserInDatabase($userID, $name, $username);
} else {
$this->u = $q->fetchAll(\PDO::FETCH_ASSOC)[0];
if ($this->u['lastUpdate'] < date('Y-m-d')) {
$u = $q->fetchAll(\PDO::FETCH_ASSOC)[0];
if ($u['lastUpdate'] < date('Y-m-d')) {
$this->updateUserInDatabase($userID, $name, $username);
}
}
return $u;
}

public function addUserToDatabase($userID, $name, $username)
Expand Down
4 changes: 2 additions & 2 deletions src/SnapeBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function __construct($botToken, $update, $settings = [])

$this->BotAPI = new BotAPI($this->botToken);
$this->API = new API($this->botToken, $this);
$this->connectToDatabase($this->settings['database']['host'], $this->settings['database']['dbName'], $this->settings['database']['username'], $this->settings['database']['password']);
$this->db = $this->connectToDatabase($this->settings['database']['host'], $this->settings['database']['dbName'], $this->settings['database']['username'], $this->settings['database']['password']);

file_put_contents('settings.json', json_encode($this->settings, JSON_PRETTY_PRINT));
if (true == $this->settings['firstRun']) {
Expand All @@ -129,7 +129,7 @@ public function __construct($botToken, $update, $settings = [])
$this->update = $update;
$this->makeVariables();
if (isset($this->userID) && $this->userID) {
$this->checkUserInDatabase($this->userID, $this->fullName, (isset($this->username) ? $this->username : ''));
$this->u = $this->checkUserInDatabase($this->userID, $this->fullName, (isset($this->username) ? $this->username : ''));
}
}

Expand Down

0 comments on commit c8e6dcc

Please sign in to comment.