alex-foleon/service-library-catalog-php

v0.1.0 2020-12-24 15:51 UTC

This package is auto-updated.

Last update: 2024-04-25 21:10:35 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score

service-library-catalog-php

Assignment

Build a tiny REST API and use this to build 2 resources. Books and Authors.

GET: /book/:id Expect a book with the author

GET: /author/:id Expect an author with all his books

POST: /author/ Create an author

POST: /book Create a book and add an author to it.

Ideas, developing

  • Repositories: Implements storing in PDO (Mysql) and caching in Redis, configurable with envs (see: docker-compose.override.yml). Tests use Sqlite and redis-mock. The idea is App caches Book with/without its Author and Author with/without his/her books. When client adds new book, Author's cache invalidates.
  • Catalogue: the Service that manages all these Repositories.
  • Authentication: App has Auth interface and implementation of supporting Bearer Auth. Other implementations can be added also.
  • Authorization: Interface and simple implementation of ACL: guests can read, users can add Books (but not Authors), admin can add Books and Authors.
  • Api endpoints: Specs for HTTP endpoints.
  • Transformers and Hydrators: App has its own "Serializer" which helps encode/decode entities for storing in some repository (document-oriented). Basic transformers for representing data in HTTP-response.
  • Controllers: V1 implements basic json REST, we can add more versions to support other protocols.

Used Libraries

  • php-di/php-di: DI container implementation;
  • predis/predis: Redis client;
  • rakit/validation: Library for validation of incoming requests;
  • nikic/fast-route: PHP routing (the foundation of thephpleague/route);
  • nyholm/psr7 and laminas/laminas-httphandlerrunner: PSR7 implementation (HTTP requests and responses);
  • laminas/laminas-permissions-acl: Classical ZF ACL implementation.

Installation

To let dockerization's network work on the local machine plz add to your /etc/hosts:

    127.0.0.1 host.docker.internal

And then:

    ### Download
    composer create-project alex-foleon/service-library-catalog-php:^0.1 catalogue --prefer-dist --ignore-platform-reqs
    cd catalogue
    ### Prepare default dockerization
    cp docker-compose.override.yml.dist docker-compose.override.yml
    docker-compose up -d
    
    ### Create database
    docker exec -it rds-library bash -c "mysql -uroot -pmy-secret-pw -e \"create database catalogue CHARACTER SET utf8 COLLATE utf8_general_ci\""
    ### Start migrations
    docker exec -it service-catalogue-php bash -c "./scripts/migrate.sh"
    ### Check system works
    curl -i http://127.0.0.1:8080/api/v1/healthcheck

After that App should work via http://127.0.0.1:8080

Testing

    docker exec -it service-catalogue-php bash -c "composer test"

Test coverage is more than 90%. More tests should be implemented, but it takes time. Code quality is ~9.8.

Authentication

App has simple Auth-service. Client should provide base64 encoded token to "Authorization" HTTP-header. Token should have the next json-structure:

{
  "secret": "some-secret",
  "payload": "roleId:clientId"
}

For example:

{"secret": "some-secret", "payload": "user:184"}

There are 3 roles in the App: guest, user and admin. No matter what ID is used as App doesn't has users repository.

You can use next encoded tokens which work for default env:

  • Guest: eyJzZWNyZXQiOiJzdXBlcl9kdXBlcl9zZWN1cml0eSIsInBheWxvYWQiOiJndWVzdDozIn0=
  • User: eyJzZWNyZXQiOiJzdXBlcl9kdXBlcl9zZWN1cml0eSIsInBheWxvYWQiOiJ1c2VyOjQifQ==
  • Admin: eyJzZWNyZXQiOiJzdXBlcl9kdXBlcl9zZWN1cml0eSIsInBheWxvYWQiOiJhZG1pbjo1In0=
  • Alien: eyJzZWNyZXQiOiJzdXBlcl9kdXBlcl9zZWN1cml0eSIsInBheWxvYWQiOiJhbGllbjo2In0= (should be denied)

Using admin example:

    curl -i -H "Authorization: Bearer eyJzZWNyZXQiOiJzdXBlcl9kdXBlcl9zZWN1cml0eSIsInBheWxvYWQiOiJhZG1pbjo1In0=" http://127.0.0.1:8080/api/v1/author/1

Endpoints

  1. Create the Author.

POST /author

{
    "name": "Charles Dickens",
    "birthdate": "1812-02-07",
    "deathdate": "1870-06-09",
    "biography": "Born in Portsmouth, Dickens left school to work in a factory when his father was incarcerated in a debtors' prison. Despite his lack of formal education, he edited a weekly journal for 20 years, wrote 15 novels, five novellas, hundreds of short stories and non-fiction articles, lectured and performed readings extensively, was an indefatigable letter writer, and campaigned vigorously for children's rights, education, and other social reforms. ",
    "summary": "Charles John Huffam Dickens FRSA (/ˈdɪkɪnz/; 7 February 1812 – 9 June 1870) was an English writer and social critic. He created some of the world's best-known fictional characters and is regarded by many as the greatest novelist of the Victorian era.[1] His works enjoyed unprecedented popularity during his lifetime, and by the 20th century, critics and scholars had recognised him as a literary genius. His novels and short stories are still widely read today."
}

Response: [200]

{
    "id": "1",
    "name": "Charles Dickens",
    "birthdate": "1812-02-07",
    "deathdate": "1870-06-09",
    "biography": "Born in Portsmouth, Dickens left school to work in a factory when his father was incarcerated in a debtors' prison. Despite his lack of formal education, he edited a weekly journal for 20 years, wrote 15 novels, five novellas, hundreds of short stories and non-fiction articles, lectured and performed readings extensively, was an indefatigable letter writer, and campaigned vigorously for children's rights, education, and other social reforms. ",
    "summary": "Charles John Huffam Dickens FRSA (/ˈdɪkɪnz/; 7 February 1812 – 9 June 1870) was an English writer and social critic. He created some of the world's best-known fictional characters and is regarded by many as the greatest novelist of the Victorian era.[1] His works enjoyed unprecedented popularity during his lifetime, and by the 20th century, critics and scholars had recognised him as a literary genius. His novels and short stories are still widely read today."
}
  1. Create the book.

POST /book

{
    "title": "Dombey and Son",
    "summary": "Dombey and Son is a novel by English author Charles Dickens. It follows the fortunes of a shipping firm owner, who is frustrated at the lack of a son to follow him in his footsteps; he initially rejects his daughter’s love before eventually becoming reconciled with her before his death. ",
    "authorId": 1
}

Response: [200]

{
    "id": "1",
    "title": "Dombey and Son",
    "summary": "Dombey and Son is a novel by English author Charles Dickens. It follows the fortunes of a shipping firm owner, who is frustrated at the lack of a son to follow him in his footsteps; he initially rejects his daughter’s love before eventually becoming reconciled with her before his death. "
}
  1. Read the author.

GET /author/ID

Response: [200]

{
    "id": "1",
    "name": "Charles Dickens",
    "birthdate": "1812-02-07",
    "deathdate": "1870-06-09",
    "biography": "Born in Portsmouth, Dickens left school to work in a factory when his father was incarcerated in a debtors' prison. Despite his lack of formal education, he edited a weekly journal for 20 years, wrote 15 novels, five novellas, hundreds of short stories and non-fiction articles, lectured and performed readings extensively, was an indefatigable letter writer, and campaigned vigorously for children's rights, education, and other social reforms. ",
    "summary": "Charles John Huffam Dickens FRSA (/ˈdɪkɪnz/; 7 February 1812 – 9 June 1870) was an English writer and social critic. He created some of the world's best-known fictional characters and is regarded by many as the greatest novelist of the Victorian era.[1] His works enjoyed unprecedented popularity during his lifetime, and by the 20th century, critics and scholars had recognised him as a literary genius. His novels and short stories are still widely read today.",
    "books": [
        {
            "id": "1",
            "title": "Dombey and Son",
            "summary": "Dombey and Son is a novel by English author Charles Dickens. It follows the fortunes of a shipping firm owner, who is frustrated at the lack of a son to follow him in his footsteps; he initially rejects his daughter’s love before eventually becoming reconciled with her before his death. "
        }
    ]
}
  1. Read the book.

GET /book/ID

Response: [200]

{
    "id": "1",
    "title": "Dombey and Son",
    "summary": "Dombey and Son is a novel by English author Charles Dickens. It follows the fortunes of a shipping firm owner, who is frustrated at the lack of a son to follow him in his footsteps; he initially rejects his daughter’s love before eventually becoming reconciled with her before his death. ",
    "author": {
        "id": "1",
        "name": "Charles Dickens",
        "birthdate": "1812-02-07",
        "deathdate": "1870-06-09",
        "biography": "Born in Portsmouth, Dickens left school to work in a factory when his father was incarcerated in a debtors' prison. Despite his lack of formal education, he edited a weekly journal for 20 years, wrote 15 novels, five novellas, hundreds of short stories and non-fiction articles, lectured and performed readings extensively, was an indefatigable letter writer, and campaigned vigorously for children's rights, education, and other social reforms. ",
        "summary": "Charles John Huffam Dickens FRSA (/ˈdɪkɪnz/; 7 February 1812 – 9 June 1870) was an English writer and social critic. He created some of the world's best-known fictional characters and is regarded by many as the greatest novelist of the Victorian era.[1] His works enjoyed unprecedented popularity during his lifetime, and by the 20th century, critics and scholars had recognised him as a literary genius. His novels and short stories are still widely read today."
    }
}