comsave/salesforce-outbound-message-bundle

此包允许您轻松处理Salesforce发送的外部消息。


README

通过Salesforce的外部消息在Symfony中创建、更新、删除对象。

Release Travis Test Coverage Packagist

需求

此包假设您正在使用

  1. MongoDB数据库(特别是 doctrine/mongodb-odm)。
  2. comsave/salesforce-mapper-bundle 用于Salesforce对象映射到您的MongoDB Document类。

包功能

  • 对象 create
  • 对象 update
  • 对象 delete。要启用此功能,请完成 附加设置步骤
  • 对象自定义处理 beforeFlush
  • 对象自定义处理 afterFlush

安装

  • composer require comsave/salesforce-outbound-message-bundle
  • 在您的 AppKernel.php 中注册此包,通过添加 new Comsave\SalesforceOutboundMessageBundle\ComsaveSalesforceOutboundMessageBundle()
  • 为了处理Salesforce的传入外部消息,创建一个路由(例如 /sync)和一个控制器方法
<?php

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());
        }
    }
}
  • 在您的 app/config/config.yml 中添加包配置
comsave_salesforce_outbound_message:
    # WSDL_CACHE_NONE, WSDL_CACHE_DISK, WSDL_CACHE_MEMORY or WSDL_CACHE_BOTH
    wsdl_cache: 'WSDL_CACHE_DISK'                     
    # An absolute path to Salesforce object WSDL files
    wsdl_directory: '/absolute/path/' 
    document_paths:
        # Map a document using its Salesforce name and your local class 
        CustomObject__c:              
            path: 'YourNamespace\Documents\CustomObject'
            force_compare: false # if true, incoming object will be compared to existing ones in the database; will continue sync only if not equal
  • DocumentInterface 添加到您希望由 OutboundMessageBundle 跟踪的文档类中。
<?php

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

class Account extends BaseAccount implements DocumentInterface
{
}
  • 为要同步的对象创建一个 EventSubscriber。对于 Account 对象,它可能看起来像这样
<?php

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.
         */
    }
}
  • 将您新建的路由添加到要同步的对象的Salesforce外部消息中(在我们的例子中是 Account)。
  • 就是这样!触发一个外部消息发送,并看到一切自动发生。😎 👍

许可

本项目受MIT许可协议许可。