diff options
Diffstat (limited to 'main/app/sprinkles/account/src/Log')
-rwxr-xr-x | main/app/sprinkles/account/src/Log/UserActivityDatabaseHandler.php | 33 | ||||
-rwxr-xr-x | main/app/sprinkles/account/src/Log/UserActivityProcessor.php | 45 |
2 files changed, 78 insertions, 0 deletions
diff --git a/main/app/sprinkles/account/src/Log/UserActivityDatabaseHandler.php b/main/app/sprinkles/account/src/Log/UserActivityDatabaseHandler.php new file mode 100755 index 0000000..d7ceeef --- /dev/null +++ b/main/app/sprinkles/account/src/Log/UserActivityDatabaseHandler.php @@ -0,0 +1,33 @@ +<?php +/** + * UserFrosting (http://www.userfrosting.com) + * + * @link https://github.com/userfrosting/UserFrosting + * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) + */ +namespace UserFrosting\Sprinkle\Account\Log; + +use UserFrosting\Sprinkle\Core\Log\DatabaseHandler; + +/** + * Monolog handler for storing user activities to the database. + * + * @author Alex Weissman (https://alexanderweissman.com) + */ +class UserActivityDatabaseHandler extends DatabaseHandler +{ + /** + * {@inheritDoc} + */ + protected function write(array $record) + { + $log = $this->classMapper->createInstance($this->modelName, $record['extra']); + $log->save(); + + if (isset($record['extra']['user_id'])) { + $user = $this->classMapper->staticMethod('user', 'find', $record['extra']['user_id']); + $user->last_activity_id = $log->id; + $user->save(); + } + } +} diff --git a/main/app/sprinkles/account/src/Log/UserActivityProcessor.php b/main/app/sprinkles/account/src/Log/UserActivityProcessor.php new file mode 100755 index 0000000..2575270 --- /dev/null +++ b/main/app/sprinkles/account/src/Log/UserActivityProcessor.php @@ -0,0 +1,45 @@ +<?php +/** + * UserFrosting (http://www.userfrosting.com) + * + * @link https://github.com/userfrosting/UserFrosting + * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) + */ +namespace UserFrosting\Sprinkle\Account\Log; + +use Monolog\Logger; + +/** + * Monolog processor for constructing the user activity message. + * + * @author Alex Weissman (https://alexanderweissman.com) + */ +class UserActivityProcessor +{ + /** + * @var int + */ + protected $userId; + + /** + * @param int $userId The id of the user for whom we will be logging activities. + */ + public function __construct($userId) + { + $this->userId = $userId; + } + + public function __invoke(array $record) + { + $additionalFields = [ + 'ip_address' => $_SERVER['REMOTE_ADDR'], + 'user_id' => $this->userId, + 'occurred_at' => $record['datetime'], + 'description' => $record['message'] + ]; + + $record['extra'] = array_replace_recursive($record['extra'], $additionalFields, $record['context']); + + return $record; + } +} |