diegoalvarezb / laravel-service-utils by diegoalvarezb

Utils for internal Laravel services.
8
1
2
Package Data
Maintainer Username: diegoalvarezb
Package Create Date: 2017-08-30
Package Last Update: 2017-09-03
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-03-27 03:11:50
Package Statistics
Total Downloads: 8
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 1
Total Watchers: 2
Total Forks: 0
Total Open Issues: 0

laravel-service-utils

This tool adds some utilities for internal services in Laravel.

Using this package, you could extend all your services (classes with the application business logic) from Diegoalvarezb\ServiceUtils\AbstractService and use some funcionalities:

  • Service response interface
  • Log management

Requirements

  • PHP >= 5.6
  • Laravel >= 5.0

Installation and configuration

Package installation with composer:

composer require diegoalvarezb/laravel-service-utils

And add the service provider in your config/app.php file:

Diegoalvarezb\FrontMessages\ServiceUtilsServiceProvider::class

And this command will add the service-utils config file to the laravel config folder:

php artisan vendor:publish --tag=service-utils

Service response interface

Use the next command to return the result of a method:

return $this->generateResponse($data = [], $errorCode = 'NO_ERROR', $message = '');

The first param contains an array with all data. The second one must be the error code (this one must exists in the service-utils config file). The third one an additional message (if you don't send this param, the corresponding in the config file will be selected).

This method will return a ServiceResponse object, wich has the next methods:

  • hasErrors()
  • isCritical()
  • getMessage()
  • getData()
  • getHttpCode()

Service log management

Use the next command to write info the log file:

$this->logException($exception, $type = 'error', $customMessage = '');

The first param contains the exception. The second one must be the log type. The third one an additional message.

The list of log types:

  • error
  • emergency
  • alert
  • critical
  • warning
  • notice
  • info

The structure of the log: [datetime] local.LOG_TYPE: Path\To\Class | method() | (Exception) | message

Example config file: service-utils.php

<?php

use Symfony\Component\HttpFoundation\Response;

return [

    /*
     * Error code list for service response interface.
     */
    'service_codes' => [

        'NOT_ERROR' => [
            'is_error' => false,
            'message' => 'Ok.',
            'http_code' => Response::HTTP_OK,
            'is_critial' => false,
        ],

        'GENERAL_ERROR' => [
            'is_error' => true,
            'message' => 'General error.',
            'http_code' => Response::HTTP_INTERNAL_SERVER_ERROR,
            'is_critial' => false,
        ],

        'NEW_ERROR' => [
            'is_error' => true,
            'message' => 'Text example of a new error.',
            'http_code' => Response::HTTP_INTERNAL_SERVER_ERROR,
            'is_critial' => true,
        ],

    ],

];

License

MIT