PHP code example of comsave / salesforce-outbound-message-bundle

1. Go to this page and download the library: Download comsave/salesforce-outbound-message-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

comsave / salesforce-outbound-message-bundle example snippets




use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Comsave\SalesforceOutboundMessageBundle\Services\RequestHandler\OutboundMessageRequestHandler;

class OutboundMessageController extends Controller
{
    public function syncAction(Request $request, OutboundMessageRequestHandler $requestHandler)
    {
        try {
            $outboundMessageXml = $request->getContent();
            return $requestHandler->handle($outboundMessageXml);
        }
        catch (\Throwable $e) {
            throw new \SoapFault("Server", $e->getMessage());
        }
    }
}



use Comsave\SalesforceOutboundMessageBundle\Interfaces\DocumentInterface;
use LogicItLab\Salesforce\MapperBundle\Model\Account as BaseAccount;

class Account extends BaseAccount implements DocumentInterface
{
}



namespace YourNamespace\EventSubscriber;

use Comsave\SalesforceOutboundMessageBundle\Event\OutboundMessageBeforeFlushEvent;
use Comsave\SalesforceOutboundMessageBundle\Event\OutboundMessageAfterFlushEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Comsave\SalesforceOutboundMessageBundle\Interfaces\DocumentInterface; 
use Comsave\Webservice\Core\UserBundle\Document\Account;

class AccountSoapRequestSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            OutboundMessageBeforeFlushEvent::NAME => [
                ['onBeforeFlush'],
            ],
            OutboundMessageAfterFlushEvent::NAME => [
                ['onAfterFlush'],
            ],
        ];
    }

    public function supports(DocumentInterface $document): bool
    {
        $documentClass = get_class($document);

        return Account::class == $documentClass || $document instanceof Account;
    }

    public function onBeforeFlush(OutboundMessageBeforeFlushEvent $event)
    {
        /**
         * Make sure to do call $this->supports() before you start processing the object
         * You only want to process the correct object in this EventSubscriber (which is Account in this case)
         */
        /** @var Account $newAccount */
        $newAccount = $event->getNewDocument();
        
        if (!$this->supports($newAccount)) return; 
    
        /** @var Account $existingAccount */
        $existingAccount = $event->getExistingDocument();
        
        /**
         * You can do any modifications you want to the object before it get's saved (flushed) to the database.
         * - - -
         * $event->getExistingDocument() provides you access to the existing object (if it exists) 
         * $event->getNewDocument() provides you access to the new object delivered by the outbound message. This is the object that will be merged over the existing one (if any) and saved to the database. In most of the cases you only need to use this one.
         */
    }

    public function onAfterFlush(OutboundMessageAfterFlushEvent $event)
    {
        /** @var Account $account */
        $account = $event->getDocument();

        if (!$this->supports($account)) return; 

        /**
         * You can process the object further if necessary after it has been saved (flushed) to the database.
         */
    }
}