Fix php style

This commit is contained in:
osaajani 2020-01-17 18:19:25 +01:00
parent 461bd9c98d
commit b8bd067dc7
59 changed files with 2307 additions and 1868 deletions

View file

@ -12,7 +12,7 @@
namespace controllers\internals;
/**
* Class to interact with adapters
* Class to interact with adapters.
*/
class Adapter extends \descartes\InternalController
{
@ -20,10 +20,11 @@ namespace controllers\internals;
private const ADAPTERS_META_START = 'meta_';
/**
* List adapters using internal metas
* List adapters using internal metas.
*
* @return array
*/
public function list_adapters ()
public function list_adapters()
{
$adapters = [];
@ -47,12 +48,12 @@ namespace controllers\internals;
return $adapters;
}
/**
* List Adapters files
* @return mixed (false|array) : array of adapters files path
*/
public function list_files ()
* List Adapters files.
*
* @return mixed (false|array) : array of adapters files path
*/
public function list_files()
{
if (!is_readable(PWD_ADAPTERS))
{
@ -66,32 +67,35 @@ namespace controllers\internals;
{
$len = mb_strlen(self::ADAPTERS_FILES_END);
$end = mb_substr($filename, -$len);
if ($end !== self::ADAPTERS_FILES_END)
if (self::ADAPTERS_FILES_END !== $end)
{
continue;
}
$adapters_files[] = PWD_ADAPTERS . '/' . $filename;
$adapters_files[] = PWD_ADAPTERS.'/'.$filename;
}
return $adapters_files;
}
/**
* Read constants of an adapter
* Read constants of an adapter.
*
* @param mixed $adapter_file
*
* @return mixed(array|bool) : False on error, array of constants name => value
*/
public function read_adapter_metas ($adapter_file)
public function read_adapter_metas($adapter_file)
{
$metas = [];
if (!is_readable($adapter_file))
{
return false;
}
$adapter_classname = pathinfo($adapter_file, PATHINFO_FILENAME);
$reflection_class = new \ReflectionClass('\adapters\\' . $adapter_classname);
$reflection_class = new \ReflectionClass('\adapters\\'.$adapter_classname);
if (!$reflection_class)
{
return false;
@ -102,7 +106,7 @@ namespace controllers\internals;
{
$start_with = mb_substr($method->getName(), 0, mb_strlen(self::ADAPTERS_META_START));
if ($start_with !== self::ADAPTERS_META_START)
if (self::ADAPTERS_META_START !== $start_with)
{
continue;
}

View file

@ -13,25 +13,16 @@ namespace controllers\internals;
class Command extends StandardController
{
protected $model = null;
protected $model;
/**
* Get the model for the Controller
* @return \descartes\Model
*/
protected function get_model () : \descartes\Model
{
$this->model = $this->model ?? new \models\Command($this->bdd);
return $this->model;
}
/**
* Create a new command
* @param int $id_user : User id
* @param string $name : Command name
* @param string $script : Script file
* @param bool $admin : Is command admin only
* Create a new command.
*
* @param int $id_user : User id
* @param string $name : Command name
* @param string $script : Script file
* @param bool $admin : Is command admin only
*
* @return mixed bool|int : False if cannot create command, id of the new command else
*/
public function create(int $id_user, string $name, string $script, bool $admin)
@ -50,19 +41,20 @@ namespace controllers\internals;
}
$internal_event = new Event($this->bdd);
$internal_event->create($id_user, 'COMMAND_ADD', 'Ajout commande : ' . $name . ' => ' . $script);
$internal_event->create($id_user, 'COMMAND_ADD', 'Ajout commande : '.$name.' => '.$script);
return $result;
}
/**
* Update a command
* @param int $id_user : User id
* @param int $id : Command id
* @param string $name : Command name
* @param string $script : Script file
* @param bool $admin : Is command admin only
* Update a command.
*
* @param int $id_user : User id
* @param int $id : Command id
* @param string $name : Command name
* @param string $script : Script file
* @param bool $admin : Is command admin only
*
* @return mixed bool|int : False if cannot create command, id of the new command else
*/
public function update_for_user(int $id_user, int $id, string $name, string $script, bool $admin)
@ -76,38 +68,36 @@ namespace controllers\internals;
return $this->get_model()->update_for_user($id_user, $id, $datas);
}
/**
* Analyse a message to check if it's a command and extract it
* @param int $id_user : User id to search a command for
* Analyse a message to check if it's a command and extract it.
*
* @param int $id_user : User id to search a command for
* @param string $message : Text of the message to analyse
*
* @return mixed : false on error, array with new text and command to execute ['updated_text' => string, 'command' => string]
*/
public function check_for_command (int $id_user, string $message)
public function check_for_command(int $id_user, string $message)
{
$extracted_command = [];
$decode_message = json_decode(trim($message), true);
if ($decode_message === null)
if (null === $decode_message)
{
return false;
}
if (!isset($decode_message['login'], $decode_message['password'], $decode_message['command']))
{
return false;
}
//Check for user
$internal_user = new \controllers\internals\User($this->bdd);
$user = $internal_user->check_credentials($decode_message['login'], $decode_message['password']);
if (!$user || $user['id'] != $id_user)
if (!$user || $user['id'] !== $id_user)
{
return false;
}
//Find command
$commands = $this->gets_for_user($user['id']);
@ -117,6 +107,7 @@ namespace controllers\internals;
if ($decode_message['command'] === $command['name'])
{
$find_command = $command;
break;
}
}
@ -125,28 +116,36 @@ namespace controllers\internals;
{
return false;
}
//Check for admin rights
if ($find_command['admin'] && !$user['admin'])
{
return false;
}
//Forge command and return
$decode_message['password'] = '******';
$updated_text = json_encode($decode_message);
$generated_command = PWD_SCRIPTS . '/' . $find_command['script'];
$generated_command = PWD_SCRIPTS.'/'.$find_command['script'];
$args = $decode_message['args'] ?? '';
$generated_command .= ' ' . escapeshellcmd($args);
$generated_command .= ' '.escapeshellcmd($args);
return [
'updated_text' => $updated_text,
'command' => $generated_command,
];
}
/**
* Get the model for the Controller.
*
* @return \descartes\Model
*/
protected function get_model(): \descartes\Model
{
$this->model = $this->model ?? new \models\Command($this->bdd);
return $this->model;
}
}

View file

@ -13,27 +13,18 @@ namespace controllers\internals;
class ConditionalGroup extends StandardController
{
protected $model = null;
protected $model;
/**
* Get the model for the Controller
* @return \descartes\Model
*/
protected function get_model () : \descartes\Model
{
$this->model = $this->model ?? new \models\ConditionalGroup($this->bdd);
return $this->model;
}
/**
* Create a new group for a user
* @param int $id_user : user id
* @param string $name : Group name
* Create a new group for a user.
*
* @param int $id_user : user id
* @param string $name : Group name
* @param string $condition : Condition for forming group content
*
* @return mixed bool|int : false on error, new group id
*/
public function create (int $id_user, string $name, string $condition)
public function create(int $id_user, string $name, string $condition)
{
$conditional_group = [
'id_user' => $id_user,
@ -47,7 +38,7 @@ namespace controllers\internals;
{
return false;
}
$id_group = $this->get_model()->insert($conditional_group);
if (!$id_group)
{
@ -55,18 +46,19 @@ namespace controllers\internals;
}
$internal_event = new Event($this->bdd);
$internal_event->create($id_user, 'CONDITIONAL_GROUP_ADD', 'Ajout du groupe conditionnel : ' . $name);
$internal_event->create($id_user, 'CONDITIONAL_GROUP_ADD', 'Ajout du groupe conditionnel : '.$name);
return $id_group;
}
/**
* Update a group for a user
* @param int $id_user : User id
* @param int $id_group : Group id
* @param string $name : Group name
* Update a group for a user.
*
* @param int $id_user : User id
* @param int $id_group : Group id
* @param string $name : Group name
* @param string $condition : Condition for forming group content
*
* @return bool : False on error, true on success
*/
public function update_for_user(int $id_user, int $id_group, string $name, string $condition)
@ -92,26 +84,28 @@ namespace controllers\internals;
return true;
}
/**
* Return a group by his name for a user
* @param int $id_user : User id
* @param string $name : Group name
* Return a group by his name for a user.
*
* @param int $id_user : User id
* @param string $name : Group name
*
* @return array
*/
public function get_by_name_for_user (int $id_user, string $name)
public function get_by_name_for_user(int $id_user, string $name)
{
return $this->get_model()->get_by_name_for_user($id_user, $name);
}
/**
* Gets the user's contacts that respects a condition
* @param int $id_user : User id
* Gets the user's contacts that respects a condition.
*
* @param int $id_user : User id
* @param string $condition : Condition string to verify
*
* @return array
*/
public function get_contacts_for_condition_and_user (int $id_user, string $condition) : array
public function get_contacts_for_condition_and_user(int $id_user, string $condition): array
{
$internal_contacts = new Contact($this->bdd);
$contacts = $internal_contacts->gets_for_user($id_user);
@ -122,7 +116,7 @@ namespace controllers\internals;
{
$contact['datas'] = json_decode($contact['datas']);
$contact = (object) $contact;
$datas = ['contact' => $contact];
$is_valid = $ruler->evaluate_condition($condition, $datas);
if (!$is_valid)
@ -130,7 +124,19 @@ namespace controllers\internals;
unset($contacts[$key]);
}
}
return $contacts;
}
/**
* Get the model for the Controller.
*
* @return \descartes\Model
*/
protected function get_model(): \descartes\Model
{
$this->model = $this->model ?? new \models\ConditionalGroup($this->bdd);
return $this->model;
}
}

View file

@ -12,42 +12,40 @@
namespace controllers\internals;
/**
* Class to call the console scripts
* Class to call the console scripts.
*/
class Console extends \descartes\InternalController
{
/**
* Start launcher daemon
* Start launcher daemon.
*/
public function launcher ()
public function launcher()
{
new \daemons\Launcher();
}
/**
* Start sender daemon
* Start sender daemon.
*/
public function sender ()
public function sender()
{
new \daemons\Sender();
}
/**
* Start webhook daemon
* Start webhook daemon.
*/
public function webhook ()
public function webhook()
{
new \daemons\Webhook();
}
/**
* Start a phone daemon
* Start a phone daemon.
*
* @param $id_phone : Phone id
*/
public function phone ($id_phone)
public function phone($id_phone)
{
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
$internal_phone = new \controllers\internals\Phone($bdd);
@ -60,9 +58,11 @@ namespace controllers\internals;
new \daemons\Phone($phone);
}
/**
* Cette fonction retourne la fenetre de connexion.
*
* @param mixed $id_phone
*/
public function test($id_phone)
{
@ -71,11 +71,12 @@ namespace controllers\internals;
$phone = $internal_phone->get($id_phone);
if (!$phone)
{
echo "No phone for id : $id_phone\n";
echo "No phone for id : {$id_phone}\n";
return false;
}
echo "Found phone for id : $id_phone\n";
echo "Found phone for id : {$id_phone}\n";
$adapter_classname = $phone['adapter'];
$adapter = new $adapter_classname($phone['number'], $phone['adapter_datas']);

View file

@ -13,22 +13,14 @@ namespace controllers\internals;
class Contact extends StandardController
{
protected $model = null;
protected $model;
/**
* Get the model for the Controller
* @return \descartes\Model
*/
protected function get_model () : \descartes\Model
{
$this->model = $this->model ?? new \models\Contact($this->bdd);
return $this->model;
}
/**
* Return a contact for a user by a number
* @param int $id_user : user id
* @param string $number : Contact number
* Return a contact for a user by a number.
*
* @param int $id_user : user id
* @param string $number : Contact number
*
* @return array
*/
public function get_by_number_and_user(int $id_user, string $number)
@ -36,36 +28,39 @@ namespace controllers\internals;
return $this->get_model()->get_by_number_and_user($id_user, $number);
}
/**
* Return a contact by his name for a user
* @param int $id_user : User id
* @param string $name : Contact name
* Return a contact by his name for a user.
*
* @param int $id_user : User id
* @param string $name : Contact name
*
* @return array
*/
public function get_by_name_and_user(int $id_user, string $name)
{
return $this->get_model()->get_by_name_and_user($id_user, $name);
}
/**
* Return all contacts of a user.
*
* @param int $id_user : user id
*
* @return array
*/
public function gets_for_user (int $id_user)
public function gets_for_user(int $id_user)
{
return $this->get_model()->gets_for_user($id_user);
}
/**
* Create a new contact
* @param int $id_user : User id
* @param string $number : Contact number
* @param string $name : Contact name
* @param string $datas : Contact datas
* Create a new contact.
*
* @param int $id_user : User id
* @param string $number : Contact number
* @param string $name : Contact name
* @param string $datas : Contact datas
*
* @return mixed bool|int : False if cannot create contact, id of the new contact else
*/
public function create($id_user, $number, $name, $datas)
@ -89,14 +84,15 @@ namespace controllers\internals;
return $result;
}
/**
* Update a contact
* @param int $id_user : User id
* @param int $id : Contact id
* @param string $number : Contact number
* @param string $name : Contact name
* @param ?string $datas : Contact datas
* Update a contact.
*
* @param int $id_user : User id
* @param int $id : Contact id
* @param string $number : Contact number
* @param string $name : Contact name
* @param ?string $datas : Contact datas
*
* @return int : number of modified rows
*/
public function update_for_user(int $id_user, int $id, string $number, string $name, string $datas)
@ -110,16 +106,17 @@ namespace controllers\internals;
return $this->get_model()->update_for_user($id_user, $id, $contact);
}
/**
* Import a list of contacts as csv
* Import a list of contacts as csv.
*
* @param resource $file_handler : File handler to import contacts from
* @param int $id_user : User id
* @param int $id_user : User id
*
* @return mixed : False on error, number of inserted contacts else
*/
public function import_csv (int $id_user, $file_handler)
public function import_csv(int $id_user, $file_handler)
{
if (!is_resource($file_handler))
if (!\is_resource($file_handler))
{
return false;
}
@ -129,14 +126,15 @@ namespace controllers\internals;
$head = null;
while ($line = fgetcsv($file_handler))
{
if ($head === null)
if (null === $head)
{
$head = $line;
continue;
}
$line = array_combine($head, $line);
if ($line === false)
if (false === $line)
{
continue;
}
@ -149,12 +147,12 @@ namespace controllers\internals;
$datas = [];
foreach ($line as $key => $value)
{
if ($key == 'name' || $key == 'number')
if ('name' === $key || 'number' === $key)
{
continue;
}
if ($value === '')
if ('' === $value)
{
continue;
}
@ -167,23 +165,24 @@ namespace controllers\internals;
$success = $this->create($id_user, $line['number'], $line['name'], $datas);
if ($success)
{
$nb_insert ++;
++$nb_insert;
}
}
return $nb_insert;
}
/**
* Import a list of contacts as json
* Import a list of contacts as json.
*
* @param resource $file_handler : File handler to import contacts from
* @param int $id_user : User id
* @param int $id_user : User id
*
* @return mixed : False on error, number of inserted contacts else
*/
public function import_json (int $id_user, $file_handler)
public function import_json(int $id_user, $file_handler)
{
if (!is_resource($file_handler))
if (!\is_resource($file_handler))
{
return false;
}
@ -198,7 +197,7 @@ namespace controllers\internals;
{
$contacts = json_decode($file_content, true);
if (!is_array($contacts))
if (!\is_array($contacts))
{
return false;
}
@ -206,7 +205,7 @@ namespace controllers\internals;
$nb_insert = 0;
foreach ($contacts as $contact)
{
if (!is_array($contact))
if (!\is_array($contact))
{
continue;
}
@ -218,11 +217,11 @@ namespace controllers\internals;
$datas = $contact['datas'] ?? [];
$datas = json_encode($datas);
$success = $this->create($id_user, $contact['number'], $contact['name'], $datas);
if ($success)
{
$nb_insert ++;
++$nb_insert;
}
}
@ -233,14 +232,15 @@ namespace controllers\internals;
return false;
}
}
/**
* Export the contacts of a user as csv
* Export the contacts of a user as csv.
*
* @param int $id_user : User id
*
* @return array : ['headers' => array of headers to return, 'content' => the generated file]
*/
public function export_csv (int $id_user) : array
public function export_csv(int $id_user): array
{
$contacts = $this->get_model()->gets_for_user($id_user);
@ -254,7 +254,7 @@ namespace controllers\internals;
$datas = json_decode($contact['datas'], true);
foreach ($datas as $key => $value)
{
$columns[] = $key;
$columns[] = $key;
}
}
$columns = array_unique($columns);
@ -270,12 +270,14 @@ namespace controllers\internals;
if (isset($contact[$column]))
{
$line[] = $contact[$column];
continue;
}
if (isset($datas[$column]))
{
$line[] = $datas[$column];
continue;
}
@ -286,7 +288,7 @@ namespace controllers\internals;
//Php only support csv formatting to file. To get it in string we need to create a tmp in memory file, write in it, and then read the file into a var
// output up to 5MB is kept in memory, if it becomes bigger it will automatically be written to a temporary file
$csv_tmp_file = fopen('php://temp/maxmemory:'. (5*1024*1024), 'r+');
$csv_tmp_file = fopen('php://temp/maxmemory:'.(5 * 1024 * 1024), 'r+');
fputcsv($csv_tmp_file, $columns);
foreach ($lines as $line)
{
@ -300,26 +302,27 @@ namespace controllers\internals;
'headers' => [
'Content-Disposition: attachment; filename=contacts.csv',
'Content-Type: text/csv',
'Content-Length: ' . strlen($csv_string),
'Content-Length: '.\mb_strlen($csv_string),
],
'content' => $csv_string,
];
}
/**
* Export the contacts of a user as json
* Export the contacts of a user as json.
*
* @param int $id_user : User id
*
* @return array : ['headers' => array of headers to return, 'content' => the generated file]
*/
public function export_json (int $id_user) : array
public function export_json(int $id_user): array
{
$contacts = $this->get_model()->gets_for_user($id_user);
foreach ($contacts as &$contact)
{
unset($contact['id']);
unset($contact['id_user']);
unset($contact['id'], $contact['id_user']);
$contact['datas'] = json_decode($contact['datas']);
}
$content = json_encode($contacts);
@ -328,10 +331,21 @@ namespace controllers\internals;
'headers' => [
'Content-Disposition: attachment; filename=contacts.json',
'Content-Type: application/json',
'Content-Length: ' . strlen($content),
'Content-Length: '.\mb_strlen($content),
],
'content' => $content,
];
}
/**
* Get the model for the Controller.
*
* @return \descartes\Model
*/
protected function get_model(): \descartes\Model
{
$this->model = $this->model ?? new \models\Contact($this->bdd);
return $this->model;
}
}

View file

@ -13,42 +13,36 @@ namespace controllers\internals;
class Event extends StandardController
{
protected $model = null;
protected $model;
/**
* Get the model for the Controller
* @return \descartes\Model
* Disabled methods.
*/
protected function get_model () : \descartes\Model
public function update_for_user()
{
$this->model = $this->model ?? new \models\Event($this->bdd);
return $this->model;
}
return false;
}
/**
* Disabled methods
*/
public function update_for_user() { return false; }
/**
* Gets lasts x events for a user order by date
* @param int $id_user : User id
* Gets lasts x events for a user order by date.
*
* @param int $id_user : User id
* @param int $nb_entry : Number of events to return
*
* @return array
*/
public function get_lasts_by_date_for_user (int $id_user, int $nb_entry)
public function get_lasts_by_date_for_user(int $id_user, int $nb_entry)
{
return $this->get_model()->get_lasts_by_date_for_user($id_user, $nb_entry);
}
/**
* Create a new event
* @param int $id_user : user id
* Create a new event.
*
* @param int $id_user : user id
* @param mixed $type
* @param mixed $text
*
* @return mixed bool : false on fail, new event id else
*/
public function create($id_user, $type, $text)
@ -61,4 +55,16 @@ namespace controllers\internals;
return $this->get_model()->insert($event);
}
/**
* Get the model for the Controller.
*
* @return \descartes\Model
*/
protected function get_model(): \descartes\Model
{
$this->model = $this->model ?? new \models\Event($this->bdd);
return $this->model;
}
}

View file

@ -1,5 +1,14 @@
<?php
/*
* This file is part of RaspiSMS.
*
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
*
* This source file is subject to the GPL-3.0 license that is bundled
* with this source code in the file LICENSE.
*/
namespace controllers\internals;
use Symfony\Component\ExpressionLanguage\ExpressionFunction;

View file

@ -16,34 +16,24 @@ namespace controllers\internals;
*/
class Group extends StandardController
{
protected $model = null;
protected $model;
/**
* Get the model for the Controller
* @return \descartes\Model
*/
protected function get_model () : \descartes\Model
{
$this->model = $this->model ?? new \models\Group($this->bdd);
return $this->model;
}
/**
* Create a new group for a user
* @param int $id_user : user id
* Create a new group for a user.
*
* @param int $id_user : user id
* @param stirng $name : Group name
* @param array $contacts_ids : Ids of the contacts of the group
* @param array $contacts_ids : Ids of the contacts of the group
*
* @return mixed bool|int : false on error, new group id
*/
public function create (int $id_user, string $name, array $contacts_ids)
public function create(int $id_user, string $name, array $contacts_ids)
{
$group = [
'id_user' => $id_user,
'name' => $name,
];
$id_group = $this->get_model()->insert($group);
if (!$id_group)
{
@ -63,18 +53,19 @@ namespace controllers\internals;
}
$internal_event = new Event($this->bdd);
$internal_event->create($id_user, 'GROUP_ADD', 'Ajout group : ' . $name);
$internal_event->create($id_user, 'GROUP_ADD', 'Ajout group : '.$name);
return $id_group;
}
/**
* Update a group for a user
* @param int $id_user : User id
* @param int $id_group : Group id
* Update a group for a user.
*
* @param int $id_user : User id
* @param int $id_group : Group id
* @param stirng $name : Group name
* @param array $contacts_ids : Ids of the contacts of the group
* @param array $contacts_ids : Ids of the contacts of the group
*
* @return bool : False on error, true on success
*/
public function update_for_user(int $id_user, int $id_group, string $name, array $contacts_ids)
@ -99,7 +90,7 @@ namespace controllers\internals;
if ($this->get_model()->insert_group_contact_relation($id_group, $contact_id))
{
$nb_contact_insert++;
++$nb_contact_insert;
}
}
@ -111,26 +102,40 @@ namespace controllers\internals;
return true;
}
/**
* Return a group by his name for a user
* @param int $id_user : User id
* @param string $name : Group name
* Return a group by his name for a user.
*
* @param int $id_user : User id
* @param string $name : Group name
*
* @return array
*/
public function get_by_name_for_user (int $id_user, string $name)
public function get_by_name_for_user(int $id_user, string $name)
{
return $this->get_model()->get_by_name_for_user($id_user, $name);
}
/**
* Get groups contacts
* Get groups contacts.
*
* @param int $id_group : Group id
*
* @return array : Contacts of the group
*/
public function get_contacts($id_group)
{
return $this->get_model()->get_contacts($id_group);
}
/**
* Get the model for the Controller.
*
* @return \descartes\Model
*/
protected function get_model(): \descartes\Model
{
$this->model = $this->model ?? new \models\Group($this->bdd);
return $this->model;
}
}

View file

@ -13,26 +13,18 @@ namespace controllers\internals;
class Media extends StandardController
{
protected $model = null;
protected $model;
/**
* Get the model for the Controller
* @return \descartes\Model
*/
protected function get_model () : \descartes\Model
{
$this->model = $this->model ?? new \models\Media($this->bdd);
return $this->model;
}
/**
* Create a media
* @param int $id_user : Id of the user
* @param int $id_scheduled : Id of the scheduled
* @param array $media : $_FILES media array
* Create a media.
*
* @param int $id_user : Id of the user
* @param int $id_scheduled : Id of the scheduled
* @param array $media : $_FILES media array
*
* @return bool : false on error, new media id else
*/
public function create (int $id_user, int $id_scheduled, array $media) : bool
public function create(int $id_user, int $id_scheduled, array $media): bool
{
$internal_scheduled = new Scheduled($this->bdd);
$scheduled = $internal_scheduled->get_for_user($id_user, $id_scheduled);
@ -40,14 +32,14 @@ namespace controllers\internals;
{
return false;
}
$result_upload_media = \controllers\internals\Tool::upload_file($media);
if ($result_upload_media['success'] == false)
if (false === $result_upload_media['success'])
{
return false;
}
$datas = [
$datas = [
'id_scheduled' => $id_scheduled,
'path' => $result_upload_media['content'],
];
@ -55,22 +47,23 @@ namespace controllers\internals;
return (bool) $this->get_model()->insert($datas);
}
/**
* Update a media for a user
* @param int $id_user : user id
* @param int $id_media : Media id
* @param int $id_scheduled : Id of the scheduled
* @param string $path : Path of the file
* Update a media for a user.
*
* @param int $id_user : user id
* @param int $id_media : Media id
* @param int $id_scheduled : Id of the scheduled
* @param string $path : Path of the file
*
* @return bool : false on error, true on success
*/
public function update_for_user (int $id_user, int $id_media, int $id_scheduled, string $path) : bool
public function update_for_user(int $id_user, int $id_media, int $id_scheduled, string $path): bool
{
$media = [
$media = [
'id_scheduled' => $id_scheduled,
'path' => $path,
];
$internal_scheduled = new Scheduled($this->bdd);
$scheduled = $this->get_for_user($id_user, $id_scheduled);
if (!$scheduled)
@ -80,15 +73,16 @@ namespace controllers\internals;
return (bool) $this->get_model()->update_for_user($id_user, $id_media, $media);
}
/**
* Delete a media for a user
* Delete a media for a user.
*
* @param int $id_user : User id
* @param int $id : Entry id
* @param int $id : Entry id
*
* @return int : Number of removed rows
*/
public function delete_for_user (int $id_user, int $id_media) : bool
public function delete_for_user(int $id_user, int $id_media): bool
{
$media = $this->get_model()->get_for_user($id_user, $id_media);
if (!$media)
@ -100,15 +94,16 @@ namespace controllers\internals;
return $this->get_model()->delete_for_user($id_user, $id);
}
/**
* Delete a media for a scheduled and a user
* @param int $id_user : User id
* Delete a media for a scheduled and a user.
*
* @param int $id_user : User id
* @param int $id_scheduled : Scheduled id to delete medias for
*
* @return int : Number of removed rows
*/
public function delete_for_scheduled_and_user (int $id_user, int $id_scheduled) : bool
public function delete_for_scheduled_and_user(int $id_user, int $id_scheduled): bool
{
$media = $this->get_model()->get_for_scheduled_and_user($id_user, $id_scheduled);
if ($media)
@ -119,15 +114,28 @@ namespace controllers\internals;
return $this->get_model()->delete_for_scheduled_and_user($id_user, $id_scheduled);
}
/**
* Find medias for a scheduled and a user
* @param int $id_user : User id
* Find medias for a scheduled and a user.
*
* @param int $id_user : User id
* @param int $id_scheduled : Scheduled id to delete medias for
*
* @return mixed : Medias || false
*/
public function get_for_scheduled_and_user (int $id_user, int $id_scheduled)
public function get_for_scheduled_and_user(int $id_user, int $id_scheduled)
{
return $this->get_model()->get_for_scheduled_and_user($id_user, $id_scheduled);
}
/**
* Get the model for the Controller.
*
* @return \descartes\Model
*/
protected function get_model(): \descartes\Model
{
$this->model = $this->model ?? new \models\Media($this->bdd);
return $this->model;
}
}

View file

@ -13,33 +13,25 @@ namespace controllers\internals;
class Phone extends StandardController
{
protected $model = null;
protected $model;
/**
* Get the model for the Controller
* @return \descartes\Model
*/
protected function get_model () : \descartes\Model
{
$this->model = $this->model ?? new \models\Phone($this->bdd);
return $this->model;
}
/**
* Return all phones of a user.
*
* @param int $id_user : user id
*
* @return array
*/
public function gets_for_user (int $id_user)
public function gets_for_user(int $id_user)
{
return $this->get_model()->gets_for_user($id_user);
}
/**
* Return a phone by his number
* Return a phone by his number.
*
* @param string $number : Phone number
*
* @return array
*/
public function get_by_number(string $number)
@ -47,11 +39,12 @@ namespace controllers\internals;
return $this->get_model()->get_by_number($number);
}
/**
* Return a phone for a user by a number
* @param int $id_user : user id
* @param string $number : Phone number
* Return a phone for a user by a number.
*
* @param int $id_user : user id
* @param string $number : Phone number
*
* @return array
*/
public function get_by_number_and_user(int $id_user, string $number)
@ -59,18 +52,19 @@ namespace controllers\internals;
return $this->get_model()->get_by_number_and_user($id_user, $number);
}
/**
* Create a phone
* @param int $id_user : User to insert phone for
* @param string $number : The number of the phone
* @param string $adapter : The adapter to use the phone
* Create a phone.
*
* @param int $id_user : User to insert phone for
* @param string $number : The number of the phone
* @param string $adapter : The adapter to use the phone
* @param string json $adapter_datas : A JSON string representing adapter's datas (for example credentials for an api)
*
* @return bool : false on error, true on success
*/
public function create (int $id_user, string $number, string $adapter, string $adapter_datas) : bool
public function create(int $id_user, string $number, string $adapter, string $adapter_datas): bool
{
$phone = [
$phone = [
'id_user' => $id_user,
'number' => $number,
'adapter' => $adapter,
@ -80,17 +74,18 @@ namespace controllers\internals;
return (bool) $this->get_model()->insert($phone);
}
/**
* Update a phone
* @param int $id_user : User to insert phone for
* @param int $id : Phone id
* @param string $number : The number of the phone
* @param string $adapter : The adapter to use the phone
* @param array $adapter_datas : An array of the datas of the adapter (for example credentials for an api)
* Update a phone.
*
* @param int $id_user : User to insert phone for
* @param int $id : Phone id
* @param string $number : The number of the phone
* @param string $adapter : The adapter to use the phone
* @param array $adapter_datas : An array of the datas of the adapter (for example credentials for an api)
*
* @return bool : false on error, true on success
*/
public function update_for_user (int $id_user, int $id, string $number, string $adapter, array $adapter_datas) : bool
public function update_for_user(int $id_user, int $id, string $number, string $adapter, array $adapter_datas): bool
{
$phone = [
'id_user' => $id_user,
@ -101,4 +96,16 @@ namespace controllers\internals;
return (bool) $this->get_model()->update_for_user($id_user, $id, $phone);
}
/**
* Get the model for the Controller.
*
* @return \descartes\Model
*/
protected function get_model(): \descartes\Model
{
$this->model = $this->model ?? new \models\Phone($this->bdd);
return $this->model;
}
}

View file

@ -13,47 +13,39 @@ namespace controllers\internals;
class Received extends StandardController
{
protected $model = null;
protected $model;
/**
* Get the model for the Controller
* @return \descartes\Model
*/
protected function get_model () : \descartes\Model
{
$this->model = $this->model ?? new \models\Received($this->bdd);
return $this->model;
}
/**
* Return the list of unread messages for a user
* @param int $id_user : User id
* Return the list of unread messages for a user.
*
* @param int $id_user : User id
* @param ?int $nb_entry : Number of entry to return
* @param ?int $page : Pagination, used to calcul offset, $nb_entry * $page
*
* @return array : Entrys list
*/
public function list_unread_for_user (int $id_user, ?int $nb_entry = null, ?int $page = null)
public function list_unread_for_user(int $id_user, ?int $nb_entry = null, ?int $page = null)
{
return $this->get_model()->list_unread_for_user($id_user, $nb_entry, $nb_entry * $page);
}
/**
* Create a received
* Create a received.
*
* @param $at : Reception date
* @param $text : Text of the message
* @param string $origin : Number of the sender
* @param string $origin : Number of the sender
* @param string $destination : Number of the receiver
* @param string $status : Status of the received message
* @param bool $command : Is the sms a command
* @param string $status : Status of the received message
* @param bool $command : Is the sms a command
*
* @return bool : false on error, new received id else
*/
public function create ($at, string $text, string $origin, string $destination, string $status = 'unread', bool $command = false) : bool
public function create($at, string $text, string $origin, string $destination, string $status = 'unread', bool $command = false): bool
{
$received = [
$received = [
'at' => $at,
'text' => $text,
'text' => $text,
'origin' => $origin,
'destination' => $destination,
'status' => $status,
@ -63,24 +55,25 @@ namespace controllers\internals;
return (bool) $this->get_model()->insert($received);
}
/**
* Update a received for a user
* @param int $id_user : user id
* Update a received for a user.
*
* @param int $id_user : user id
* @param int $id_received : received id
* @param $at : Reception date
* @param $text : Text of the message
* @param string $origin : Number of the sender
* @param string $origin : Number of the sender
* @param string $destination : Number of the receiver
* @param string $status : Status of the received message
* @param bool $command : Is the sms a command
* @param string $status : Status of the received message
* @param bool $command : Is the sms a command
*
* @return bool : false on error, true on success
*/
public function update_for_user (int $id_user, int $id_received, $at, string $text, string $origin, string $destination, string $status = 'unread', bool $command = false) : bool
public function update_for_user(int $id_user, int $id_received, $at, string $text, string $origin, string $destination, string $status = 'unread', bool $command = false): bool
{
$received = [
$received = [
'at' => $at,
'text' => $text,
'text' => $text,
'origin' => $origin,
'destination' => $destination,
'status' => $status,
@ -90,67 +83,71 @@ namespace controllers\internals;
return (bool) $this->get_model()->update_for_user($id_user, $id_received, $received);
}
/**
* Update a received message for a user to mark the message as read
* @param int $id_user : user id
* Update a received message for a user to mark the message as read.
*
* @param int $id_user : user id
* @param int $id_received : received id
*
* @return bool : false on error, true on success
*/
public function mark_as_read_for_user (int $id_user, int $id_received) : bool
public function mark_as_read_for_user(int $id_user, int $id_received): bool
{
$received = [
$received = [
'status' => 'read',
];
return (bool) $this->get_model()->update_for_user($id_user, $id_received, $received);
}
/**
* Update a received message for a user to mark the message as unread
* @param int $id_user : user id
* Update a received message for a user to mark the message as unread.
*
* @param int $id_user : user id
* @param int $id_received : received id
*
* @return bool : false on error, true on success
*/
public function mark_as_unread_for_user (int $id_user, int $id_received) : bool
public function mark_as_unread_for_user(int $id_user, int $id_received): bool
{
$received = [
$received = [
'status' => 'unread',
];
return (bool) $this->get_model()->update_for_user($id_user, $id_received, $received);
}
/**
* Return number of unread messages for a user
* Return number of unread messages for a user.
*
* @param int $id_user : User id
* @return array
*
* @return array
*/
public function count_unread_for_user(int $id_user)
{
return $this->get_model()->count_unread_for_user($id_user);
}
/**
* Return x last receiveds message for a user, order by date
* @param int $id_user : User id
* Return x last receiveds message for a user, order by date.
*
* @param int $id_user : User id
* @param int $nb_entry : Number of receiveds messages to return
* @return array
*
* @return array
*/
public function get_lasts_by_date_for_user(int $id_user, int $nb_entry)
{
return $this->get_model()->get_lasts_by_date_for_user($id_user, $nb_entry);
}
/**
* Return receiveds for an origin and a user
* @param int $id_user : User id
* @param string $origin : Number who sent the message
* Return receiveds for an origin and a user.
*
* @param int $id_user : User id
* @param string $origin : Number who sent the message
*
* @return array
*/
public function gets_by_origin_and_user(int $id_user, string $origin)
@ -158,11 +155,12 @@ namespace controllers\internals;
return $this->get_model()->gets_by_origin_and_user($id_user, $origin);
}
/**
* Get number of sended SMS for every date since a date for a specific user
* @param int $id_user : user id
* @param \DateTime $date : Date since which we want the messages
* Get number of sended SMS for every date since a date for a specific user.
*
* @param int $id_user : user id
* @param \DateTime $date : Date since which we want the messages
*
* @return array
*/
public function count_by_day_since_for_user(int $id_user, $date)
@ -178,10 +176,11 @@ namespace controllers\internals;
return $return;
}
/**
* Return all discussions (ie : numbers we have a message received from or sended to) for a user
* Return all discussions (ie : numbers we have a message received from or sended to) for a user.
*
* @param int $id_user : User id
*
* @return array
*/
public function get_discussions_for_user(int $id_user)
@ -189,11 +188,12 @@ namespace controllers\internals;
return $this->get_model()->get_discussions_for_user($id_user);
}
/**
* Get SMS received since a date for a user
* Get SMS received since a date for a user.
*
* @param int $id_user : User id
* @param $date : La date depuis laquelle on veux les SMS (au format 2014-10-25 20:10:05)
*
* @return array : Tableau avec tous les SMS depuis la date
*/
public function get_since_by_date_for_user(int $id_user, $date)
@ -201,28 +201,42 @@ namespace controllers\internals;
return $this->get_model()->get_since_by_date_for_user($id_user, $date);
}
/**
* Find messages received since a date for a certain origin and user
* Find messages received since a date for a certain origin and user.
*
* @param int $id_user : User id
* @param $date : Date we want messages sinces
* @param string $origin : Origin number
*
* @return array
*/
public function get_since_by_date_for_origin_and_user(int $id_user, $date, string $origin)
{
return $this->get_model()->get_since_by_date_for_origin_and_user($id_user, $date, $origin);
}
/**
* Find destination of last received message for an origin and user
* @param int $id_user : User id
* @param string $origin : Origin number
* Find destination of last received message for an origin and user.
*
* @param int $id_user : User id
* @param string $origin : Origin number
*
* @return array
*/
public function get_last_for_origin_and_user (int $id_user, string $origin)
public function get_last_for_origin_and_user(int $id_user, string $origin)
{
return $this->get_model()->get_last_for_origin_and_user($id_user, $origin);
}
/**
* Get the model for the Controller.
*
* @return \descartes\Model
*/
protected function get_model(): \descartes\Model
{
$this->model = $this->model ?? new \models\Received($this->bdd);
return $this->model;
}
}

View file

@ -10,20 +10,20 @@
*/
namespace controllers\internals;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
/**
* Class to analyse rules used by conditional groups
*/
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
/**
* Class to analyse rules used by conditional groups.
*/
class Ruler extends \descartes\InternalController
{
private $expression_language;
/**
* Constructor
* Constructor.
*/
public function __construct ()
public function __construct()
{
$this->expression_language = new ExpressionLanguage();
@ -31,43 +31,47 @@ use Symfony\Component\ExpressionLanguage\ExpressionFunction;
$this->expression_language->registerProvider(new ExpressionProvider());
}
/**
* Verify if a condition is valid. i.e we can evaluate it without error.
* @param string $condition : The condition to evaluate.
* @param array $datas : The datas to made available to condition
*
* @param string $condition : The condition to evaluate
* @param array $datas : The datas to made available to condition
*
* @return bool : false if invalid, true else
*/
public function validate_condition (string $condition, array $datas = []) : bool
public function validate_condition(string $condition, array $datas = []): bool
{
try
try
{
$this->expression_language->evaluate($condition, $datas);
return true;
}
catch (\Exception $e)
{
return false;
}
}
}
/**
* Evaluate a condition
* @param string $condition : The condition to evaluate.
* @param array $datas : The datas to made available to condition
* Evaluate a condition.
*
* @param string $condition : The condition to evaluate
* @param array $datas : The datas to made available to condition
*
* @return ?bool : false if invalid, true else, null only on error
*/
public function evaluate_condition (string $condition, array $datas = []) : ?bool
public function evaluate_condition(string $condition, array $datas = []): ?bool
{
try
try
{
$result = $this->expression_language->evaluate($condition, $datas);
return (bool) $result;
}
catch (\Exception $e)
{
return null;
}
}
}
}

View file

@ -13,34 +13,26 @@ namespace controllers\internals;
class Scheduled extends StandardController
{
protected $model = null;
protected $model;
/**
* Get the model for the Controller
* @return \descartes\Model
*/
protected function get_model () : \descartes\Model
{
$this->model = $this->model ?? new \models\Scheduled($this->bdd);
return $this->model;
}
/**
* Create a scheduled
* Create a scheduled.
*
* @param int $id_user : User to insert scheduled for
* @param $at : Scheduled date to send
* @param string $text : Text of the message
* @param ?string $origin : Origin number of the message, null by default
* @param bool $flash : Is the sms a flash sms, by default false
* @param array $numbers : Numbers to send message to
* @param array $contacts_ids : Contact ids to send message to
* @param array $groups_ids : Group ids to send message to
* @param array $conditional_group_ids : Conditional Groups ids to send message to
* @param string $text : Text of the message
* @param ?string $origin : Origin number of the message, null by default
* @param bool $flash : Is the sms a flash sms, by default false
* @param array $numbers : Numbers to send message to
* @param array $contacts_ids : Contact ids to send message to
* @param array $groups_ids : Group ids to send message to
* @param array $conditional_group_ids : Conditional Groups ids to send message to
*
* @return bool : false on error, new id on success
*/
public function create (int $id_user, $at, string $text, ?string $origin = null, bool $flash = false, array $numbers = [], array $contacts_ids = [], array $groups_ids = [], array $conditional_group_ids = [])
public function create(int $id_user, $at, string $text, ?string $origin = null, bool $flash = false, array $numbers = [], array $contacts_ids = [], array $groups_ids = [], array $conditional_group_ids = [])
{
$scheduled = [
$scheduled = [
'id_user' => $id_user,
'at' => $at,
'text' => $text,
@ -58,14 +50,14 @@ namespace controllers\internals;
return false;
}
}
$id_scheduled = $this->get_model()->insert($scheduled);
if (!$id_scheduled)
{
$date = date('Y-m-d H:i:s');
$internal_event = new Event($this->bdd);
$internal_event->create($id_user, 'SCHEDULED_ADD', 'Ajout d\'un Sms pour le ' . $date . '.');
$internal_event->create($id_user, 'SCHEDULED_ADD', 'Ajout d\'un Sms pour le '.$date.'.');
return false;
}
@ -97,7 +89,7 @@ namespace controllers\internals;
$this->get_model()->insert_scheduled_group_relation($id_scheduled, $group_id);
}
$internal_conditional_group = new ConditionalGroup($this->bdd);
foreach ($conditional_group_ids as $conditional_group_id)
{
@ -113,24 +105,25 @@ namespace controllers\internals;
return $id_scheduled;
}
/**
* Update a scheduled
* @param int $id_user : User to insert scheduled for
* Update a scheduled.
*
* @param int $id_user : User to insert scheduled for
* @param int $id_scheduled : Scheduled id
* @param $at : Scheduled date to send
* @param string $text : Text of the message
* @param ?string $origin : Origin number of the message, null by default
* @param bool $flash : Is the sms a flash sms, by default false
* @param array $numbers : Numbers to send message to
* @param array $contacts_ids : Contact ids to send message to
* @param array $groups_ids : Group ids to send message to
* @param array $conditional_group_ids : Conditional Groups ids to send message to
* @param string $text : Text of the message
* @param ?string $origin : Origin number of the message, null by default
* @param bool $flash : Is the sms a flash sms, by default false
* @param array $numbers : Numbers to send message to
* @param array $contacts_ids : Contact ids to send message to
* @param array $groups_ids : Group ids to send message to
* @param array $conditional_group_ids : Conditional Groups ids to send message to
*
* @return bool : false on error, new id on success
*/
public function update_for_user (int $id_user, int $id_scheduled, $at, string $text, ?string $origin = null, bool $flash = false, array $numbers = [], array $contacts_ids = [], array $groups_ids = [], array $conditional_group_ids = [])
public function update_for_user(int $id_user, int $id_scheduled, $at, string $text, ?string $origin = null, bool $flash = false, array $numbers = [], array $contacts_ids = [], array $groups_ids = [], array $conditional_group_ids = [])
{
$scheduled = [
$scheduled = [
'id_user' => $id_user,
'at' => $at,
'text' => $text,
@ -138,7 +131,6 @@ namespace controllers\internals;
'flash' => $flash,
];
if ($origin)
{
$internal_phone = new Phone($this->bdd);
@ -161,7 +153,7 @@ namespace controllers\internals;
{
$this->get_model()->insert_scheduled_number($id_scheduled, $number);
}
$internal_contact = new Contact($this->bdd);
foreach ($contacts_ids as $contact_id)
{
@ -185,7 +177,7 @@ namespace controllers\internals;
$this->get_model()->insert_scheduled_group_relation($id_scheduled, $group_id);
}
$internal_conditional_group = new ConditionalGroup($this->bdd);
foreach ($conditional_group_ids as $conditional_group_id)
{
@ -201,28 +193,29 @@ namespace controllers\internals;
return true;
}
/**
* Get messages scheduled before a date for a number and a user
* Get messages scheduled before a date for a number and a user.
*
* @param int $id_user : User id
* @param $date : Date before which we want messages
* @param string $number : Number for which we want messages
*
* @return array
*/
public function gets_before_date_for_number_and_user (int $id_user, $date, string $number)
public function gets_before_date_for_number_and_user(int $id_user, $date, string $number)
{
return $this->get_model()->gets_before_date_for_number_and_user($id_user, $date, $number);
}
/**
* Get all messages to send and the number to use to send theme
* Get all messages to send and the number to use to send theme.
*
* @return array : [['id_scheduled', 'text', 'origin', 'destination', 'flash'], ...]
*/
public function get_smss_to_send ()
public function get_smss_to_send()
{
$smss_to_send = [];
$internal_templating = new \controllers\internals\Templating();
$internal_setting = new \controllers\internals\Setting($this->bdd);
$internal_group = new \controllers\internals\Group($this->bdd);
@ -240,7 +233,7 @@ namespace controllers\internals;
if (!isset($users_settings[$scheduled['id_user']]))
{
$users_settings[$scheduled['id_user']] = [];
$settings = $internal_setting->gets_for_user($scheduled['id_user']);
foreach ($settings as $name => $value)
{
@ -251,7 +244,7 @@ namespace controllers\internals;
if (!isset($users_phones[$scheduled['id_user']]))
{
$phones = $internal_phone->gets_for_user($scheduled['id_user']);
$users_phones[$scheduled['id_user']] = $phones ? $phones : [];;
$users_phones[$scheduled['id_user']] = $phones ? $phones : [];
}
$messages = [];
@ -268,7 +261,7 @@ namespace controllers\internals;
'flash' => $scheduled['flash'],
];
if ($message['origin'] == null)
if (null === $message['origin'])
{
$k = array_rand($users_phones[$scheduled['id_user']]);
$rnd_phone = $users_phones[$scheduled['id_user']][$k];
@ -294,7 +287,6 @@ namespace controllers\internals;
$messages[] = $message;
}
//Add messages for contacts
$contacts = $this->get_contacts($scheduled['id']);
@ -329,14 +321,14 @@ namespace controllers\internals;
'destination' => $number['number'],
'flash' => $scheduled['flash'],
];
if ($message['origin'] == null)
if (null === $message['origin'])
{
$k = array_rand($users_phones[$scheduled['id_user']]);
$rnd_phone = $users_phones[$scheduled['id_user']][$k];
$message['origin'] = $rnd_phone['number'];
}
if ((int) ($users_settings[$scheduled['id_user']]['templating'] ?? false))
{
$contact['datas'] = json_decode($contact['datas'], true);
@ -361,7 +353,7 @@ namespace controllers\internals;
foreach ($messages as $message)
{
//Remove empty messages
if (trim($message['text']) == '')
if ('' === trim($message['text']))
{
continue;
}
@ -372,11 +364,12 @@ namespace controllers\internals;
return $smss_to_send;
}
/**
* Return numbers for a scheduled message
* Return numbers for a scheduled message.
*
* @param int $id_scheduled : Scheduled id
*
* @return array
*/
public function get_numbers(int $id_scheduled)
@ -384,10 +377,11 @@ namespace controllers\internals;
return $this->get_model()->get_numbers($id_scheduled);
}
/**
* Return contacts for a scheduled message
* Return contacts for a scheduled message.
*
* @param int $id_scheduled : Scheduled id
*
* @return array
*/
public function get_contacts(int $id_scheduled)
@ -395,25 +389,39 @@ namespace controllers\internals;
return $this->get_model()->get_contacts($id_scheduled);
}
/**
* Return groups for a scheduled message
* Return groups for a scheduled message.
*
* @param int $id_scheduled : Scheduled id
*
* @return array
*/
public function get_groups(int $id_scheduled)
{
return $this->get_model()->get_groups($id_scheduled);
}
/**
* Return conditional groups for a scheduled message
* Return conditional groups for a scheduled message.
*
* @param int $id_scheduled : Scheduled id
*
* @return array
*/
public function get_conditional_groups(int $id_scheduled)
{
return $this->get_model()->get_conditional_groups($id_scheduled);
}
/**
* Get the model for the Controller.
*
* @return \descartes\Model
*/
protected function get_model(): \descartes\Model
{
$this->model = $this->model ?? new \models\Scheduled($this->bdd);
return $this->model;
}
}

View file

@ -13,35 +13,27 @@ namespace controllers\internals;
class Sended extends StandardController
{
protected $model = null;
protected $model;
/**
* Get the model for the Controller
* @return \descartes\Model
*/
protected function get_model () : \descartes\Model
{
$this->model = $this->model ?? new \models\Sended($this->bdd);
return $this->model;
}
/**
* Create a sended
* Create a sended.
*
* @param $at : Reception date
* @param $text : Text of the message
* @param string $origin : Number of the sender
* @param string $origin : Number of the sender
* @param string $destination : Number of the receiver
* @param string $uid : Uid of the sms on the adapter service used
* @param string $adapter : Name of the adapter service used to send the message
* @param bool $flash : Is the sms a flash
* @param string $status : Status of a the sms. By default 'unknown'
* @param string $uid : Uid of the sms on the adapter service used
* @param string $adapter : Name of the adapter service used to send the message
* @param bool $flash : Is the sms a flash
* @param string $status : Status of a the sms. By default 'unknown'
*
* @return bool : false on error, new sended id else
*/
public function create ($at, string $text, string $origin, string $destination, string $uid, string $adapter, bool $flash = false, ?string $status = 'unknown') : bool
public function create($at, string $text, string $origin, string $destination, string $uid, string $adapter, bool $flash = false, ?string $status = 'unknown'): bool
{
$sended = [
$sended = [
'at' => $at,
'text' => $text,
'text' => $text,
'origin' => $origin,
'destination' => $destination,
'uid' => $uid,
@ -53,26 +45,27 @@ namespace controllers\internals;
return (bool) $this->get_model()->insert($sended);
}
/**
* Update a sended for a user
* @param int $id_user : user id
* Update a sended for a user.
*
* @param int $id_user : user id
* @param int $id_sended : Sended id
* @param $at : Reception date
* @param $text : Text of the message
* @param string $origin : Number of the sender
* @param string $destination : Number of the receiver
* @param string $uid : Uid of the sms on the adapter service used
* @param string $adapter : Name of the adapter service used to send the message
* @param bool $flash : Is the sms a flash
* @param ?string $status : Status of a the sms. By default null -> unknown
* @param string $origin : Number of the sender
* @param string $destination : Number of the receiver
* @param string $uid : Uid of the sms on the adapter service used
* @param string $adapter : Name of the adapter service used to send the message
* @param bool $flash : Is the sms a flash
* @param ?string $status : Status of a the sms. By default null -> unknown
*
* @return bool : false on error, true on success
*/
public function update_for_user (int $id_user, int $id_sended, $at, string $text, string $origin, string $destination, string $uid, string $adapter, bool $flash = false, ?string $status = null) : bool
public function update_for_user(int $id_user, int $id_sended, $at, string $text, string $origin, string $destination, string $uid, string $adapter, bool $flash = false, ?string $status = null): bool
{
$sended = [
$sended = [
'at' => $at,
'text' => $text,
'text' => $text,
'origin' => $origin,
'destination' => $destination,
'uid' => $uid,
@ -83,69 +76,74 @@ namespace controllers\internals;
return (bool) $this->get_model()->update_for_user($id_user, $id_sended, $sended);
}
/**
* Update a sended status for a user
* @param int $id_user : user id
* @param int $id_sended : Sended id
* @param string $status : Status of a the sms (unknown, delivered, failed)
* Update a sended status for a user.
*
* @param int $id_user : user id
* @param int $id_sended : Sended id
* @param string $status : Status of a the sms (unknown, delivered, failed)
*
* @return bool : false on error, true on success
*/
public function update_status_for_user (int $id_user, int $id_sended, string $status) : bool
public function update_status_for_user(int $id_user, int $id_sended, string $status): bool
{
$sended = [
$sended = [
'status' => $status,
];
return (bool) $this->get_model()->update_for_user($id_user, $id_sended, $sended);
}
/**
* Update a sended status for a sended
* @param int $id_sended : Sended id
* @param string $status : Status of a the sms (unknown, delivered, failed)
* Update a sended status for a sended.
*
* @param int $id_sended : Sended id
* @param string $status : Status of a the sms (unknown, delivered, failed)
*
* @return bool : false on error, true on success
*/
public function update_status (int $id_sended, string $status) : bool
public function update_status(int $id_sended, string $status): bool
{
$sended = [
$sended = [
'status' => $status,
];
return (bool) $this->get_model()->update($id_sended, $sended);
}
/**
* Return x last sendeds message for a user, order by date
* @param int $id_user : User id
* Return x last sendeds message for a user, order by date.
*
* @param int $id_user : User id
* @param int $nb_entry : Number of sendeds messages to return
* @return array
*
* @return array
*/
public function get_lasts_by_date_for_user(int $id_user, int $nb_entry)
{
return $this->get_model()->get_lasts_by_date_for_user($id_user, $nb_entry);
}
/**
* Return sendeds for a destination and a user
* @param int $id_user : User id
* @param string $origin : Number who sent the message
* Return sendeds for a destination and a user.
*
* @param int $id_user : User id
* @param string $origin : Number who sent the message
*
* @return array
*/
public function gets_by_destination_and_user(int $id_user, string $origin)
{
return $this->get_model()->gets_by_destination_and_user($id_user, $origin);
}
/**
* Return sended for an uid and an adapter
* @param string $uid : Uid of the sended
* Return sended for an uid and an adapter.
*
* @param string $uid : Uid of the sended
* @param string $adapter : Adapter used to send the message
*
* @return array
*/
public function get_by_uid_and_adapter(string $uid, string $adapter)
@ -153,11 +151,12 @@ namespace controllers\internals;
return $this->get_model()->get_by_uid_and_adapter($uid, $adapter);
}
/**
* Get number of sended SMS for every date since a date for a specific user
* @param int $id_user : user id
* @param \DateTime $date : Date since which we want the messages
* Get number of sended SMS for every date since a date for a specific user.
*
* @param int $id_user : user id
* @param \DateTime $date : Date since which we want the messages
*
* @return array
*/
public function count_by_day_since_for_user(int $id_user, $date)
@ -172,16 +171,29 @@ namespace controllers\internals;
return $return;
}
/**
* Find last sended message for a destination and user
* @param int $id_user : User id
* Find last sended message for a destination and user.
*
* @param int $id_user : User id
* @param string $destination : Destination number
*
* @return array
*/
public function get_last_for_destination_and_user (int $id_user, string $destination)
public function get_last_for_destination_and_user(int $id_user, string $destination)
{
return $this->get_model()->get_last_for_destination_and_user($id_user, $destination);
}
/**
* Get the model for the Controller.
*
* @return \descartes\Model
*/
protected function get_model(): \descartes\Model
{
$this->model = $this->model ?? new \models\Sended($this->bdd);
return $this->model;
}
}

View file

@ -13,25 +13,16 @@ namespace controllers\internals;
class Setting extends StandardController
{
protected $model = null;
/**
* Get the model for the Controller
* @return \descartes\Model
*/
protected function get_model () : \descartes\Model
{
$this->model = $this->model ?? new \models\Setting($this->bdd);
return $this->model;
}
protected $model;
/**
* Return all settings of a user.
*
* @param int $id_user : user id
*
* @return array
*/
public function gets_for_user (int $id_user)
public function gets_for_user(int $id_user)
{
$settings = $this->get_model()->gets_for_user($id_user);
$settings_array = [];
@ -44,28 +35,30 @@ namespace controllers\internals;
return $settings_array;
}
/**
* Update a setting by his name and user id.
* @param int $id_user : user id
* @param string $name : setting name
* @param mixed $value
*
* @param int $id_user : user id
* @param string $name : setting name
* @param mixed $value
*
* @return int : number of modified lines
*/
public function update_for_user (int $id_user, string $name, $value) : bool
public function update_for_user(int $id_user, string $name, $value): bool
{
return (bool) $this->get_model()->update_by_name_for_user($id_user, $name, $value);
}
/**
* Create a new setting
* @param int $id_user : user id
* @param string $name : setting name
* @param mixed $value : value of the setting
* @return bool
* Create a new setting.
*
* @param int $id_user : user id
* @param string $name : setting name
* @param mixed $value : value of the setting
*
* @return bool
*/
public function create (int $id_user, string $name, $value) : bool
public function create(int $id_user, string $name, $value): bool
{
$setting = [
'id_user' => $id_user,
@ -76,10 +69,11 @@ namespace controllers\internals;
return (bool) $this->get_model()->insert($setting);
}
/**
* Generate and insert default settings for a user
* Generate and insert default settings for a user.
*
* @param int $id_user : user id
*
* @return bool
*/
public function create_defaults_for_user(int $id_user)
@ -93,4 +87,16 @@ namespace controllers\internals;
return $all_success;
}
/**
* Get the model for the Controller.
*
* @return \descartes\Model
*/
protected function get_model(): \descartes\Model
{
$this->model = $this->model ?? new \models\Setting($this->bdd);
return $this->model;
}
}

View file

@ -13,23 +13,14 @@ namespace controllers\internals;
class SmsStop extends StandardController
{
protected $model = null;
protected $model;
/**
* Get the model for the Controller
* @return \descartes\Model
*/
protected function get_model () : \descartes\Model
{
$this->model = $this->model ?? new \models\SmsStop($this->bdd);
return $this->model;
}
/**
* Create a new smsstop
* @param int $id_user : User id
* @param string $number : Number to stop smss for
* Create a new smsstop.
*
* @param int $id_user : User id
* @param string $number : Number to stop smss for
*
* @return mixed bool|int : False if cannot create smsstop, id of the new smsstop else
*/
public function create(int $id_user, string $number)
@ -41,13 +32,14 @@ namespace controllers\internals;
return $this->get_model()->insert($smsstop);
}
/**
* Update a smsstop
* @param int $id_user : User id
* @param int $id_smsstop : SmsStop id
* @param string $number : Number to stop smss for
* Update a smsstop.
*
* @param int $id_user : User id
* @param int $id_smsstop : SmsStop id
* @param string $number : Number to stop smss for
*
* @return mixed bool|int : False if cannot create smsstop, id of the new smsstop else
*/
public function update_for_user(int $id_user, int $id_smsstop, string $number)
@ -59,15 +51,28 @@ namespace controllers\internals;
return $this->get_model()->update_for_user($id_user, $id_smsstop, $datas);
}
/**
* Return a smsstop by his number and user
* @param int $id_user : user id
* @param string $number : phone number
* Return a smsstop by his number and user.
*
* @param int $id_user : user id
* @param string $number : phone number
*
* @return array
*/
public function get_by_number_for_user (int $id_user, string $number)
public function get_by_number_for_user(int $id_user, string $number)
{
return $this->get_model()->get_by_number_for_user($id_user, $number);
}
/**
* Get the model for the Controller.
*
* @return \descartes\Model
*/
protected function get_model(): \descartes\Model
{
$this->model = $this->model ?? new \models\SmsStop($this->bdd);
return $this->model;
}
}

View file

@ -19,111 +19,120 @@ namespace controllers\internals;
}
/**
* Get the model for the Controller
* @return \descartes\Model
*/
abstract protected function get_model () : \descartes\Model;
/**
* Return all the entries
* Return all the entries.
*
* @return array
*/
public function get_all ()
public function get_all()
{
return $this->get_model()->get_all();
}
/**
* Return a entry by his id
* Return a entry by his id.
*
* @param int $id : Entry id
*
* @return array
*/
public function get (int $id)
public function get(int $id)
{
return $this->get_model()->get($id);
}
/**
* Return a entry by his id and a user
* Return a entry by his id and a user.
*
* @param int $id_user : Entry id
* @param int $id : Entry id
* @param int $id : Entry id
*
* @return array
*/
public function get_for_user (int $id_user, int $id)
public function get_for_user(int $id_user, int $id)
{
return $this->get_model()->get_for_user($id_user, $id);
}
/**
* Return all entries for a user
* Return all entries for a user.
*
* @param int $id_user : Entry id
*
* @return array
*/
public function gets_for_user (int $id_user)
public function gets_for_user(int $id_user)
{
return $this->get_model()->gets_for_user($id_user);
}
/**
* Return the list of entries for a user
* @param int $id_user : User id
* Return the list of entries for a user.
*
* @param int $id_user : User id
* @param ?int $nb_entry : Number of entry to return
* @param ?int $page : Pagination, used to calcul offset, $nb_entry * $page
*
* @return array : Entrys list
*/
public function list_for_user (int $id_user, ?int $nb_entry = null, ?int $page = null)
public function list_for_user(int $id_user, ?int $nb_entry = null, ?int $page = null)
{
return $this->get_model()->list_for_user($id_user, $nb_entry, $nb_entry * $page);
}
/**
* Return a list of entries in a group of ids and for a user
* @param int $id_user : user id
* @param array $ids : ids of entries to find
* Return a list of entries in a group of ids and for a user.
*
* @param int $id_user : user id
* @param array $ids : ids of entries to find
*
* @return array
*/
public function gets_in_for_user (int $id_user, array $ids)
public function gets_in_for_user(int $id_user, array $ids)
{
return $this->get_model()->gets_in_for_user($id_user, $ids);
}
/**
* Delete a entry by his id for a user
* Delete a entry by his id for a user.
*
* @param int $id_user : User id
* @param int $id : Entry id
* @param int $id : Entry id
*
* @return int : Number of removed rows
*/
public function delete_for_user(int $id_user, int $id)
{
return $this->get_model()->delete_for_user($id_user, $id);
}
/**
* Delete a entry by his id
* Delete a entry by his id.
*
* @param int $id : Entry id
*
* @return int : Number of removed rows
*/
public function delete (int $id)
public function delete(int $id)
{
return $this->get_model()->delete($id);
}
/**
* Count number of entry for a user
* Count number of entry for a user.
*
* @param int $id_user : User id
*
* @return int : number of entries
*/
public function count_for_user(int $id_user)
{
return $this->get_model()->count_for_user($id_user);
}
/**
* Get the model for the Controller.
*
* @return \descartes\Model
*/
abstract protected function get_model(): \descartes\Model;
}

View file

@ -13,16 +13,16 @@ namespace controllers\internals;
/**
* Templating questions relative class
* Not a standard controller as it's not linked to a model in any way
* Not a standard controller as it's not linked to a model in any way.
*/
class Templating
{
/**
* Twig environment
* Twig environment.
*/
private $sandbox;
public function __construct ()
public function __construct()
{
$tags = [
'if',
@ -45,23 +45,24 @@ namespace controllers\internals;
$properties = [];
$functions = [
'date', 'max', 'min', 'random',
'range',
'range',
];
$policy = new \Twig\Sandbox\SecurityPolicy($tags, $filters, $methods, $properties, $functions);
$this->sandbox = new \Twig\Extension\SandboxExtension($policy, true);
}
/**
* Render a string as a twig template
* Render a string as a twig template.
*
* @param string $template : Template string
* @param array $datas : Datas to pass to the template
* @param array $datas : Datas to pass to the template
*
* @return array : keys, success, error, result
*/
public function render (string $template, array $datas = [])
public function render(string $template, array $datas = [])
{
try
try
{
$loader = new \Twig\Loader\ArrayLoader([
'template' => $template,
@ -69,7 +70,7 @@ namespace controllers\internals;
$twig = new \Twig\Environment($loader);
$result = $twig->render('template', $datas);
return [
'success' => true,
'result' => $result,
@ -83,5 +84,4 @@ namespace controllers\internals;
];
}
}
}

View file

@ -13,7 +13,7 @@ namespace controllers\internals;
/**
* Some tools frequently used.
* Not a standard controller as it's not linked to a model in any way
* Not a standard controller as it's not linked to a model in any way.
*/
class Tool extends \descartes\InternalController
{
@ -40,7 +40,7 @@ namespace controllers\internals;
return $phone_number_util->format($phone_number_o, \libphonenumber\PhoneNumberFormat::E164);
}
catch(\Exception $e)
catch (\Exception $e)
{
return false;
}
@ -62,22 +62,25 @@ namespace controllers\internals;
return $phone_number_util->format($phone_number_o, \libphonenumber\PhoneNumberFormat::INTERNATIONAL);
}
catch(\Exception $e)
catch (\Exception $e)
{
return $number;
}
}
/**
* Format a number and make a link to a discussion with this number
* Format a number and make a link to a discussion with this number.
*
* @param string $number : Number to format and make a link for
*
* @return string : Link to the number
*/
public static function phone_link ($number)
public static function phone_link($number)
{
$number_format = \controllers\internals\Tool::phone_format($number);
$number_format = self::phone_format($number);
$url = \descartes\Router::url('Discussion', 'show', ['number' => $number]);
return '<a href="' . self::s($url, false, true, false) . '">' . self::s($number_format, false, true, false) . '</a>';
return '<a href="'.self::s($url, false, true, false).'">'.self::s($number_format, false, true, false).'</a>';
}
/**
@ -132,7 +135,6 @@ namespace controllers\internals;
return $objectDate && $objectDate->format($format) === $date;
}
/**
* Cette fonction retourne un mot de passe généré aléatoirement.
*
@ -206,8 +208,10 @@ namespace controllers\internals;
}
/**
* Allow to read an uploaded file
* Allow to read an uploaded file.
*
* @param array $file : The array extracted from $_FILES['file']
*
* @return array : ['success' => bool, 'content' => file handler | error message, 'error_code' => $file['error']]
*/
public static function read_uploaded_file(array $file)
@ -219,40 +223,41 @@ namespace controllers\internals;
'mime_type' => false,
];
if ($file['error'] !== UPLOAD_ERR_OK)
if (UPLOAD_ERR_OK !== $file['error'])
{
switch ($file['error'])
{
case UPLOAD_ERR_INI_SIZE :
$result['content'] = 'Impossible de télécharger le fichier car il dépasse les ' . ini_get('upload_max_filesize') / (1000 * 1000) . ' Mégaoctets.';
case UPLOAD_ERR_INI_SIZE:
$result['content'] = 'Impossible de télécharger le fichier car il dépasse les '.ini_get('upload_max_filesize') / (1000 * 1000).' Mégaoctets.';
break;
case UPLOAD_ERR_FORM_SIZE :
case UPLOAD_ERR_FORM_SIZE:
$result['content'] = 'Le fichier dépasse la limite de taille.';
break;
case UPLOAD_ERR_PARTIAL :
case UPLOAD_ERR_PARTIAL:
$result['content'] = 'L\'envoi du fichier a été interrompu.';
break;
case UPLOAD_ERR_NO_FILE :
break;
case UPLOAD_ERR_NO_FILE:
$result['content'] = 'Aucun fichier n\'a été envoyé.';
break;
case UPLOAD_ERR_NO_TMP_DIR :
case UPLOAD_ERR_NO_TMP_DIR:
$result['content'] = 'Le serveur ne dispose pas de fichier temporaire permettant l\'envoi de fichiers.';
break;
case UPLOAD_ERR_CANT_WRITE :
break;
case UPLOAD_ERR_CANT_WRITE:
$result['content'] = 'Impossible d\'envoyer le fichier car il n\'y a plus de place sur le serveur.';
break;
case UPLOAD_ERR_EXTENSION :
break;
case UPLOAD_ERR_EXTENSION:
$result['content'] = 'Le serveur a interrompu l\'envoi du fichier.';
break;
}
return $result;
return $result;
}
$tmp_filename = $file['tmp_name'] ?? false;
@ -261,7 +266,7 @@ namespace controllers\internals;
return $result;
}
$result['mime_type'] = mime_content_type($tmp_filename) == 'text/plain' ? $file['type'] : mime_content_type($tmp_filename);
$result['mime_type'] = 'text/plain' === mime_content_type($tmp_filename) ? $file['type'] : mime_content_type($tmp_filename);
$file_handler = fopen($tmp_filename, 'r');
$result['success'] = true;
@ -269,11 +274,12 @@ namespace controllers\internals;
return $result;
}
/**
* Allow to upload file
* Allow to upload file.
*
* @param array $file : The array extracted from $_FILES['file']
*
* @return array : ['success' => bool, 'content' => file path | error message, 'error_code' => $file['error']]
*/
public static function upload_file(array $file)
@ -284,40 +290,41 @@ namespace controllers\internals;
'error_code' => $file['error'] ?? 99,
];
if ($file['error'] !== UPLOAD_ERR_OK)
if (UPLOAD_ERR_OK !== $file['error'])
{
switch ($file['error'])
{
case UPLOAD_ERR_INI_SIZE :
$result['content'] = 'Impossible de télécharger le fichier car il dépasse les ' . ini_get('upload_max_filesize') / (1000 * 1000) . ' Mégaoctets.';
case UPLOAD_ERR_INI_SIZE:
$result['content'] = 'Impossible de télécharger le fichier car il dépasse les '.ini_get('upload_max_filesize') / (1000 * 1000).' Mégaoctets.';
break;
case UPLOAD_ERR_FORM_SIZE :
case UPLOAD_ERR_FORM_SIZE:
$result['content'] = 'Le fichier dépasse la limite de taille.';
break;
case UPLOAD_ERR_PARTIAL :
case UPLOAD_ERR_PARTIAL:
$result['content'] = 'L\'envoi du fichier a été interrompu.';
break;
case UPLOAD_ERR_NO_FILE :
break;
case UPLOAD_ERR_NO_FILE:
$result['content'] = 'Aucun fichier n\'a été envoyé.';
break;
case UPLOAD_ERR_NO_TMP_DIR :
case UPLOAD_ERR_NO_TMP_DIR:
$result['content'] = 'Le serveur ne dispose pas de fichier temporaire permettant l\'envoi de fichiers.';
break;
case UPLOAD_ERR_CANT_WRITE :
break;
case UPLOAD_ERR_CANT_WRITE:
$result['content'] = 'Impossible d\'envoyer le fichier car il n\'y a plus de place sur le serveur.';
break;
case UPLOAD_ERR_EXTENSION :
break;
case UPLOAD_ERR_EXTENSION:
$result['content'] = 'Le serveur a interrompu l\'envoi du fichier.';
break;
}
return $result;
return $result;
}
$tmp_filename = $file['tmp_name'] ?? false;
@ -332,13 +339,13 @@ namespace controllers\internals;
return $result;
}
$new_file_path = PWD_DATAS . '/' . $md5_filename;
$new_file_path = PWD_DATAS.'/'.$md5_filename;
if (file_exists($new_file_path))
{
$result['success'] = true;
$result['content'] = $new_file_path;
return $result;
}
@ -346,6 +353,7 @@ namespace controllers\internals;
if (!$success)
{
$result['content'] = 'Impossible d\'écrire le fichier sur le serveur.';
return $result;
}

View file

@ -12,7 +12,7 @@
namespace controllers\internals;
/**
* Methods to manage user. Not a standard controller as it has nothing to do with user based restrictions and must be usable only by admin
* Methods to manage user. Not a standard controller as it has nothing to do with user based restrictions and must be usable only by admin.
*/
class User extends \descartes\InternalController
{
@ -52,9 +52,8 @@ namespace controllers\internals;
return $this->model_user->remove($id);
}
/**
* Check user credentials
* Check user credentials.
*
* @param string $email : User email
* @param string $password : User password
@ -92,7 +91,6 @@ namespace controllers\internals;
return (bool) $this->model_user->update_password($id, $password);
}
/**
* Update user email.
*
@ -105,12 +103,11 @@ namespace controllers\internals;
{
return (bool) $this->model_user->update_email($id, $email);
}
/**
* Update user api key.
*
* @param string $id : user id
* @param string $id : user id
* @param ?string $api_key : new api key
*
* @return mixed : false on error, else new api key;
@ -129,7 +126,8 @@ namespace controllers\internals;
}
/**
* Get a user by his email address
* Get a user by his email address.
*
* @param string $email : User email
*
* @return mixed boolean | array : false if cannot find user for this email, the user else
@ -138,21 +136,22 @@ namespace controllers\internals;
{
return $this->model_user->get_by_email($email);
}
/**
* Find a user by his id
* Find a user by his id.
*
* @param string $id : User id
*
* @return mixed array
*/
public function get ($id)
public function get($id)
{
return $this->model_user->get($id);
}
/**
* Get a user by his api_key address
* Get a user by his api_key address.
*
* @param string $api_key : User api key
*
* @return mixed boolean | array : false if cannot find user for this api key, the user else
@ -163,11 +162,13 @@ namespace controllers\internals;
}
/**
* Update a user by his id
* Update a user by his id.
*
* @param mixed $id
* @param mixed $email
* @param mixed $password
* @param mixed $admin
* @param mixed $api_key
*
* @return int : Number of modified user
*/
@ -184,12 +185,12 @@ namespace controllers\internals;
}
/**
* Create a new user
* Create a new user.
*
* @param mixed $email
* @param mixed $password
* @param mixed $admin
* @param ?string $api_key : The api key of the user, if null generate randomly
* @param mixed $email
* @param mixed $password
* @param mixed $admin
* @param ?string $api_key : The api key of the user, if null generate randomly
*
* @return mixed bool|int : false on error, id of the new user else
*/
@ -214,18 +215,19 @@ namespace controllers\internals;
if (!$success)
{
$this->delete($new_user_id);
return false;
}
return $new_user_id;
}
/**
* Generate a random api key
* Generate a random api key.
*
* @return string : The api key
*/
public function generate_random_api_key () : string
public function generate_random_api_key(): string
{
return bin2hex(random_bytes(16));
}

View file

@ -13,24 +13,15 @@ namespace controllers\internals;
class Webhook extends StandardController
{
protected $model = null;
protected $model;
/**
* Get the model for the Controller
* @return \descartes\Model
*/
protected function get_model () : \descartes\Model
{
$this->model = $this->model ?? new \models\Webhook($this->bdd);
return $this->model;
}
/**
* Create a new webhook
* @param int $id_user : User id
* @param string $url : Webhook url
* @param string $type : Webhook type
* Create a new webhook.
*
* @param int $id_user : User id
* @param string $url : Webhook url
* @param string $type : Webhook type
*
* @return mixed bool|int : False if cannot create webhook, id of the new webhook else
*/
public function create(int $id_user, string $url, string $type)
@ -46,17 +37,18 @@ namespace controllers\internals;
{
return false;
}
return $result;
}
/**
* Update a webhook
* @param int $id_user : User id
* @param int $id : Webhook id
* @param string $url : Webhook url
* @param string $type : Webhook type
* Update a webhook.
*
* @param int $id_user : User id
* @param int $id : Webhook id
* @param string $url : Webhook url
* @param string $type : Webhook type
*
* @return mixed bool|int : False if cannot create webhook, id of the new webhook else
*/
public function update_for_user(int $id_user, int $id, string $url, string $type)
@ -69,15 +61,28 @@ namespace controllers\internals;
return $this->get_model()->update_for_user($id_user, $id, $datas);
}
/**
* Find all webhooks for a user and for a type of webhook
* @param int $id_user : User id
* @param string $type : Webhook type
* Find all webhooks for a user and for a type of webhook.
*
* @param int $id_user : User id
* @param string $type : Webhook type
*
* @return array
*/
public function gets_for_type_and_user (int $id_user, string $type)
public function gets_for_type_and_user(int $id_user, string $type)
{
return $this->get_model()->gets_for_type_and_user($id_user, $type);
}
/**
* Get the model for the Controller.
*
* @return \descartes\Model
*/
protected function get_model(): \descartes\Model
{
$this->model = $this->model ?? new \models\Webhook($this->bdd);
return $this->model;
}
}