myvon/devicechanger 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

myvon/devicechanger

Composer 安装命令:

composer require myvon/devicechanger

包简介

This package allow to send a link through any method and check if the user has sent the data needed.

README 文档

README

You want your user to send you data through another device (for example a phone) without leaving your page ? This small library help you to !

How it works

This package allow to send a link through any method and check if the user has sent the data needed.

Installation

You can install the package via composer:

composer require myvon/devicechanger

Creating the file your user is sent to

You first need to create the file where your user will be redirected to send the required data.

You can check the examples/upload.php file to see an example of a file where the user is invited tu upload a file

Choosing an url generator

The second step is to choose a way to generate the url to the file. Two generator are provided by default:

  • the Myvon\DeviceChanger\DummyGenerator which simply return the url with the correct query parameters
  • the Myvon\DeviceChanger\BitlyUrlGenerator which uses the Bitly service to generate a tiny url and reduce message size (usefull for sms)

For the dummy generator, simply provide the public url to the file :

use Myvon\DeviceChanger\DummyGenerator;


$generator = new \Myvon\DeviceChanger\DummyGenerator("http://localhost/upload.php");

For the bitly generator, you'll need to provide the public url and your bitly access token :

use Myvon\DeviceChanger\DummyGenerator;


$generator = new \Myvon\DeviceChanger\BitlyUrlGenerator("http://localhost/upload.php", "myBitlyAccessToken");

Creating your own generator

You can create your own generator by implementing the Myvon\DeviceChanger\UrlGeneratorInterface interface. You'll simply need to implement the public function getUrl(string $uid) method which return the generated url, or false if the generation failed.

Choosing a channel to send the link

After choosing the generator, you will need to choose through which channel you send the link. Two senders are provided by default:

  • The Myvon\DeviceChanger\DummyGenerator which will simply save the message and the recipient and always success. He is mainly used to test the library
  • The Myvon\DeviceChanger\OvhSmsGenerator which send the link by sms using OVH. Be advised that the phone number must start with the country code (+33 for france)

To use the OvhSmsGenerator you'll to provide :

  • An Ovh\Api instance (which is provided by the official PHP OVH Api available at https://github.com/ovh/php-ovh)
  • The sender username you configured in the OVH manager
  • The sms service name (starting by sms-) provided by OVH.
$sender = new \Myvon\DeviceChanger\OvhSmsSender($ovhApi, "MY COMPANY", "sms-xxxxxx-1");

Creating a custom channel

You can create a custom channel by implementing the Myvon\DeviceChanger\SenderInterface interface. You'll need to implement the two following methods:

  • public function isValid(string $recipient): bool; which check if the recipient is valid (for example if it is a valid email if you send by mail or a valid phone number for sms channel)
  • public function send(string $recipient, string $message): bool; which send the $message to the $recipient

Specify the storage

The last step to use the library is to specify how the data are stored and how to check to they have been received.

For now the only storage provided by default is the Myvon\DeviceChanger\FileStorage which use the filesystem to store the data (mainly used for upload).

You'll simply need to provide the directory where the data will be stored:

$storage = new \Myvon\DeviceChanger\FileStorage('./upload');

Adding custom storage

If you need to create a custom storage you can do it by implementing the Myvon\DeviceChanger\StorageInterface interface. You'll need to implement the two following methods:

  • public function isReceived(string $uid): bool; which check if the data are received
  • public function fetch(string $uid); which return the received data (or a way to access them)

Sending the link

You can finally use the Myvon\DeviceChanger\DeviceChanger class to send the link and check if the data are received.

You will need to provide :

  • A class that implements the Myvon\DeviceChanger\UrlGeneratorInterface interface
  • A class that implements the Myvon\DeviceChanger\SenderInterface interface
  • A class that implements the Myvon\DeviceChanger\StorageInterface interface
  • The message sent to the user

The message can be personalized with parameters (see the send method bellow) and must contain "%url%" which will be replaced by the generated url.

$deviceChanger = new \Myvon\DeviceChanger\DeviceChanger(
    $generator,
    $sender,
    $storage,
    "Pour continuer, veuillez vous rendre à l'adresse %url% (envoyé à %phone%)"
);

The parameters must follow this pattern : "%myparameter%"

You can then send the message:

if(($uid = $deviceChanger->send("+33600000000"))) {
    // Success
}

You can provide your parameters as second argument to the method :

$uid = $deviceChanger->send("+33600000000", [
'myparameter' => 'my value',
]); // %myparameter% will be replace by 'my value'

Check if the data are received

Now the link has been sent, you can check if your user has submitted the data :

if($deviceChanger->check($uid)) {
    // Data have been received
}

To retrieve the data :

$data  = $devicechanger->fetch($uid); // Return false if data not received

Note that the Myvon\DeviceChanger\FileStorage class will return the full path to the directory where the file(s) have been received.

Examples

You can check the following examples:

  • examples/check.php which check if the data are received for a given UID then return the data if they have been received or KO otherwise.
  • examples/example.php which contain a full example in one page using the DummyGenerator and the DummySender (can be run in cli)
  • examples/upload.php which is an example of the file the user is redirected to. It contain a simple upload form allowing the user to upload a file

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see License File for more information.

myvon/devicechanger 适用场景与选型建议

myvon/devicechanger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 06 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-06-09