stages:
  - test
  - build
  - bundle
variables:
  APP_ENV: dev

PHPstan:
  stage: test
  image: composer:2.7.9
  before_script:
    - composer install --ignore-platform-reqs
  script:
    - vendor/bin/phpstan analyse --no-progress --error-format gitlab --memory-limit 2G > phpstan.json
  artifacts:
    when: always
    reports:
      codequality: phpstan.json

PHPCS:
  stage: test
  image: composer:2
  before_script:
    - apk add php-cli composer php-gd php-ctype php-xml php-simplexml php-xmlwriter php-tokenizer php-session php-dom
    - composer install
  script:
    - vendor/bin/phpcs --report=full --report-\\Micheh\\PhpCodeSniffer\\Report\\Gitlab=phpcs-quality-report.json
  artifacts:
    reports:
      codequality: phpcs-quality-report.json

#Database:
#  stage: test
#  image: composer:2
#  variables:
#    MYSQL_ROOT_PASSWORD: root
#  services:
#    - name: mysql:latest
#      alias: mysql
#  before_script:
#    - apk add php-cli composer php-gd php-ctype php-xml php-simplexml php-xmlwriter php-tokenizer php-session php-dom php-mysqli php-pdo_mysql
#    - composer install
#    - echo "DATABASE_URL=mysql://root:root@mysql:3306/symfony" > .env.local
#  script:
#    - /usr/bin/php?? bin/console doctrine:database:create --if-not-exists
#    - /usr/bin/php?? bin/console doctrine:migrations:migrate --no-interaction


#TODO: Write Unit tests and enable
#Unit tests:
#  stage: test
#  image: composer:2.5.7
#  variables:
#    XDEBUG_MODE: coverage
#  before_script:
#    - apk add php-cli composer php-gd php-ctype php-xml php-simplexml php-xmlwriter php-tokenizer php-session php-dom php-mysqli php-pdo_mysql
#    - apk add php$(ls /usr/bin/php?? | grep -oE [0-9]*)-pecl-xdebug
#    - export PHPINI=$(/usr/bin/php?? -i | grep -oE "/etc/php.*/php.ini")
#    - echo "zend_extension=xdebug.so" >> $PHPINI
#    - composer install
#  script:
#    - /usr/bin/php?? bin/phpunit --coverage-clover=coverage.xml
#  artifacts:
#    reports:
#      codequality: coverage.xml


Webpack:
  stage: build
  image: node:lts-alpine
  script:
    - yarn install
    - yarn build
  artifacts:
    paths:
      - public/build/

Composer:
  stage: build
  image: composer:2.7.9
  script:
    - apk add php-cli composer php-gd php-ctype php-xml php-simplexml php-xmlwriter php-tokenizer php-session php-dom php-mysqli php-pdo_mysql
    - composer install --optimize-autoloader
    - composer dump-autoload --classmap-authoritative
  artifacts:
    paths:
      - vendor/

Docker:
  # Use the official docker image.
  image: gitlab.jonasled.de/jonasled/buildx-docker:latest
  stage: build
  services:
    - docker:dind
  before_script:
    - docker context create build
    - docker buildx create build --use
    - docker run --privileged --rm tonistiigi/binfmt --install all
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  # Default branch leaves tagempty (= latest tag)
  # All other branches are tagged with the escaped branch name (commit ref slug)
  script:
    - |
      if [[ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]]; then
        tag="latest"
      else
        tag="$CI_COMMIT_REF_SLUG"
      fi
    - docker buildx build --platform linux/arm64,linux/amd64 --push --provenance false --tag "$CI_REGISTRY_IMAGE:${tag}" .

Bundle:
  stage: bundle
  image: alpine:latest
  dependencies:
    - Webpack
    - Composer
  script:
    - apk add zip
    - zip -r bundle.zip .
  artifacts:
    paths:
      - bundle.zip