# ows-metadata

## Local Setup

* Install [docker](https://docs.docker.com/get-docker/)
* Clone this repository

### Set Credentials

1. Create `.env` file from template
```
$ cp .env.shadow .env
```

#### Packages from GitHub

To install some private packages, you need to be able to autheticate with Github.

* Create Github access token [here](https://github.com/settings/tokens) with all **repo** scope only.
* Set auth token in `.env` replacing `<key-goes-here>` in `COMPOSER_AUTH`

#### Databases

Set your personal database credentials for **QA** in `.env` where empty.

#### AWS

* Install and configure [awsume](https://www.notion.so/AWS-Access-f841b9dd815d4443a80e96a86c92cd2f#ad5d3a529a7d41b299daa604c517ebf2) to easily set and change AWS credentials on your current shell which will be used by the application container automatically.
* Install [ecr-credentials-helper](https://www.notion.so/AWS-Access-f841b9dd815d4443a80e96a86c92cd2f#ad5d3a529a7d41b299daa604c517ebf2) to automatically use your current AWS credentials when pulling down ECR images.

### Running Application

```bash
$ docker compose up --build web-deploy
```
:bulb: Current AWS credentials in your host machine's shell will be used to fetch from ECR and by the application.


#### URL Reference
| desc | location |
|----|----|
| healthcheck | http://0.0.0.0:80/healthchk.php |
| apple | http://0.0.0.0:80/oa/metadata/generate/dms_id/1/subformat/music/returnErr/1/xml_only/0/delivery_type/complete_album/upc/196925242622 |
| spotify | http://0.0.0.0:80/oa/metadata/generate/dms_id/286/storename/ddexern/version/3.8.0/returnErr/1/xml_only/0/delivery_type/complete_album/upc/196925242622 |

:question: Why is there an HAProxy container?

By design, Zend Framework bootstraps and loads the PHP application for every HTTP request. This means that database connections need to be re-established (2x 5s each). HAProxy keeps tcp connections open to the databases so that the application only needs to re-establish a connection with HAProxy which is running locally. This speeds up each request by about 60-70%.

### Testing and Linting

Run lint, static analysis, and tests (parallel-lint, PHP-CS-Fixer, PHPStan, PHPCS, PHPUnit):

```bash
$ docker compose run --rm --build unit-lint
```

Filter to a single test or class with the `TEST_FILTER` env var:

```bash
$ TEST_FILTER=testGenerateProductsAlbum docker compose run --rm --build unit-lint
```

#### Snapshot tests

Many tests use `assertMatchesXmlSnapshot` from `spatie/phpunit-snapshot-assertions`. Snapshot files live alongside their tests in `__snapshots__/` directories and are committed to the repo.

The `unit-lint` service runs in strict mode (`CREATE_SNAPSHOTS=false`): tests fail when a referenced snapshot file is missing, rather than silently regenerating it inside the throwaway container.

To create a new snapshot or refresh an existing one, use the `update-snapshots` service. It bind-mounts `tests/` so generated files persist on the host:

```bash
$ docker compose run --rm --build update-snapshots
```

`TEST_FILTER` works here too — useful when adding a single new snapshot test:

```bash
$ TEST_FILTER=testNewSnapshotCase docker compose run --rm --build update-snapshots
```

Commit the generated `*.xml` snapshot file alongside your test code.

## Refresh Apple Music Schema Validation File

1. Checkout and go to the [direct_delivery](https://github.com/theorchard/direct_delivery) (a.k.a VECTOR workers) project
2. Start up a worker in CLI mode
```bash
docker compose run --build --rm base
```
3. Lookup credentials from [Apple DMS Page in OA](https://oa.theorchard.com/warehouse/view_customer_master.php?customer_master_master_id=1) or [AWS Secrets Manager](https://us-east-1.console.aws.amazon.com/secretsmanager/secret?name=prod%2Fdirect_delivery%2Fconnection_info%2F1%2Frelease&region=us-east-1)
4. Run the following command **in the container**
```bash
/usr/local/itms/bin/iTMSTransporter -m generateSchema -u <USER> -p <PASSWORD> -schema music5.3 -schemaType strict -itc_provider Orchard -destination /tmp/schema
```
5. Move file on **host** machine from `./tmp/schema/music5.3-strict.rng` to `<ows-metadata-project>/src/Store/Itunes/Music/Version050300/Schema/music5.3-strict.rng`

