diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..a7c44dd --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +root = true + +[*] +charset = utf-8 +indent_size = 4 +indent_style = space +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false + +[*.{yml,yaml}] +indent_size = 2 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..aa8ebc7 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,15 @@ +# Path-based git attributes +# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html + +# Ignore all test and documentation with "export-ignore". +/.github export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore +/phpunit.xml.dist export-ignore +/psalm.xml.dist export-ignore +/tests export-ignore +/.editorconfig export-ignore +/.php-cs-fixer.dist.php export-ignore +/art export-ignore +/docs export-ignore +/UPGRADING.md export-ignore diff --git a/.github/ISSUE_TEMPLATE/bug.yml b/.github/ISSUE_TEMPLATE/bug.yml new file mode 100644 index 0000000..a9933e1 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug.yml @@ -0,0 +1,58 @@ +name: Bug Report +description: Report an Issue or Bug with the Package +title: "[Bug]: " +labels: ["bug"] +body: + - type: markdown + attributes: + value: | + We're sorry to hear you have a problem. Can you help us solve it by providing the following details. + - type: textarea + id: what-happened + attributes: + label: What happened? + description: What did you expect to happen? + placeholder: I cannot currently do X thing because when I do, it breaks X thing. + validations: + required: true + - type: textarea + id: how-to-reproduce + attributes: + label: How to reproduce the bug + description: How did this occur, please add any config values used and provide a set of reliable steps if possible. + placeholder: When I do X I see Y. + validations: + required: true + - type: input + id: package-version + attributes: + label: Package Version + description: What version of our Package are you running? Please be as specific as possible + placeholder: 2.0.0 + validations: + required: true + - type: input + id: php-version + attributes: + label: PHP Version + description: What version of PHP are you running? Please be as specific as possible + placeholder: 8.2.0 + validations: + required: true + - type: dropdown + id: operating-systems + attributes: + label: Which operating systems does with happen with? + description: You may select more than one. + multiple: true + options: + - macOS + - Windows + - Linux + - type: textarea + id: notes + attributes: + label: Notes + description: Use this field to provide any other notes that you feel might be relevant to the issue. + validations: + required: false diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 0000000..ed523ac --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,11 @@ +blank_issues_enabled: false +contact_links: + - name: Ask a question + url: https://github.com/PiteurStudio/php-domain-dz/discussions/new?category=q-a + about: Ask the community for help + - name: Request a feature + url: https://github.com/PiteurStudio/php-domain-dz/discussions/new?category=ideas + about: Share ideas for new features + - name: Report a security issue + url: https://github.com/PiteurStudio/php-domain-dz/security/policy + about: Learn how to notify us for sensitive bugs diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..39b1580 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,19 @@ +# Please see the documentation for all configuration options: +# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates + +version: 2 +updates: + + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" + labels: + - "dependencies" + + - package-ecosystem: "composer" + directory: "/" + schedule: + interval: "weekly" + labels: + - "dependencies" diff --git a/.github/workflows/dependabot-auto-merge.yml b/.github/workflows/dependabot-auto-merge.yml new file mode 100644 index 0000000..c3ad22d --- /dev/null +++ b/.github/workflows/dependabot-auto-merge.yml @@ -0,0 +1,33 @@ +name: dependabot-auto-merge +on: pull_request_target + +permissions: + pull-requests: write + contents: write + +jobs: + dependabot: + runs-on: ubuntu-latest + timeout-minutes: 5 + if: ${{ github.actor == 'dependabot[bot]' }} + steps: + + - name: Dependabot metadata + id: metadata + uses: dependabot/fetch-metadata@v2.2.0 + with: + github-token: "${{ secrets.GITHUB_TOKEN }}" + + - name: Auto-merge Dependabot PRs for semver-minor updates + if: ${{steps.metadata.outputs.update-type == 'version-update:semver-minor'}} + run: gh pr merge --auto --merge "$PR_URL" + env: + PR_URL: ${{github.event.pull_request.html_url}} + GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} + + - name: Auto-merge Dependabot PRs for semver-patch updates + if: ${{steps.metadata.outputs.update-type == 'version-update:semver-patch'}} + run: gh pr merge --auto --merge "$PR_URL" + env: + PR_URL: ${{github.event.pull_request.html_url}} + GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} diff --git a/.github/workflows/fix-php-code-style-issues.yml b/.github/workflows/fix-php-code-style-issues.yml new file mode 100644 index 0000000..56d54d3 --- /dev/null +++ b/.github/workflows/fix-php-code-style-issues.yml @@ -0,0 +1,28 @@ +name: Fix PHP code style issues + +on: + push: + paths: + - '**.php' + +permissions: + contents: write + +jobs: + php-code-styling: + runs-on: ubuntu-latest + timeout-minutes: 5 + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + ref: ${{ github.head_ref }} + + - name: Fix PHP code style issues + uses: aglipanci/laravel-pint-action@2.4 + + - name: Commit changes + uses: stefanzweifel/git-auto-commit-action@v5 + with: + commit_message: Fix styling diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml new file mode 100644 index 0000000..97f6087 --- /dev/null +++ b/.github/workflows/run-tests.yml @@ -0,0 +1,50 @@ +name: Tests + +on: + push: + paths: + - '**.php' + - '.github/workflows/run-tests.yml' + - 'phpunit.xml.dist' + - 'composer.json' + - 'composer.lock' + +jobs: + test: + runs-on: ${{ matrix.os }} + timeout-minutes: 5 + strategy: + fail-fast: true + matrix: +# os: [ubuntu-latest, windows-latest] + os: [ubuntu-latest] + php: [8.0] +# php: [8.3, 8.2, 8.1 , 8.0] + stability: [prefer-lowest, prefer-stable] + + name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }} + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo + coverage: none + + - name: Setup problem matchers + run: | + echo "::add-matcher::${{ runner.tool_cache }}/php.json" + echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" + + - name: Install dependencies + run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction + + - name: List Installed Dependencies + run: composer show -D + + - name: Execute tests + run: vendor/bin/pest --ci diff --git a/.github/workflows/update-changelog.yml b/.github/workflows/update-changelog.yml new file mode 100644 index 0000000..39de30d --- /dev/null +++ b/.github/workflows/update-changelog.yml @@ -0,0 +1,32 @@ +name: "Update Changelog" + +on: + release: + types: [released] + +permissions: + contents: write + +jobs: + update: + runs-on: ubuntu-latest + timeout-minutes: 5 + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + ref: main + + - name: Update Changelog + uses: stefanzweifel/changelog-updater-action@v1 + with: + latest-version: ${{ github.event.release.name }} + release-notes: ${{ github.event.release.body }} + + - name: Commit updated CHANGELOG + uses: stefanzweifel/git-auto-commit-action@v5 + with: + branch: main + commit_message: Update CHANGELOG + file_pattern: CHANGELOG.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..81d7cc9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,33 @@ +# Composer Related +composer.lock +/vendor + +# Frontend Assets +/node_modules + +# Logs +npm-debug.log +yarn-error.log + +# Caches +.phpunit.cache +.phpunit.result.cache +/build + +# Editors +/.idea +/.fleet +/.vscode + +# Code sniffer +.php_cs +.php_cs.cache +.php-cs-fixer.cache + +# Misc +phpunit.xml +phpstan.neon +psalm.xml +testbench.yaml +/docs +/coverage diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..665542f --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,4 @@ +# Changelog + +All notable changes to `php-domain-dz` will be documented in this file. + diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..302b375 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) PiteurStudio + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..48a5a8e --- /dev/null +++ b/README.md @@ -0,0 +1,71 @@ +# checks .dz domains via the nic.dz API + +[![Latest Version on Packagist](https://img.shields.io/packagist/v/piteurstudio/php-domain-dz.svg?style=flat-square)](https://packagist.org/packages/piteurstudio/php-domain-dz) +[![Tests](https://img.shields.io/github/actions/workflow/status/piteurstudio/php-domain-dz/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/piteurstudio/php-domain-dz/actions/workflows/run-tests.yml) +[![Total Downloads](https://img.shields.io/packagist/dt/piteurstudio/php-domain-dz.svg?style=flat-square)](https://packagist.org/packages/piteurstudio/php-domain-dz) + +This php packages checks .dz domains via the [nic.dz Public API](https://api.nic.dz/swagger-ui/index.html). + +## Installation + +You can install the package via composer: + +```bash +composer require piteurstudio/php-nicdz +``` + +## Usage + +### Check if a domain is available + +```php +use PiteurStudio\NicDz; + +include 'vendor/autoload.php'; + +$dztld = new NicDz('example-domain.dz'); + +$dztld->isAvailable(); // return bool +``` + +### Get whois information in different formats + +```php +$dztld = new NicDz('piteur-studio.dz'); + +$dztld->whois()->toArray(); + +$dztld->whois()->toObject(); + +$dztld->whois()->toString(); + +$dztld->whois()->toJson(); + +``` + +## Testing + +```bash +composer test +``` + +## Changelog + +Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. + +## Contributing + +Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details. + +## Security Vulnerabilities + +Please review [our security policy](../../security/policy) on how to report security vulnerabilities. + +## Credits + +- [Piteur Studio](https://github.com/PiteurStudio) +- [All Contributors](../../contributors) + +## License + +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..8f6e1c8 --- /dev/null +++ b/composer.json @@ -0,0 +1,52 @@ +{ + "name": "piteurstudio/php-nicdz", + "description": "checks availability of .DZ and whois data via the nic.dz API", + "keywords": [ + "PiteurStudio", + "php-domain-dz", + "nic.dz", + "Algeria" + ], + "homepage": "https://github.com/piteurstudio/php-domain-dz", + "license": "MIT", + "authors": [ + { + "name": "Nassim", + "email": "mb.nassim@piteur-studio.dz", + "homepage": "https://www.piteur-studio.dz" + } + ], + "require": { + "php": "^8.0", + "symfony/http-client": "^7.1" + }, + "require-dev": { + "pestphp/pest": "^3.0", + "laravel/pint": "^1.0", + "spatie/ray": "^1.0" + }, + "autoload": { + "psr-4": { + "PiteurStudio\\": "src" + } + }, + "autoload-dev": { + "psr-4": { + "PiteurStudio\\Tests\\": "tests" + } + }, + "scripts": { + "test": "vendor/bin/pest", + "test-coverage": "vendor/bin/pest --coverage", + "format": "vendor/bin/pint" + }, + "config": { + "sort-packages": true, + "allow-plugins": { + "pestphp/pest-plugin": true, + "phpstan/extension-installer": true + } + }, + "minimum-stability": "dev", + "prefer-stable": true +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..6a901b6 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,38 @@ + + + + + tests + + + + + + + + + + + + + + + ./src + + + diff --git a/src/NicDz.php b/src/NicDz.php new file mode 100755 index 0000000..4d0b9b6 --- /dev/null +++ b/src/NicDz.php @@ -0,0 +1,134 @@ +domain = $domain; + + $this->client = HttpClient::create([ + 'verify_peer' => false, + 'verify_host' => false, + 'timeout' => $this->timeout, // Timeout in seconds + ]); + + // Retry on nic.dz api fail (400 : Bad request and 409 : Conflict status codes) + + $this->client = new RetryableHttpClient( + $this->client, + new GenericRetryStrategy([400, 409]), // Retry on these status codes, max 3 retries + $this->maxRetries, // Max retry attempts + ); + + try { + $this->response = $this->client->request( + 'GET', + $this->nicdz_endpoint.'v1/domains/'.$this->domain, + ); + } catch (TransportExceptionInterface $e) { + throw new \RuntimeException('Error retrieving domain data: '.$e->getMessage()); + } + } + + /** + * Check if the domain is available. + */ + public function isAvailable(): bool + { + $statusCode = $this->response->getStatusCode(); + + // 302 => successful operation ( strange status code from nic.dz ) , mean domain is not available + + if ($statusCode === 302) { + return false; + } elseif ($statusCode === 404) { + return true; + } + + throw new \RuntimeException('Unexpected status code: '.$statusCode); + } + + /** + * Get WHOIS information for the domain. + * + * @return NicDz + * + * @throws \Exception if domain is available or no WHOIS data is present + */ + public function whois(): static + { + + $array = $this->response->toArray(false); // use `false` to bypass 302 status code exception + + if ($this->isAvailable()) { + + $this->whoisData = [ + 'domain' => $this->domain, + 'title' => 'Whois Record Not Available', + 'message' => 'This domain is not registered.', + ]; + + } else { + + // Remove id and _links keys as they are not needed in most cases + unset($array['id']); + unset($array['_links']); + + $this->whoisData = $array; + + } + + return $this; + } + + /*Whois Record Not Available*/ + + public function toArray(): array + { + return $this->whoisData; + } + + public function toJson(): string + { + return json_encode($this->whoisData); + } + + public function toObject(): object + { + return json_decode($this->toJson()); + } + + public function toString(): string + { + $result = ''; + + foreach ($this->whoisData as $key => $value) { + $result .= $key.' : '.$value."\n"; // Append key : value and newline + } + + return $result; + } +} diff --git a/tests/DomainDzTest.php b/tests/DomainDzTest.php new file mode 100644 index 0000000..9224622 --- /dev/null +++ b/tests/DomainDzTest.php @@ -0,0 +1,44 @@ +isAvailable())->toBeFalse(); +}); + +test('isAvailable() : return true when domain is available', function () { + + $domainDz = new NicDz('google-domain-makacho.dz'); + + expect($domainDz->isAvailable())->toBeTrue(); +}); + +it('has whois data when its unavailable', function () { + + $domainDz = new NicDz('google.dz'); + + expect($domainDz->whois()->toArray())->toBeArray() + ->and($domainDz->whois()->toJson())->toBeJson() + ->and($domainDz->whois()->toObject())->toBeObject() + ->and($domainDz->whois()->toString())->toBeString(); + +}); + +it('has whois message when its available', function () { + + $domainDz = new NicDz('google-makacho.dz'); + + expect($domainDz->whois()->toArray())->toBeArray() + ->and($domainDz->whois()->toJson())->toBeJson() + ->and($domainDz->whois()->toObject())->toBeObject() + ->and($domainDz->whois()->toString())->toBeString() + ->and($domainDz->whois()->toArray())->toBe([ + 'domain' => 'google-makacho.dz', + 'title' => 'Whois Record Not Available', + 'message' => 'This domain is not registered.', + ]); + +}); diff --git a/tests/Pest.php b/tests/Pest.php new file mode 100644 index 0000000..b3d9bbc --- /dev/null +++ b/tests/Pest.php @@ -0,0 +1 @@ +