mumsnet/mn-toolkit

This package contains various functionalities used across all our projects. Those functionalities have been replicated from legacy ruby gems.


README

This repo houses the source for our composer package mn-toolkit. This package contains the following features:

  • Feature toggles - Used to toggle features on and off
    You can use the function 'isOn' to check the state of a feature toggle You can use the function 'getToggles' to check the available toggles The are also functions for checking white listed and black listed uris.
    Those functions just require the toggle and the request uri

  • Local file cache - return the cached value associated with $key if available, or load it using $loadFunction and add it to the cache.

  • Ganesha File Store Adapter - PHP implementation of Circuit Breaker pattern
    More info at: https://github.com/ackintosh/ganesha

  • GlobalLogger - Logger class for this package
    Global logger is used to set an instance of a global logger and then retrieve it when needed so we can log errors to it. This sends the errors to your logs on your local and to papertrail on stage and production. We mainly use it for system errors and you can use it like so: log::error('Token does not exist');

  • Globals frontend - Loads and caches global assets

  • JWT - Used to add and check JWT tokens for securing cross microservice requests
    To set a JWT token, generate it on the JWT website using the ENV values. There is a JWT quide in the docs. When the token has been generated, you add it to the Authorization header of all the requests that you want to secure. To check if a request has a token you use:
    $authorization = $request->header('Authorization');
    $withoutBearer = str_replace('bearer ', '', $authorization);
    if($authorization && $withoutBearer != ''){
    $isValid = JWT::getInstance()->isValidToken($withoutBearer);
    if($isValid){
    return $next($request);
    }
    }

  • Transactional emails - Sends transactional Emails
    To use this function you need to pass it an instance of your logger so that the package can log thngs in case they go wrong during sending. You use it like so:
    $logger = Log::getLogger();
    $mn_transctional_email = new SendTransactionalEmail($logger);
    $mn_transctional_email->sendTransactionalEmail($template, $email, 'Mumnset notification', 'Hello panos', '127.0.0.1', ['body' => $body]);
    The sendTransactional Mail function requires the following parameters:
    $message_type,
    $to_address,
    $subject,
    $fallback_text,
    $request_id,
    $template_fields = [],
    $cc_addresses = ''

  • Source IP detection - Might not need this one

  • SiteAction (Graylog) - Logger for behavior logging
    This logger is used less for errors and more for user behavior logging like: user tried to update his details without being logged in.
    In these logs you can pass quite a lot of info, like so:
    $this->siteaction = SiteAction::getInstance();
    $this->siteaction->log($user_id, 'Failed to created pregnancy record', 'profile', 'profile_update', ['errors' => json_encode($e->getMessage())]);

  • Correlation ID (set origin request id)
    Sets the origin request id header if it doesnt exist for correlation and for tracking the request

  • User sessions (SSO) for lambda - sets user session for log in through lambda Its used the same way as the user sessions for laravel below:

  • User sessions (SSO) for laravel - sets user session for log in through laravel
    You can set a session like so:
    $session = new UserSessionsLaravel();
    $session->setUserSession(['user_id' => $userEmailCheck->id], true);
    You can get a user id from the session like so:
    $session = new UserSessionsLaravel($_COOKIE);
    $userId = $session->getUserIdFromSession();

  • Slack Messenger - Accepts a channel and a message and sends the message to the channel
    This is quite simple to use. Just call the function and give it a channel name and a message. Make sure you have the channels credentials in your ENV. SlackMessenger::sendMessage('channel','message')

Install

Via Composer

$ composer require mumsnet/mn-toolkit