Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling XML data #6

Open
TwassistantKeith opened this issue Aug 17, 2016 · 2 comments
Open

Handling XML data #6

TwassistantKeith opened this issue Aug 17, 2016 · 2 comments

Comments

@TwassistantKeith
Copy link

Hey there, I am trying to handle XML data in the body of a payload from the queue:

array (
  'MessageId' => 'XXX',
  'ReceiptHandle' => 'XXX',
  'MD5OfBody' => 'XXX',
  'Body' => '<Notification><NotificationMetaData><NotificationType>Test</NotificationType><PayloadVersion>1.0</PayloadVersion><UniqueId>XXX</UniqueId><PublishTime>2016-08-17T13:42:02Z</PublishTime></NotificationMetaData><NotificationPayload><TestNotification /></NotificationPayload></Notification>',
  'Attributes' => 
  array (
    'ApproximateReceiveCount' => '10',
  ),
)  

I believe this could be handled by adding XML detection to your modifyPayload function, something along these lines:

private function modifyPayload($payload, $class)
    {
        if (!is_array($payload)) $payload = json_decode($payload, true);

        function is_valid_xml ( $xml ) {
            libxml_use_internal_errors( true );

            $doc = new \DOMDocument('1.0', 'utf-8');

            $doc->loadXML( $xml );

            $errors = libxml_get_errors();

            return empty( $errors );
        }

        if(is_valid_xml($payload['Body'])) {
            $data = json_decode(json_encode(simplexml_load_string($payload['Body'], "SimpleXMLElement", LIBXML_NOCDATA)),true);
        } else {
            $data = json_decode($payload['Body']);
        }

        $body = [
          'job' => $class . '@handle',
          'data' => $data,
        ];

        $payload['Body'] = json_encode($body);

        return $payload;
    }

I just hacked this together so I'm sure there's a better way, but it appears to work properly. What do you think?

@TwassistantKeith
Copy link
Author

TwassistantKeith commented Aug 17, 2016

Actually, I get this error now:

[2016-08-17 10:39:36] production.ERROR: exception 'ErrorException' with message 'Argument 1 passed to Dusterio\PlainSqs\Integrations\LaravelServiceProvider::Dusterio\PlainSqs\Integrations\{closure}() must be an instance of Illuminate\Queue\Events\JobProcessed, string given' in /home/x/vendor/dusterio/laravel-plain-sqs/src/Integrations/LaravelServiceProvider.php:27

I believe this is a problem due to the different between Laravel 5.1 and 5.2. I am using 5.1 and so the Queue::after function looks different:

Queue::after(function ($connection, $job, $data) {

vs 5.2:

Queue::after(function (JobProcessed $event) {

I've changed my function to:

Queue::after(function ($connection, $job, $data) {
      $job->delete();
});

And everything is now processing correctly.

@dusterio
Copy link
Owner

Thanks for finding and pointing this out! Since 5.3 is coming out soon as well, I will need to add some version checks in the Laravel/Lumen service provider it seems – to support different versions. I will include this 5.1 fix as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants