webparking / logic4-client
A package to connect with Logic4
Installs: 1 656
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 1
Forks: 2
Open Issues: 2
Requires
- php: ^8.1
- guzzlehttp/guzzle: ^7.8
- nesbot/carbon: ^2.71
- psr/simple-cache: ^3.0
Requires (Dev)
- cebe/php-openapi: ^1.7
- friendsofphp/php-cs-fixer: ^3.35
- mockery/mockery: ^1.6
- nette/php-generator: ^4.1
- orchestra/testbench: ^8.13
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpstan/phpstan-mockery: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- phpunit/phpunit: ^10.4
- rector/rector: ^1.1.1
- rregeer/phpunit-coverage-check: ^0.3.1
- symfony/var-dumper: ^6.3
- dev-master
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.3.1
- v2.3.0
- v2.2.0
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.2
- v2.0.1
- v2.0.0
- v1.0.0
- dev-feature/update-endpoints
- dev-dependabot/github_actions/peter-evans/create-pull-request-7
- dev-improvement/dto-per-api-version
- dev-feature/fix-github-job
- dev-generator-v2
- dev-feature/improve-pagination-support
- dev-bugfix/missing-request-parameters
This package is auto-updated.
Last update: 2024-11-04 09:45:02 UTC
README
Logic4 client
Please note that this API client is not created or maintained by Logic4. For questions about Logic4 or it's API please contact Logic4 support.
Installation
You can install the package via composer:
composer require webparking/logic4-client
Configuration
In order to use the Logic4 API, you need to configure the AccessTokenManager
with your credentials. You can do this by calling the configure
method on the AccessTokenManager
instance.
The following configuration options are required
public_key
- The public key of your Logic4 accountcompany_key
- The company key of your Logic4 accountusername
- The username of your Logic4 accountsecret_key
- The secret key of your Logic4 accountpassword
- The password of your Logic4 accountadministration_id
- The administration id of your Logic4 account
The access token is automatically refreshed when it expires and stored in the cache. The cache object is set using the $cache
property on the AccessTokenManager
class and should implement the Psr\SimpleCache\CacheInterface
interface.
After configuring the AccessTokenManager
you can use any request class in your application without having to worry about the access token and other middleware.
Example configuration
# In this example, we load the configuration from a .env file. $dotEnv = Dotenv\Dotenv::createImmutable(__DIR__); $credentials = $dotEnv->load() # We use the ArrayStore for testing purposes, but you can use any cache implementation that implements the Psr\SimpleCache\CacheInterface interface. $cache = new \Illuminate\Cache\Repository(new \Illuminate\Cache\ArrayStore()); $tokenManager = new \Webparking\Logic4Client\AccessTokenManager($cache); $tokenManager->configure($credentials); # Initialize the ClientFactory using the AccessTokenManager $clientFactory = new \Webparking\Logic4Client\ClientFactory($tokenManager); # Initialize the request class using the ClientFactory $request = new \Webparking\Logic4Client\Requests\ProductRequest($clientFactory); $request->getProducts([ 'DebtorId' => 1, ]);
Laravel integration
Integration with Laravel is easily done by configuring the AccessTokenManager
after it has been resolved by the Laravel IoC Container.
Add the following code to your AppServiceProvider
class:
use Webparking\Logic4Client\AccessTokenManager; $this->app ->afterResolving(AccessTokenManager::class, fn (AccessTokenManager $tokenManager) => $tokenManager->configure([ 'public_key' => config('services.logic4.public_key'), 'company_key' => config('services.logic4.company_key'), 'username' => config('services.logic4.username'), 'secret_key' => config('services.logic4.secret_key'), 'password' => config('services.logic4.password'), 'administration_id' => config('services.logic4.administration_id'), ]));
This makes it possible to directly use any request class in your application without having to worry about the access token and other middleware.
Example request
use Webparking\Logic4Client\Requests\ProductRequest; use Webparking\Logic4Client\Data\Product; use Illuminate\Support\Collection; /** @return array<Product> */ public function index(ProductRequest $productRequest, int $debtorId): array { $products = $productRequest->getProducts([ 'DebtorId' => $debtorId, ]); return iterator_to_array($products); }
Testing
You can test the package by running ./vendor/bin/phpunit
in the root of the project. Make sure install the dependencies by running composer install
before running the tests.
composer install ./vendor/bin/phpunit
Preventing stray requests
If you would like to ensure that all requests sent via the client have been mocked throughout your individual test or complete test suite, you can call the preventStrayRequests
method on the \Webparking\Logic4Client\ClientFactory
class. After calling this method, any requests that do not have a corresponding mock will throw an exception rather than making the actual request:
// turns on stray request prevention \Webparking\Logic4Client\ClientFactory::preventStrayRequests(); // an exception is thrown if the request is not mocked $request->getProducts(['DebtorId' => 1]); // turns off stray request prevention \Webparking\Logic4Client\ClientFactory::preventStrayRequests(false); // no exception is thrown and will make the actual request $request->getProducts(['DebtorId' => 1]);
Contributing
Please report any issue you find in the issues page. Pull requests are more than welcome.
Generating endpoints
The endpoints can automatically be generated using the generate
command. This command will generate a request class for each endpoint and a client class that can be used to instantiate the request classes.
make generate
License
The MIT License (MIT). Please see License File for more information.