Skip to content

Commit

Permalink
feat: Updates PHP version, refactor models, add event emission and ot…
Browse files Browse the repository at this point in the history
…her adjustments.
  • Loading branch information
gustavofreze committed Feb 10, 2024
1 parent 81d3a60 commit 54e6185
Show file tree
Hide file tree
Showing 174 changed files with 2,533 additions and 1,981 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea

/vendor/
/report
composer.lock
.phpunit.result.cache
*.lock
.phpunit.*
30 changes: 25 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
FROM gustavofreze/php:8.2-fpm

RUN apk update \
&& apk add libressl-dev \
&& pecl install mongodb \
&& docker-php-ext-enable mongodb \
&& rm -rf /var/cache/apk/*
LABEL author="Gustavo Freze" \
maintainer="Gustavo Freze" \
org.label-schema.name="gustavofreze/cheap-delivery" \
org.label-schema.vcs-url="https://github.com/gustavofreze/cheap-delivery/blob/main/Dockerfile" \
org.label-schema.schema-version="1.0"

ARG FLYWAY_VERSION=10.6.0

RUN docker-php-ext-install pdo_mysql

RUN apk --no-cache add tar curl mysql-client openjdk21-jre \
&& mkdir -p /opt/flyway \
&& curl -L "https://repo1.maven.org/maven2/org/flywaydb/flyway-commandline/${FLYWAY_VERSION}/flyway-commandline-${FLYWAY_VERSION}-linux-x64.tar.gz" | tar -xz --strip-components=1 -C /opt/flyway \
&& rm -f /opt/flyway/jre/bin/java \
&& ln -s /usr/lib/jvm/java-11-openjdk/jre/bin/java /opt/flyway/jre/bin/java \
&& ln -s /opt/flyway/flyway /usr/local/bin/flyway \
&& apk del curl tar

WORKDIR /var/www/html

COPY ./db /db

RUN chmod +x /db/entrypoint.sh

ENTRYPOINT ["bash", "/db/entrypoint.sh"]
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021-2023 Gustavo Freze
Copyright (c) 2021-2024 Gustavo Freze

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
35 changes: 25 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
DOCKER_RUN = docker run --rm -it --net=host -v ${PWD}:/app -w /app gustavofreze/cheap-delivery
IMAGE = gustavofreze/php:8.2
DOCKER_RUN = docker run -u root --rm -it --net=host -v ${PWD}:/app -w /app ${IMAGE}
DOCKER_EXEC = docker exec -it cheap-delivery

.PHONY: configure run test test-no-coverage review show-reports clean
FLYWAY = docker run --rm -v ${PWD}/db/mysql/migrations:/flyway/sql --env-file=config/local.env --link cheap-delivery-adm --network=cheap-delivery_default -e FLYWAY_EDITION="community" flyway/flyway:10.6.0
MIGRATE_DB = ${FLYWAY} -locations=filesystem:/flyway/sql -schemas=cheap_delivery_adm

.PHONY: configure run test test-no-coverage review show-coverage clean migrate-database clean-database

configure:
@docker-compose up -d --build
@${DOCKER_EXEC} composer update --optimize-autoloader

run:
configure-local:
@${DOCKER_RUN} composer update --optimize-autoloader

test: run review
@${DOCKER_RUN} composer tests
test:
@${DOCKER_RUN} composer run test

test-no-coverage:
@${DOCKER_RUN} composer run test-no-coverage

test-unit:
@${DOCKER_RUN} composer run test-unit

test-no-coverage: run review
@${DOCKER_RUN} composer tests-no-coverage
test-integration:
@${DOCKER_RUN} composer run test-integration

review:
@${DOCKER_RUN} composer review

show-reports:
show-coverage:
@sensible-browser report/coverage/coverage-html/index.html

clean:
@sudo chown -R ${USER}:${USER} ${PWD}
@rm -rf report vendor
@rm -rf report vendor *.phpunit *.lock

migrate-database:
@${MIGRATE_DB} migrate

clean-database:
@${MIGRATE_DB} clean
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

A empresa XPTO realizou um sorteio entre jogadores do Brasil inteiro, porém, os brindes precisam chegar a seus
ganhadores de alguma maneira. Para tanto, é necessário implementar um sistema para calcular o menor custo para o envio
de cada brinde de acordo com a distância do ganhador, e o peso do brinde. Ao entrar em contato com as transportadoras,
de cada brinde conforme a distância do ganhador, e o peso do brinde. Ao entrar em contato com as transportadoras,
recebemos as seguintes condições para o transporte:

| Empresa | Valor fixo | Valor km/kg |
Expand Down Expand Up @@ -52,17 +52,17 @@ make configure

URLs de acesso:

| Ambiente | DNS |
|:---------|:-----------------------------------|
| `Local` | http://cheap-delivery.localhost:81 |
| Ambiente | DNS |
|:---------|:--------------------------------|
| `Local` | http://cheap-delivery.localhost |

<div id="tests"></div>

### Shipment

###### Realiza o cálculo do menor custo de envio disponível.

**POST** `{{dns}}/shipment`
**POST** `{{dns}}/shipments`

**Request**

Expand All @@ -75,14 +75,14 @@ URLs de acesso:

```json
{
"person": {
"name": "Gustavo",
"distance": 150.00
},
"product": {
"name": "Notebook",
"weight": 3.70
}
"person": {
"name": "Gustavo",
"distance": 150.00
},
"product": {
"name": "Notebook",
"weight": 3.70
}
}
```

Expand All @@ -94,7 +94,7 @@ HTTP/1.1 200 OK

```json
{
"carrier": "DHL",
"cost": 37.75
"carrier": "DHL",
"cost": 37.75
}
```
37 changes: 17 additions & 20 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,38 @@
},
"autoload-dev": {
"psr-4": {
"CheapDelivery\\": "tests/"
"CheapDelivery\\": [
"tests/Unit/",
"tests/Integration/"
]
}
},
"require": {
"php": "^8.1||^8.2",
"php": "^8.2",
"slim/psr7": "^1.6",
"slim/slim": "4.11.0",
"respect/validation": "^2.2",
"php-di/slim-bridge": "^3.3",
"tiny-blocks/http": "^2.1",
"tiny-blocks/serializer": "^2.0",
"mongodb/mongodb": "1.*",
"ext-mongodb": "*",
"ext-pdo": "*"
"doctrine/dbal": "^3.8",
"tiny-blocks/http": "^3.0",
"php-di/slim-bridge": "^3.4",
"ext-pdo": "*",
"ext-bcmath": "*"
},
"require-dev": {
"phpmd/phpmd": "^2.13",
"phpunit/phpunit": "^9.6",
"squizlabs/php_codesniffer": "^3.7",
"phpmd/phpmd": "^2.15",
"phpunit/phpunit": "^10",
"squizlabs/php_codesniffer": "^3.8",
"dg/bypass-finals": "^1.4"
},
"scripts": {
"phpcs": "phpcs --standard=PSR12 --extensions=php ./src",
"phpmd": "phpmd ./src text phpmd.xml --suffixes php --exclude --ignore-violations-on-exit",
"test": "phpunit --log-junit=report/coverage/junit.xml --coverage-xml=report/coverage/coverage-xml --coverage-html=report/coverage/coverage-html tests",
"test-no-coverage": "phpunit --no-coverage",
"test": "phpunit -c phpunit.xml --testsuite all",
"test-unit": "phpunit -c phpunit.xml --no-coverage --testsuite unit",
"test-no-coverage": "phpunit -c phpunit.xml --no-coverage --testsuite all",
"test-integration": "phpunit -c phpunit.xml --no-coverage --testsuite integration",
"review": [
"@phpcs",
"@phpmd"
],
"tests": [
"@test"
],
"tests-no-coverage": [
"@test-no-coverage"
]
}
}
10 changes: 0 additions & 10 deletions config/configs.env

This file was deleted.

19 changes: 19 additions & 0 deletions config/local.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Api information
API_NAME='Cheap Delivery'

# Database
MYSQL_DATABASE_HOST=cheap-delivery-adm
MYSQL_DATABASE_PORT=3306
MYSQL_DATABASE_NAME=cheap_delivery_adm
MYSQL_DATABASE_USER=root
MYSQL_DATABASE_PASSWORD=root

# Flyway
FLYWAY_URL=jdbc:mysql://cheap-delivery-adm:3306/cheap_delivery_adm?useUnicode=yes&characterEncoding=UTF-8
FLYWAY_USER=root
FLYWAY_TABLE=schema_history
FLYWAY_SCHEMAS=cheap_delivery_adm
FLYWAY_EDITION=community
FLYWAY_PASSWORD=root
FLYWAY_LOCATIONS=filesystem:/var/www/html/db/mysql/migrations
FLYWAY_CLEAN_DISABLED=false
4 changes: 3 additions & 1 deletion config/nginx/nginx.conf → config/nginx/site.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
server {
listen 80;

root /var/www/html/public;
index index.php index.html;

Expand All @@ -19,4 +21,4 @@ server {
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
}
}
}
28 changes: 28 additions & 0 deletions db/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

wait_for_db() {
attempt=0
max_attempts=60
db_ready=false

while [ $attempt -lt $max_attempts ]; do
if mysqladmin ping -h"$MYSQL_DATABASE_HOST" -u"$MYSQL_DATABASE_USER" -p"$MYSQL_DATABASE_PASSWORD" -P"$MYSQL_DATABASE_PORT" --silent; then
db_ready=true
break
fi

sleep 1
((attempt++))
done

if ! $db_ready; then
echo "Database is not ready after $max_attempts attempts."
exit 1
fi
}

wait_for_db

flyway migrate

php-fpm -F
24 changes: 0 additions & 24 deletions db/mongodb/mongo-init.js

This file was deleted.

3 changes: 3 additions & 0 deletions db/mysql/config/credentials.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[client]
user = "root"
password = "root"
12 changes: 12 additions & 0 deletions db/mysql/init-db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

DATABASE_NAME="cheap_delivery_adm"

CREDENTIALS="/scripts/config/credentials.cnf"
CREATE_DATABASE="CREATE DATABASE IF NOT EXISTS ${DATABASE_NAME};"

printf "\n\nRunning DDL commands for the database %s ... \n\n" "${DATABASE_NAME}"

mysql --defaults-extra-file="${CREDENTIALS}" -e "${CREATE_DATABASE}"

printf "\n\nEnd of execution. \n"
13 changes: 13 additions & 0 deletions db/mysql/migrations/V0000__Create_carrier_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE TABLE carrier
(
id BINARY(16) NOT NULL COMMENT 'Unique identifier for the carrier.',
name VARCHAR(255) NOT NULL COMMENT 'Name of the carrier.',
cost_modality JSON NOT NULL COMMENT 'JSON representation of types and costs of transportation offered by the carrier.',
created_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) COMMENT 'Date and time when the carrier record was inserted.',
PRIMARY KEY (id),
INDEX carrier_idx01 (name),
CONSTRAINT carrier_uk01
UNIQUE (name)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci COMMENT ='Table used to persist carrier records.';
10 changes: 10 additions & 0 deletions db/mysql/migrations/V0001__Create_shipment_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE shipment
(
id BINARY(16) NOT NULL COMMENT 'Unique identifier for the shipment.',
cost DECIMAL(15, 2) NOT NULL COMMENT 'Cost of the shipment.',
carrier_name VARCHAR(255) NOT NULL COMMENT 'Name of the carrier providing the shipment service.',
created_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) COMMENT 'Date and time when the shipment record was inserted.',
PRIMARY KEY (id)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci COMMENT ='Table used to persist shipment records.';
16 changes: 16 additions & 0 deletions db/mysql/migrations/V0002__Create_outbox_event_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CREATE TABLE outbox_event
(
id BINARY(16) PRIMARY KEY NOT NULL COMMENT 'UUID that identifies the event in canonical format (separated by hyphen in the form 8-4-4-4-12).',
aggregate_type VARCHAR(255) NOT NULL COMMENT 'Name of the aggregation root that produced the event in CamelCase format.',
aggregate_id VARCHAR(36) NOT NULL COMMENT 'Textual representation of the aggregation root identifier.',
event_type VARCHAR(255) NOT NULL COMMENT 'Name of the event in CamelCase format.',
revision INT NOT NULL COMMENT 'Positive number indicating the version of the payload produced from the event.',
payload JSON NOT NULL COMMENT 'Event payload as a JSON object.',
occurred_on DATETIME(6) NOT NULL COMMENT 'Moment when the event occurred.',
created_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) COMMENT 'Date when the record was inserted.',
INDEX outbox_event_idx01 (aggregate_id),
INDEX outbox_event_idx02 (occurred_on),
INDEX outbox_event_idx03 (created_at)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci COMMENT ='Table used to persist events atomically for eventual publication on the message broker.';
1 change: 1 addition & 0 deletions db/mysql/migrations/V0003__Insert_carriers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO carrier(id, name, cost_modality) VALUES(UUID_TO_BIN(UUID()), 'DHL', '{"modality":"Composite","modalityOne":{"cost":10,"modality":"Fixed"},"modalityTwo":{"cost":0.05,"modality":"Linear"}}'), (UUID_TO_BIN(UUID()), 'FedEx', '{"modality":"Composite","modalityOne":{"cost":4.30,"modality":"Fixed"},"modalityTwo":{"cost":0.12,"modality":"Linear"}}'), (UUID_TO_BIN(UUID()), 'Loggi', '{"modality":"Composite","modalityOne":{"modality":"Partial","costModality":{"modality":"Composite","modalityOne":{"cost":2.10,"modality":"Fixed"},"modalityTwo":{"cost":0.12,"modality":"Linear"}},"costCondition":{"name":"WeightSmallerThan","weight":5.00}},"modalityTwo":{"modality":"Partial","costModality":{"modality":"Composite","modalityOne":{"cost":10.00,"modality":"Fixed"},"modalityTwo":{"cost":0.01,"modality":"Linear"}},"costCondition":{"name":"WeightGreaterThanOrEqual","weight":5.00}}}');
Loading

0 comments on commit 54e6185

Please sign in to comment.