diff --git a/.gitignore b/.gitignore index 97896f7..0e5cbaf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,9 @@ -composer.lock +build vendor -.DS_Store \ No newline at end of file +tests/TestSupport/temp +tests/temp +composer.lock +phpunit.xml +.env +.phpunit.result.cache +.php_cs.cache \ No newline at end of file diff --git a/README.md b/README.md index 6331ecf..7f7bf99 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,9 @@ Simple wrapper for official php package https://github.com/fakturoid/fakturoid-p ### Docs -- [Installation](#installation) -- [Configuration](#configuration) -- [Examples](#examples) +- [Installation](#installation) +- [Configuration](#configuration) +- [Examples](#examples) ## Installation @@ -20,28 +20,6 @@ composer require dominik-wbz/laravel-fakturoid This will both update composer.json and install the package into the vendor/ directory. -Next, add the service provider and facade to `config/app.php` - -Add the service provider to providers: - -``` -'providers' => [ - ... - WEBIZ\LaravelFakturoid\FakturoidServiceProvider::class, - ... -] -``` - -And add the facade to aliases: - -``` -'aliases' => [ - ... - 'Fakturoid' => WEBIZ\LaravelFakturoid\Facade::class, - ... -] -``` - ### Step 2: Configuration First initialise the config file by running this command: @@ -56,10 +34,10 @@ With this command, initialize the configuration and modify the created file, loc ```php return [ - 'account_name' => 'XXX', - 'account_email' => 'XXX', - 'account_api_key' => 'XXX', - 'app_contact' => 'PHPlib ', + 'account_name' => env('FAKTUROID_NAME', 'XXX'), + 'account_email' => env('FAKTUROID_EMAIL', 'XXX'), + 'account_api_key' => env('FAKTUROID_API_KEY', 'XXX'), + 'app_contact' => env('FAKTUROID_APP_CONTACT', 'Application '), ]; ``` @@ -70,35 +48,37 @@ return [ ```php use Fakturoid; -use Fakturoid\Exception; try { - // create subject - $subject = Fakturoid::createSubject(array( - 'name' => 'Firma s.r.o.', - 'email' => 'aloha@pokus.cz' - )); - if ($subject->getBody()) { - $subject = $subject->getBody(); - - // create invoice with lines - $lines = array(array( - 'name' => 'Big sale', - 'quantity' => 1, - 'unit_price' => 1000 - )); - $invoice = Fakturoid::createInvoice(array('subject_id' => $subject->id, 'lines' => $lines)); - $invoice = $invoice->getBody(); - - // send created invoice - Fakturoid::fireInvoice($invoice->id, 'deliver'); - } -} catch (Exception $e) { - dd($e->getCode() . ": " . $e->getMessage()); + // create subject + $subject = Fakturoid::createSubject(array( + 'name' => 'Firma s.r.o.', + 'email' => 'aloha@pokus.cz' + )); + if ($subject->getBody()) { + $subject = $subject->getBody(); + + // create invoice with lines + $lines = [ + [ + 'name' => 'Big sale', + 'quantity' => 1, + 'unit_price' => 1000 + ], + ]; + + $invoice = Fakturoid::createInvoice(array('subject_id' => $subject->id, 'lines' => $lines)); + $invoice = $invoice->getBody(); + + // send created invoice + Fakturoid::fireInvoice($invoice->id, 'deliver'); + } +} catch (\Exception $e) { + dd($e->getCode() . ": " . $e->getMessage()); } ``` ## License -Copyright (c) 2019 webiz eu s.r.o MIT Licensed. +Copyright (c) 2019 - 2020 webiz eu s.r.o MIT Licensed. diff --git a/composer.json b/composer.json index f4b4246..8a5e848 100644 --- a/composer.json +++ b/composer.json @@ -1,20 +1,65 @@ { - "name": "dominik-wbz/laravel-fakturoid", - "license": "MIT", - "authors": [ - { - "name": "Dominik Nguyen", - "email": "dom@webiz.cz" - } - ], - "require": { - "php": ">=5.6.0", - "illuminate/support": "5.*", - "fakturoid/fakturoid-php": "^1.0" - }, - "autoload": { - "psr-4": { - "WEBIZ\\LaravelFakturoid\\": "src/" - } - } -} + "name": "dominik-wbz/laravel-fakturoid", + "description": "Fakturoid Laravel Wrapper", + "keywords": [ + "webiz", + "laravel", + "ecommerce", + "invoicing" + ], + "homepage": "https://github.com/dominik-wbz/laravel-fakturoid", + "license": "MIT", + "type": "library", + "authors": [ + { + "name": "Dominik Nguyen", + "email": "dom@webiz.cz", + "role": "Developer" + }, + { + "name": "Jakub Theimer", + "email": "theimer@madne.st", + "role": "Developer" + } + ], + "require": { + "php": ">=7.2.0", + "laravel/framework": "^7.0", + "illuminate/support": "^6.18|^7.0", + "fakturoid/fakturoid-php": "^1.0" + }, + "require-dev": { + "mockery/mockery": "^1.3", + "orchestra/testbench": "^5.1", + "phpunit/phpunit": "^8.0|^9.0" + }, + "autoload": { + "psr-4": { + "WEBIZ\\LaravelFakturoid\\": "src" + } + }, + "autoload-dev": { + "psr-4": { + "WEBIZ\\LaravelFakturoid\\Tests\\": "tests" + } + }, + "minimum-stability": "dev", + "prefer-stable": true, + "scripts": { + "test": "vendor/bin/phpunit", + "test-coverage": "vendor/bin/phpunit --coverage-html coverage" + }, + "config": { + "sort-packages": true + }, + "extra": { + "laravel": { + "providers": [ + "WEBIZ\\LaravelFakturoid\\FakturoidServiceProvider" + ], + "aliases": { + "Fakturoid": "WEBIZ\\LaravelFakturoid\\Facade" + } + } + } +} \ No newline at end of file diff --git a/config/fakturoid.php b/config/fakturoid.php new file mode 100644 index 0000000..cd3bba1 --- /dev/null +++ b/config/fakturoid.php @@ -0,0 +1,8 @@ + env('FAKTUROID_NAME', 'XXX'), + 'account_email' => env('FAKTUROID_EMAIL', 'XXX'), + 'account_api_key' => env('FAKTUROID_API_KEY', 'XXX'), + 'app_contact' => env('FAKTUROID_APP_CONTACT', 'Application '), +]; diff --git a/src/Facade.php b/src/Facade.php index 7d2bb09..1139abf 100644 --- a/src/Facade.php +++ b/src/Facade.php @@ -4,11 +4,15 @@ use Illuminate\Support\Facades\Facade as LaravelFacade; -class Facade extends LaravelFacade { +class Facade extends LaravelFacade +{ /** * Get the registered name of the component. * * @return string */ - protected static function getFacadeAccessor() {return 'Fakturoid';} -} \ No newline at end of file + protected static function getFacadeAccessor() + { + return 'laravel-fakturoid'; + } +} diff --git a/src/FakturoidServiceProvider.php b/src/FakturoidServiceProvider.php index 3aa56ba..5f07612 100644 --- a/src/FakturoidServiceProvider.php +++ b/src/FakturoidServiceProvider.php @@ -5,29 +5,33 @@ use Illuminate\Support\ServiceProvider; use WEBIZ\LaravelFakturoid\LaravelFakturoid; -class FakturoidServiceProvider extends ServiceProvider { - /** - * Bootstrap services. - * - * @return void - */ - public function boot() { - $this->publishes([ - __DIR__ . '/config.php' => config_path('fakturoid.php'), - ]); - } +class FakturoidServiceProvider extends ServiceProvider +{ + /** + * Bootstrap the application services. + */ + public function boot() + { + if ($this->app->runningInConsole()) { + $this->publishes([ + __DIR__ . '/../config/fakturoid.php' => config_path('fakturoid.php'), + ], 'config'); + } + } - /** - * Register services. - * - * @return void - */ - public function register() { - if (is_dir($vendor = __DIR__ . '/../vendor')) { - require_once $vendor . '/autoload.php'; - } - $this->app->singleton('Fakturoid', function ($app) { - return new LaravelFakturoid(); - }); - } + /** + * Register services. + * + * @return void + */ + public function register() + { + // Automatically apply the package configuration + $this->mergeConfigFrom(__DIR__ . '/../config/fakturoid.php', 'fakturoid'); + + // Register the main class to use with the facade + $this->app->singleton('laravel-fakturoid', function () { + return new LaravelFakturoid; + }); + } } diff --git a/src/LaravelFakturoid.php b/src/LaravelFakturoid.php index cc0fdd3..ee1f4bd 100644 --- a/src/LaravelFakturoid.php +++ b/src/LaravelFakturoid.php @@ -2,35 +2,32 @@ namespace WEBIZ\LaravelFakturoid; -use Fakturoid; - -class LaravelFakturoid { - - public function __construct() { - $this->config = [ - 'account_name' => config('fakturoid.account_name'), - 'account_email' => config('fakturoid.account_email'), - 'account_api_key' => config('fakturoid.account_api_key'), - 'app_contact' => config('fakturoid.app_contact'), - ]; - $this->initFakturoid(); - } - - protected function initFakturoid() { - $this->fakturoid = new Fakturoid\Client($this->config['account_name'], $this->config['account_email'], $this->config['account_api_key'], $this->config['app_contact']); - - return $this->fakturoid; - } - - public function __call($name, $arguments) { - if (method_exists($this, $name)) { - return $this->{$name}(...$arguments); - } else if (method_exists($this->fakturoid, $name)) { - $fakturoid = $this->fakturoid; - $methodResult = $fakturoid->{$name}(...$arguments); - return $methodResult; - } - return null; - } - +use Fakturoid\Client as FakturoidClient; + +class LaravelFakturoid +{ + protected FakturoidClient $fakturoid; + + public function __construct() + { + $this->fakturoid = new FakturoidClient( + config('fakturoid.account_name'), + config('fakturoid.account_email'), + config('fakturoid.account_api_key'), + config('fakturoid.app_contact') + ); + } + + public function __call($method, $arguments) + { + if (method_exists($this, $method)) { + return $this->{$method}(...$arguments); + } + + if (method_exists($this->fakturoid, $method)) { + return $this->fakturoid->{$method}(...$arguments); + } + + throw new \BadMethodCallException("Method '{$method}' does not exist on Fakturoid instance."); + } } diff --git a/src/config.php b/src/config.php deleted file mode 100644 index fb07924..0000000 --- a/src/config.php +++ /dev/null @@ -1,7 +0,0 @@ - 'XXX', - 'account_email' => 'XXX', - 'account_api_key' => 'XXX', - 'app_contact' => 'PHPlib ', -]; \ No newline at end of file