diff options
author | Marvin Borner | 2018-05-23 22:23:28 +0200 |
---|---|---|
committer | Marvin Borner | 2018-05-23 22:23:28 +0200 |
commit | b66a61addb6c8e66cb26fcf74b532d68891267e4 (patch) | |
tree | 05e9449ff25bdc98f68105f41923ccb9f6ef5095 /main/app/sprinkles/core/src/Alert | |
parent | 1d4ef435177a5f9b6d1a289800d933e49be0c550 (diff) |
Refactored code, many fixes and improvements in chat backend+frontend
Diffstat (limited to 'main/app/sprinkles/core/src/Alert')
-rw-r--r-- | main/app/sprinkles/core/src/Alert/AlertStream.php | 28 | ||||
-rw-r--r-- | main/app/sprinkles/core/src/Alert/CacheAlertStream.php | 43 | ||||
-rw-r--r-- | main/app/sprinkles/core/src/Alert/SessionAlertStream.php | 31 |
3 files changed, 45 insertions, 57 deletions
diff --git a/main/app/sprinkles/core/src/Alert/AlertStream.php b/main/app/sprinkles/core/src/Alert/AlertStream.php index 3946cbf..adb9b5b 100644 --- a/main/app/sprinkles/core/src/Alert/AlertStream.php +++ b/main/app/sprinkles/core/src/Alert/AlertStream.php @@ -5,6 +5,7 @@ * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ + namespace UserFrosting\Sprinkle\Core\Alert; use UserFrosting\Fortress\ServerSideValidator; @@ -28,13 +29,12 @@ abstract class AlertStream /** * @var UserFrosting\I18n\MessageTranslator|null */ - protected $messageTranslator = null; + protected $messageTranslator = NULL; /** * Create a new message stream. */ - public function __construct($messagesKey, $translator = null) - { + public function __construct($messagesKey, $translator = NULL) { $this->messagesKey = $messagesKey; $this->setTranslator($translator); @@ -45,8 +45,7 @@ abstract class AlertStream * * @param UserFrosting\I18n\MessageTranslator $translator A MessageTranslator to be used to translate messages when added via `addMessageTranslated`. */ - public function setTranslator($translator) - { + public function setTranslator($translator) { $this->messageTranslator = $translator; return $this; } @@ -58,8 +57,7 @@ abstract class AlertStream * @param string $message The message to be added to the message stream. * @return MessageStream this MessageStream object. */ - public function addMessage($type, $message) - { + public function addMessage($type, $message) { $messages = $this->messages(); $messages[] = array( "type" => $type, @@ -77,9 +75,8 @@ abstract class AlertStream * @param array[string] $placeholders An optional hash of placeholder names => placeholder values to substitute into the translated message. * @return MessageStream this MessageStream object. */ - public function addMessageTranslated($type, $messageId, $placeholders = array()) - { - if (!$this->messageTranslator){ + public function addMessageTranslated($type, $messageId, $placeholders = array()) { + if (!$this->messageTranslator) { throw new \RuntimeException("No translator has been set! Please call MessageStream::setTranslator first."); } @@ -94,8 +91,7 @@ abstract class AlertStream * * @return array An array of messages, each of which is itself an array containing "type" and "message" fields. */ - public function getAndClearMessages() - { + public function getAndClearMessages() { $messages = $this->messages(); $this->resetMessageStream(); return $messages; @@ -106,10 +102,9 @@ abstract class AlertStream * * @param ServerSideValidator $validator */ - public function addValidationErrors(ServerSideValidator $validator) - { + public function addValidationErrors(ServerSideValidator $validator) { foreach ($validator->errors() as $idx => $field) { - foreach($field as $eidx => $error) { + foreach ($field as $eidx => $error) { $this->addMessage("danger", $error); } } @@ -120,8 +115,7 @@ abstract class AlertStream * * @return MessageTranslator The translator for this message stream. */ - public function translator() - { + public function translator() { return $this->messageTranslator; } diff --git a/main/app/sprinkles/core/src/Alert/CacheAlertStream.php b/main/app/sprinkles/core/src/Alert/CacheAlertStream.php index 1fd5131..f3f6489 100644 --- a/main/app/sprinkles/core/src/Alert/CacheAlertStream.php +++ b/main/app/sprinkles/core/src/Alert/CacheAlertStream.php @@ -5,6 +5,7 @@ * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ + namespace UserFrosting\Sprinkle\Core\Alert; use Illuminate\Cache\Repository as Cache; @@ -18,30 +19,29 @@ use UserFrosting\Support\Repository\Repository; * the alerts. Note that the tags are added each time instead of the * constructor since the session_id can change when the user logs in or out * - * @author Louis Charette + * @author Louis Charette */ class CacheAlertStream extends AlertStream { /** - * @var Cache Object We use the cache object so that added messages will automatically appear in the cache. + * @var Cache Object We use the cache object so that added messages will automatically appear in the cache. */ protected $cache; /** - * @var Repository Object We use the cache object so that added messages will automatically appear in the cache. + * @var Repository Object We use the cache object so that added messages will automatically appear in the cache. */ protected $config; /** * Create a new message stream. * - * @param string $messagesKey Store the messages under this key - * @param MessageTranslator|null $translator - * @param Cache $cache - * @param Repository $config + * @param string $messagesKey Store the messages under this key + * @param MessageTranslator|null $translator + * @param Cache $cache + * @param Repository $config */ - public function __construct($messagesKey, MessageTranslator $translator = null, Cache $cache, Repository $config) - { + public function __construct($messagesKey, MessageTranslator $translator = NULL, Cache $cache, Repository $config) { $this->cache = $cache; $this->config = $config; parent::__construct($messagesKey, $translator); @@ -50,12 +50,11 @@ class CacheAlertStream extends AlertStream /** * Get the messages from this message stream. * - * @return array An array of messages, each of which is itself an array containing 'type' and 'message' fields. + * @return array An array of messages, each of which is itself an array containing 'type' and 'message' fields. */ - public function messages() - { - if ($this->cache->tags('_s'.session_id())->has($this->messagesKey)) { - return $this->cache->tags('_s'.session_id())->get($this->messagesKey) ?: []; + public function messages() { + if ($this->cache->tags('_s' . session_id())->has($this->messagesKey)) { + return $this->cache->tags('_s' . session_id())->get($this->messagesKey) ?: []; } else { return []; } @@ -64,21 +63,19 @@ class CacheAlertStream extends AlertStream /** * Clear all messages from this message stream. * - * @return void + * @return void */ - public function resetMessageStream() - { - $this->cache->tags('_s'.session_id())->forget($this->messagesKey); + public function resetMessageStream() { + $this->cache->tags('_s' . session_id())->forget($this->messagesKey); } /** * Save messages to the stream * - * @param string $messages The message - * @return void + * @param string $messages The message + * @return void */ - protected function saveMessages($messages) - { - $this->cache->tags('_s'.session_id())->forever($this->messagesKey, $messages); + protected function saveMessages($messages) { + $this->cache->tags('_s' . session_id())->forever($this->messagesKey, $messages); } } diff --git a/main/app/sprinkles/core/src/Alert/SessionAlertStream.php b/main/app/sprinkles/core/src/Alert/SessionAlertStream.php index 8b4604b..fec0973 100644 --- a/main/app/sprinkles/core/src/Alert/SessionAlertStream.php +++ b/main/app/sprinkles/core/src/Alert/SessionAlertStream.php @@ -5,6 +5,7 @@ * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ + namespace UserFrosting\Sprinkle\Core\Alert; use UserFrosting\I18n\MessageTranslator; @@ -15,24 +16,23 @@ use UserFrosting\Session\Session; * Implements a message stream for use between HTTP requests, with i18n support via the MessageTranslator class * Using the session storage to store the alerts * - * @author Alex Weissman (https://alexanderweissman.com) + * @author Alex Weissman (https://alexanderweissman.com) */ class SessionAlertStream extends AlertStream { /** - * @var Session We use the session object so that added messages will automatically appear in the session. + * @var Session We use the session object so that added messages will automatically appear in the session. */ protected $session; /** * Create a new message stream. * - * @param string $messagesKey Store the messages under this key - * @param MessageTranslator|null $translator - * @param Session $session + * @param string $messagesKey Store the messages under this key + * @param MessageTranslator|null $translator + * @param Session $session */ - public function __construct($messagesKey, MessageTranslator $translator = null, Session $session) - { + public function __construct($messagesKey, MessageTranslator $translator = NULL, Session $session) { $this->session = $session; parent::__construct($messagesKey, $translator); } @@ -40,31 +40,28 @@ class SessionAlertStream extends AlertStream /** * Get the messages from this message stream. * - * @return array An array of messages, each of which is itself an array containing "type" and "message" fields. + * @return array An array of messages, each of which is itself an array containing "type" and "message" fields. */ - public function messages() - { + public function messages() { return $this->session[$this->messagesKey] ?: []; } /** * Clear all messages from this message stream. * - * @return void + * @return void */ - public function resetMessageStream() - { + public function resetMessageStream() { $this->session[$this->messagesKey] = []; } /** * Save messages to the stream * - * @param string $messages The message - * @return void + * @param string $messages The message + * @return void */ - protected function saveMessages($messages) - { + protected function saveMessages($messages) { $this->session[$this->messagesKey] = $messages; } } |