承接 condoedge/communications 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

condoedge/communications

Composer 安装命令:

composer require condoedge/communications

包简介

A communications package for kompo/kompo

README 文档

README

Concept of Working

This package provides a way to handle communications within your application. It allows you to set up triggers, manage variables, and enhance context for events. The main idea is to have a way to send communications to users based on events that happen in the application. For example, you could send an email to a user when they register, or when they buy a product.

It provides a route /communication-templates where you can edit/create the templates for the communications with variables and types of communications.

Steps to Use It

Installation

To install the package, run the following command:

composer require condoedge/communications

Publishing configuration

To publish the configuration file, use the following command:

php artisan vendor:publish --tag="condoedge-communications-config"

Publishing javascript (Needed for the CKeditor)

To publish the JavaScript files required for CKEditor, use the following command:

php artisan vendor:publish --tag="condoedge-communications-js"

Setting communicables

Each kind of communications has their own interface to be implemented by the communicable model. For example, the EmailCommunicable model implements the EmailCommunicable interface. So if the communication is an email, the communicable model should implement the EmailCommunicable interface or it'll be ignored.

Our actual interfaces are:

  • EmailCommunicable
  • SMSCommunicable
  • DatabaseCommunicable (Notifications)

Setting Up Triggers

Listener flow

To set up a trigger, you need to create an event that will trigger the communication. Below is an example of how to create a trigger event:

<?php

class RandomTriggerer implements CommunicableEvent
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    protected $event;
    protected $teamsIds;

    public function __construct($event)
    {
        $this->event = $event;
    }

    function getParams(): array
    {
        return [
            'event' => $this->event,
        ];
    }

    // If you don't set communicables, the communication won't be sent
    function getCommunicables(): array|Collection
    {
        return CommunicableRandom::where('event_id', $this->event->id)->get();
    }

    static function getName(): string
    {
        return __('communications.a-random-triggerer');
    }
}

Next, set the communication triggers in the kompo-communications config file:

<?php

return [
    'triggers' => [
        OpenedReinscriptions::class,
    ],
];

Introducing CKEditor

Enhanced editor

To implement CKEditor, use the _EnhancedEditor helper in your kompo component. In communications, it is used in the EmailCommunicationHandler to allow the user to write the email content with variables.

Using Variables in CKEditor

To see variables in the editor, set the variables as follows:

// AppServiceProvider.php
Variables::setVariables([
    'events' => [
        // ID, name, classes, automatic handling (access to the object and attribute)
        ['event.name_ev', 'Event name', 'bg-level1', true]
    ],
    'teams' => [
        ['team_name', 'Team name', 'bg-level1', false]
    ]
]);

Replacing Content with Variables

The content will replace the variables in the text with the provided values. You can also use the automatic handling of the variables ({model.attribute}) for simple cases.

// AppServiceProvider.php
ContentReplacer::setHandlers([
    'team_name' => function ($team) {
        return $team->name;
    }
]);

Enhancing Context for Events

You can enhance the context that you'll provide in the events. You can also implement the enhanceContext method in the model to do this, which is often a better approach.

ContextEnhancer::setEnhancers([
    'event' => function ($event) {
        return [
            'team' => $event->team,
            'teams_ids' => [$event->team_id],
        ];
    }
]);

or

// RandomModel.php
public function enhanceContext()
{
    return [
        'team' => $this->team,
        'teams_ids' => [$this->team_id],
    ];
}

Setting default communications for triggers

To set the default communications for each trigger, you can use the provided button on the list page. You need to configure the default communications for each trigger in the resources\views\stubs\communication-templates directory. The file structure should follow this format:

default-{slugged-name-of-the-trigger}-{number-of-type-of-communications}-{locale-key}.blade.php

For example: default-opened-reinscriptions-1-en.blade.php

The name of the trigger is given by $trigger::class.

Here's a breakdown of the numbering system for each type of communication:

  • Email: 1
  • SMS: 2
  • Notification: 3

Make sure to update the appropriate file for each trigger and communication type.

Sending communications ways

Email

We use the Laravel Mail facade to send emails. You should change the file communication-layout.blade.php to your email layout.

SMS

We use the vonage API to send SMS.

Database

We use the Laravel Notification system to send notifications with some enhancements to manage them. Giving the possibility to provide some custom notifications handlers.

Extending sending ways

We don't have an easy way to do this. We'll need a refactor for do CommunicationType enum setteable from a config file. Now it's static.

We have an specific handler for each type of communications. Implementing a different way to send communications, and save them. You should create a new handler and extends the AbstractCommunicationHandler class.

class EmailCommunicationHandler extends AbstractCommunicationHandler
{
    public function communicableInterface()
    {
        return EmailCommunicable::class;
    }

    // NOTIFICATION

    /**
     * @param EmailCommunicable[] $communicables
     * @param mixed $params
     * @return void
     */
    public function notifyCommunicables(array $communicables, $params = [])
    {
        $layout = $params['layout'] ?? DefaultLayoutEmailCommunicable::class;
        
        $communicables = collect($communicables)->map(function($communicable) use ($layout, $params) {
            $params = ContextEnhancer::setCommunicable($communicable)->getEnhancedContext($params);

            Mail::to($communicable->getEmail())->send(new $layout($this->communication, $params));
        });
    }   
}

condoedge/communications 适用场景与选型建议

condoedge/communications 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.59k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 10 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 condoedge/communications 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 condoedge/communications 我们能提供哪些服务?
定制开发 / 二次开发

基于 condoedge/communications 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.59k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 10
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-18