diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dda0991 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,12 @@ +# Path-based git attributes +# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html + +# Ignore all test and documentation with "export-ignore". +/.gitattributes export-ignore +/.gitignore export-ignore +/.travis.yml export-ignore +/phpunit.xml.dist export-ignore +/tests export-ignore +/.editorconfig export-ignore +/.php.cs export-ignore +/.github export-ignore diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml new file mode 100644 index 0000000..a9322d1 --- /dev/null +++ b/.github/workflows/run-tests.yml @@ -0,0 +1,36 @@ +name: Run tests + +on: + push: + pull_request: + schedule: + - cron: '0 0 * * *' + +jobs: + php-tests: + runs-on: ubuntu-latest + + strategy: + matrix: + php: [7.4] + + name: PHP${{ matrix.php }} - ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v1 + + - name: Setup PHP + uses: shivammathur/setup-php@v1 + with: + php-version: ${{ matrix.php }} + coverage: none + + - name: Install dependencies + run: | + sudo composer self-update + composer update --prefer-stable --prefer-dist --no-interaction --no-suggest + + - name: Execute tests + run: vendor/bin/phpunit --testdox + \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8d6e996 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +build +composer.lock +docs +vendor +coverage +.phpunit.result.cache +.idea +.php_cs.cache diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..f7f4ac0 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,7 @@ +# Changelog + +All notable changes to `laravel-dashboard-shopify-tile` will be documented in this file + +## 1.0.0 - 2020-06-09 + +- initial release diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..b4ae1c4 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,55 @@ +# Contributing + +Contributions are **welcome** and will be fully **credited**. + +Please read and understand the contribution guide before creating an issue or pull request. + +## Etiquette + +This project is open source, and as such, the maintainers give their free time to build and maintain the source code +held within. They make the code freely available in the hope that it will be of use to other developers. It would be +extremely unfair for them to suffer abuse or anger for their hard work. + +Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the +world that developers are civilized and selfless people. + +It's the duty of the maintainer to ensure that all submissions to the project are of sufficient +quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. + +## Viability + +When requesting or submitting new features, first consider whether it might be useful to others. Open +source projects are used by many developers, who may have entirely different needs to your own. Think about +whether or not your feature is likely to be used by other users of the project. + +## Procedure + +Before filing an issue: + +- Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. +- Check to make sure your feature suggestion isn't already present within the project. +- Check the pull requests tab to ensure that the bug doesn't have a fix in progress. +- Check the pull requests tab to ensure that the feature isn't already in progress. + +Before submitting a pull request: + +- Check the codebase to ensure that your feature doesn't already exist. +- Check the pull requests to ensure that another person hasn't already submitted the feature or fix. + +## Requirements + +If the project maintainer has any additional requirements, you will find them listed here. + +- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). + +- **Add tests!** - Your patch won't be accepted if it doesn't have tests. + +- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. + +- **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. + +- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. + +- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. + +**Happy coding**! diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..d4d10d8 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) :vendor_name + +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..465977d --- /dev/null +++ b/README.md @@ -0,0 +1,94 @@ +# A tile to display Shopify information + +[![Latest Version on Packagist](https://img.shields.io/packagist/v/ingoldsby/laravel-dashboard-shopify-tile.svg?style=flat-square)](https://packagist.org/packages/ingoldsby/laravel-dashboard-shopify-tile) +[![Total Downloads](https://img.shields.io/packagist/dt/ingoldsby/laravel-dashboard-shopify-tile.svg?style=flat-square)](https://packagist.org/packages/ingoldsby/laravel-dashboard-shopify-tile) + +This tile can be used on [the Laravel Dashboard](https://docs.spatie.be/laravel-dashboard) to display Shopify information. + +## Installation + +You can install the package via composer: + +```bash +composer require ingoldsby/laravel-dashboard-shopify-tile +``` + +## Shopify API credentials + +Before using this tile you need to have created a [private app](https://help.shopify.com/en/manual/apps/private-apps) on the Shopify admin page and obtained an API key and password. + +## Usage + +In the `dashboard` config file, you must add this configuration in the `tiles` key. + +```php +// in config/dashboard.php + +return [ + // ... + 'tiles' => [ + 'shopify' => [ + 'api_key' => env('SHOPIFY_API_KEY'), + 'password' => env('SHOPIFY_API_PASSWORD'), + ] + ], +]; +``` + +In `app\Console\Kernel.php` you should schedule the `\Ingoldsby\ShopifyTile\Commands\FetchShopifyInfoCommand` to run. You can let it run every minute if you want. You could also run this less frequently if fast updates on the dashboard aren’t that important for this tile. + +```php +// in app/console/Kernel.php + +protected function schedule(Schedule $schedule) +{ + // ... + $schedule->command(\Ingoldsby\ShopifyTile\Commands\FetchShopifyInfoCommand::class)->everyMinute(); +} +``` + +In your dashboard view you can use three separate tiles: +* `livewire:shopify-tile` + +```html + + + +``` + +## Testing + +``` bash +composer test +``` + +## Changelog + +Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. + +## Contributing + +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. + +## Security + +If you discover any security related issues, please email instead of using the issue tracker. + +## Support Spatie + +I have learnt a lot from Spatie's various packages, including [Mailcoach](https://mailcoach.app), and would recommend you check them out if you want to know more. + +Learn how to create a package like this one, by watching Spatie's premium video course: + +[![Laravel Package training](https://spatie.be/github/package-training.jpg)](https://laravelpackage.training) + +Spatie invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support them by [buying one of their paid products](https://spatie.be/open-source/support-us). + +## Credits + +- [Ingoldsby](https://github.com/ingoldsby) +- [All Contributors](../../contributors) + +## License + +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..d8d1eb6 --- /dev/null +++ b/composer.json @@ -0,0 +1,51 @@ +{ + "name": "ingoldsby/laravel-dashboard-shopify-tile", + "description": "A Shopify tile for Laravel Dashboard", + "keywords": [ + "ingoldsby", + "laravel-dashboard-shopify-tile" + ], + "homepage": "https://github.com/ingoldsby/laravel-dashboard-shopify-tile", + "license": "MIT", + "authors": [ + { + "name": "Ingoldsby", + "email": "ingoldsby@ingoldsby.info", + "role": "Developer" + } + ], + "require": { + "php": "^7.4", + "spatie/laravel-dashboard": "^1.0" + }, + "require-dev": { + "phpunit/phpunit": "^9.0" + }, + "autoload": { + "psr-4": { + "Ingoldsby\\ShopifyTile\\": "src" + } + }, + "autoload-dev": { + "psr-4": { + "Spatie\\ShopifyTile\\Tests\\": "tests" + } + }, + "scripts": { + "test": "vendor/bin/phpunit", + "test-coverage": "vendor/bin/phpunit --coverage-html coverage", + "format": "vendor/bin/php-cs-fixer fix --allow-risky=yes" + }, + "config": { + "sort-packages": true + }, + "extra": { + "laravel": { + "providers": [ + "Ingoldsby\\ShopifyTile\\ShopifyServiceProvider" + ] + } + }, + "minimum-stability": "dev", + "prefer-stable": true +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..c6c093f --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,29 @@ + + + + + tests + + + + + src/ + + + + + + + + + \ No newline at end of file diff --git a/resources/views/all/tile.blade.php b/resources/views/all/tile.blade.php new file mode 100644 index 0000000..7d94955 --- /dev/null +++ b/resources/views/all/tile.blade.php @@ -0,0 +1,69 @@ + +
+
+
+
+
+ CUSTOMERS +
+
+ {{ $shopify['customers']['count'] }} +
+
+ @isset ($shopify['customers']['latest']['first_name']) + {{ ucfirst($shopify['customers']['latest']['first_name']) }} + @else + Last + @endif + registered {{ \Carbon\Carbon::parse($shopify['customers']['latest']['created_at'])->diffForHumans() }} +
+
+
+
+
+
+ ORDERS +
+
+ {{ $shopify['orders']['count'] }} +
+
+ {{ $shopify['orders']['latest']['name'] }} created {{ \Carbon\Carbon::parse($shopify['orders']['latest']['created_at'])->diffForHumans() }} +
+
+
+
+
+
+ ABANDONED CARTS +
+
+ {{ $shopify['checkouts']['count'] }} +
+ @isset ($shopify['checkouts']['latest']['created_at']) +
+ Last abandoned {{ \Carbon\Carbon::parse($shopify['checkouts']['latest']['created_at'])->diffForHumans() }} +
+ @else +
+ No active abandoned carts +
+ @endisset +
+
+
+
+
+ PRODUCTS +
+
+ {{ $shopify['products']['count'] }} +
+
+ {{ $shopify['products']['latest']['title'] }} created {{ \Carbon\Carbon::parse($shopify['orders']['latest']['created_at'])->diffForHumans() }} +
+
+
+
+
+
\ No newline at end of file diff --git a/src/Commands/FetchShopifyInfoCommand.php b/src/Commands/FetchShopifyInfoCommand.php new file mode 100644 index 0000000..41d4a43 --- /dev/null +++ b/src/Commands/FetchShopifyInfoCommand.php @@ -0,0 +1,35 @@ +info('Fetching Shopify Info ...'); + + $response['customers']['count'] = $api->getShopifyInfo('customers', 'count'); + $response['customers']['latest'] = $api->getShopifyInfo('customers', 'latest'); + + $response['orders']['count'] = $api->getShopifyInfo('orders', 'count'); + $response['orders']['latest'] = $api->getShopifyInfo('orders', 'latest'); + + $response['checkouts']['count'] = $api->getShopifyInfo('checkouts', 'count'); + $response['checkouts']['latest'] = $api->getShopifyInfo('checkouts', 'latest'); + + $response['products']['count'] = $api->getShopifyInfo('products', 'count'); + $response['products']['latest'] = $api->getShopifyInfo('products', 'latest'); + + ShopifyStore::make()->setShopifyInfo($response); + + $this->info('All done!'); + } +} \ No newline at end of file diff --git a/src/Components/ShopifyComponent.php b/src/Components/ShopifyComponent.php new file mode 100644 index 0000000..9614b82 --- /dev/null +++ b/src/Components/ShopifyComponent.php @@ -0,0 +1,25 @@ +position = $position; + } + + public function render() + { + return view('dashboard-shopify-tiles::all.tile', [ + 'shopify' => ShopifyStore::make()->getShopifyInfo(), + 'refreshIntervalInSeconds' => config('dashboard.tiles.shopify.refresh_interval_in_seconds') ?? 60, + ]); + } +} \ No newline at end of file diff --git a/src/ShopifyApi.php b/src/ShopifyApi.php new file mode 100644 index 0000000..b3d24f6 --- /dev/null +++ b/src/ShopifyApi.php @@ -0,0 +1,71 @@ +json(); + + if ($type == 'count') { + return $response['count']; + } + + if (isset($response[$endpoint][0])) { + return $response[$endpoint][0]; + } + + return []; + + } + + public static function generateUrl(string $url, string $endpoint, string $type) : string + { + + $url = $url . '/' . $endpoint; + + if ($type == 'count') { + $url = $url . '/' . $type; + } + + $url = $url . '.json'; + + if ($type == 'latest') { + $url = $url . '?limit=1'; + + switch ($endpoint) { + case 'orders': + $url = $url . self::ORDERS_FIELDS; + break; + case 'customers': + $url = $url . self::CUSTOMERS_FIELDS; + break; + case 'checkouts': + $url = $url . self::CHECKOUTS_FIELDS; + break; + case 'products': + $url = $url . self::PRODUCTS_FIELDS; + break; + } + + } + + return $url; + + } + +} \ No newline at end of file diff --git a/src/ShopifyServiceProvider.php b/src/ShopifyServiceProvider.php new file mode 100644 index 0000000..e955fa1 --- /dev/null +++ b/src/ShopifyServiceProvider.php @@ -0,0 +1,30 @@ +app->runningInConsole()) { + $this->commands([ + FetchShopifyInfoCommand::class, + ]); + } + + $this->publishes([ + __DIR__ . '/../resources/views' => resource_path('views/vendor/dashboard-shopify-tiles'), + ], 'dashboard-shopify-tiles'); + + $this->loadViewsFrom(__DIR__ . '/../resources/views', 'dashboard-shopify-tiles'); + + Livewire::component('shopify-tile', ShopifyComponent::class); + + } +} diff --git a/src/ShopifyStore.php b/src/ShopifyStore.php new file mode 100644 index 0000000..85ac83e --- /dev/null +++ b/src/ShopifyStore.php @@ -0,0 +1,33 @@ +tile = Tile::firstOrCreateForName("shopify"); + } + + public function setShopifyInfo($info) : self + { + $this->tile->putData('shopify', $info); + + return $this; + } + + public function getShopifyInfo() + { + return $this->tile->getData('shopify') ?? []; + } + +} diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php new file mode 100644 index 0000000..79a0cc5 --- /dev/null +++ b/tests/ExampleTest.php @@ -0,0 +1,14 @@ +assertTrue(true); + } +} \ No newline at end of file