mnavarrocarter / service-consumer-bundle
Installs: 77
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^7.1.3
- guzzlehttp/guzzle: ^6.3
- psr/http-message: ^1.0
- psr/log: ^1.0
- symfony/framework-bundle: ^4.0
Requires (Dev)
- symfony/phpunit-bridge: ^4.0
- symfony/var-dumper: ^4.0
- symfony/yaml: ^4.0
This package is auto-updated.
Last update: 2019-08-18 18:31:46 UTC
README
A Symfony Bundle for efficiently manage Soap and Http Calls.
Install
composer require mnavarrocarter/service-consumer-bundle
Why this bundle?
When working with different applications and web services, it is very easy to bloat up classes by mixing responsibilities and grouping a bunch of calls on them. What this bundle helps you achieve is good separation of concerns, and a useful way of organizing your Http and Soap Requests. The idea is for each call to have it's own class, that will be responsible for managing request building, error handling and other things.
Features
- Organize your external calls in classes, keeping them separated from others parts of your code.
- Automatic logging of request/response of every Soap and Http call to the
LoggerInterface
implementation you like.
Requirements
HttpConsumer
A client that can work with PSR-7 message implementations is needed. Guzzle 6+ is the default alternative of this bundle. This will be the case until PSR-18 is approved (hopefully!). That will be, most likely, version 2.0 of this bundle, eliminating the coupling with Guzzle.
SoapConsumer
The only requirement is to have installed/enabled the php-soap
extension in your machine.
Logger
Also, a PSR-3 Logger implementation is needed. Monolog, that comes bundled with Symfony's Framework Bundle is the default one, but you can swap the implementation for whatever thing you want in the bundle config by referencing a valid service name.
Configuration
Both service consumers come disabled by default. You must explicitly enable in the configuration the consumers you wish to use, they come disabled by default:
mnc_service_consumer: http: enabled: true soap: enabled: true # You can pass an array of wsdl dirs to use function auto discovery. wsdl_dirs: - '%kernel.project_dir%/Tests/Builder/Fixtures' # You can swap the logger implementation here. logger: 'logger'
Usage
Creating a Method
First, you must create a method so a consumer can use it. The recommended practice
is to create a folder called SoapMethod
or HttpMethod
depending of your case, and
then group the different services in their folders.
Then, create a class that extends either SoapMethodInterface
or HttpMethodInterface
and implement the required methods. You can find documentation about them in the
interface. Make sure you read it!
A good practice is to create an abstract class first, so you can configure the defaults
for a common set of calls, like a base_uri
or a wsdl
, or soap class mapping info.
If you use Symfony's autowiring and autoconfigure feature, all the classes implementing
those interfaces will be automatically configured and injected into their respective
consumer. If you don't, then make sure to register them as services adding the
mnc_service_consumer.soap_method
or mnc_service_consumer.http_method
tags.
Calling your method from the consumer
Make sure you have enabled your consumer and call it using it's service name:
mnc_service_consumer.http
or mnc_service_consumer.soap
.
You can also call them by their FQCN or by injecting the interface, which is bound to the implementation so you can use autowiring, or easily create your own implementation if you want to, and swap it. In this case, we are injecting the service by typehinting the interface.
Passing Arguments to your methods from outside
It is best practice to always build the required arguments inside your method class, but sometimes is useful to allow adding those from the outside call, maybe for testing purposes.
For this the execute method takes, as an optional second argument, an array of arguments that will be injected in different ways depending of the method type: Soap or Http.
For the classes implementing the HttpMehtodInterface
, the arguments will be injected
into the getRequest()
method as an array. So you can fetch them:
For the classes implementing SoapMethodInterface
, they will be merged with the
arguments that come from the getArguments()
method. If a key gets repeated, it will
be overwritten by the arguments passed in the consumer call.
Other methods you can use
There are some other methods that you can call from the consumer that could be useful: