Skip to content

mascam97/laravel-api-quotes

Repository files navigation

Laravel API Quotes Stable Status Technical Debt Coverage

Main portfolio as PHP Backend Developer - Community to rate quotes

Project goal

2021: Personal project to apply knowledge about API REST and learn more about Laravel.

2022: Project to apply some experience as define business logic, apply best practices, integrate packages and try out tools.

2023: Main portfolio to apply some advanced functionalities and practices.

Achievements 2021 🌟

  • Learned better practices about APIs (versioning, url names and Authentication by API tokens).
  • Implemented Authentication functions (register and login) and Authorization - Policies.
  • Implemented API resources to transform data.
  • Implemented a feature where users can rate quotes (Polymorphic relationships).
  • Tested with PHPUnit (Test-Driven Development) and Postman and created a documentation link.
  • Implemented custom errors and logs about information of the policy, authentication and an Observer in Quotes.
  • Create a custom Artisan command to send an email and tested it in local (mailhog).
  • Implemented Task Scheduling to refresh the database each month and sending an email to users weekly.
  • Implemented a Listener and an Event to sending an email to the user when one of his quotes were rated.
  • Implemented Queue and Jobs with the container Redis.
  • Implemented support for spanish language (messages and emails).

Achievements 2022 🌟

  • Created a Project definition file to define the main business logic
  • Implemented Localization and Middleware to define language user
  • Implemented Continuous Integration with GitHub Actions
  • Implemented PHP CS Fixer to fix code standard
  • Implemented PHPStan for a static analysis in the code
  • Implemented rector for handle automated refactorings
  • Implemented PHP Insights to check code quality
  • Implemented State pattern and Enums
  • Upgraded to PHP 8.1 and Laravel 9
  • Refactoring all the code (setUps implementation in test, use of Actions, DTOs, type hinting, etc.)
  • Implemented a QueryBuilder library and improve the API logic (delete unnecessary API versioning)
  • Implemented Sonar Cloud to reduce Technical Debt (duplications, smells code, etc.)
  • Implemented some recommendations to build large-than-average web applications
  • Reached the 8 level in PHPStan
  • Redefined business logic in ratings for a better scalability
  • Improved test suit with advanced practices (test command, jobs, middleware, etc.)

Achievements 2023 🌟

  • Upgraded to PHP 8.2 and Laravel 10
  • Upgraded to PHPUnit 10
  • Implemented an Administrator domain
  • Implemented Roles and Permissions for administrator domain
  • Implemented User administration for administrator domain
  • Implemented Activities in user deletion for administrator domain
  • Optimized Database Queries with best practices and include queries in tests
  • Increased Coverage to 95%
  • Implemented an External API to get data
  • Implemented OAuth for authentication
  • Implemented Soft Delete in Users
  • Implemented MySQL and Redis in pipeline
  • Improved API design
  • Implemented GraphQL and use queries to get data

Getting Started πŸš€

These instructions will get you a copy of the project up and running on your local machine.

Prerequisites πŸ“‹

The programs you need are:

Installing πŸ”§

First duplicate the file .env.example as .env.

cp .env.example .env

Note: You could change some values, anyway docker-compose create the database according to the defined values.

Then install the PHP dependencies:

 docker run --rm --interactive --tty \
 --volume $PWD:/app \
 composer install

Then create the next alias to run commands in the container with Laravel Sail.

alias sail='bash vendor/bin/sail'

Note: Setting this alias as permanent is recommended.

Create the images and run the services (laravel app, mysql, redis and mailhog):

sail up

With Laravel Sail you can run commands as docker-compose (docker-compose up -d = sail up -d) and php(e.g php artisan migrate = sail artisan migrate). To run Composer, Artisan, and Node / NPM commands just add sail at the beginning (e.g sail npm install). More information here.

Then generate the application key.

sail artisan key:generate

Generate the database with fake data:

sail artisan migrate --seed

Finally, create the encryption keys needed to generate secure access tokens (personal access and password grant).

php artisan passport:install

Note: There are some clients generated by the seeder. To create a new one, use

`php artisan passport:client --password --name="Name" --provider="users"`

Testing

Static Analysis

In order to find errors in the code without running it. It is better before running the tests.

sail composer phpstan

Backend testing

There are some unit testing in Models and Traits and some feature testings in controllers, all these tests guarantee functionalities like table relationship, validations, authentication, authorization, actions as create, read, update and delete, etc.

sail artisan test

Fix Code Standard

After we are sure our code passes the analysis and tests, before we commit it, we ensure it has the standard of code style. This should look like just one programmer has written it.

sail composer php-cs-fixer

This command fits the code automatically.


Automated Refactoring

There are some rules to refactoring the code, as code quality, dead code and standard PHP 8.

sail composer rector

This is not a required step, it is an extra improving by rector.php.


Analyze the code quality

There are some rules in config/insights.php to check the code quality.

sail artisan insights

Advanced features

Running Artisan Commands

There is a custom command to send an email as example to the users.

sail artisan send:newsletter

In docker-compose there is a container about MailHog, this container shows the email sent in your local in the port 8025 by default.

Running Tasks Scheduled

There are two task, one to refresh the database (monthly) and other to email users (weekly) about how many users and quotes there are. Run it with:

sail artisan schedule:run

You could use schedule:list to look more information and its next schedule.

Running Queues

There is a Job (send a welcome email) created when a new user is registered and there is an event to send an email when a quote is rated, both are stored in queue, to run them run:

sail artisan queue:listen

Note: Remember in production the better command is queue:work, explanation.


Deployment πŸ“¦

API Documentation πŸ“„

Run the next command to generate the API documentation in the folder public/docs.

sail artisan scribe:generate

Built With πŸ› οΈ

Authors

Contributing

You're free to contribute to this project by submitting issues and/or pull requests.

License

This project is licensed under the MIT License.

References πŸ“š