tourze / campaign-bundle
Symfony bundle for managing campaigns, awards, and user rewards system
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/campaign-bundle
Requires
- php: ^8.1
- ext-date: *
- ext-spl: *
- doctrine/collections: ^2.3
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/orm: ^3.0
- doctrine/persistence: ^4.1
- easycorp/easyadmin-bundle: ^4
- psr/log: ^3|^2|^1
- symfony/config: ^7.3
- symfony/console: ^7.3
- symfony/dependency-injection: ^7.3
- symfony/event-dispatcher-contracts: ^3
- symfony/expression-language: ^7.3
- symfony/framework-bundle: ^7.3
- symfony/http-kernel: ^7.3
- symfony/security-bundle: ^7.3
- symfony/security-core: ^7.3
- tourze/arrayable: 1.*
- tourze/bundle-dependency: 1.*
- tourze/doctrine-indexed-bundle: 1.0.*
- tourze/doctrine-resolve-target-entity-bundle: 1.*
- tourze/doctrine-snowflake-bundle: 1.1.*
- tourze/doctrine-timestamp-bundle: 1.1.*
- tourze/doctrine-track-bundle: 1.1.*
- tourze/easy-admin-menu-bundle: 1.0.*
- tourze/json-rpc-cache-bundle: 1.0.*
- tourze/json-rpc-core: 1.0.*
- tourze/json-rpc-lock-bundle: 1.0.*
- tourze/json-rpc-log-bundle: 1.0.*
- tourze/json-rpc-paginator-bundle: 1.0.*
- tourze/symfony-aop-doctrine-bundle: 1.0.*
- tourze/symfony-cron-job-bundle: 1.1.*
- tourze/symfony-ecol-bundle: 1.0.*
- tourze/text-manage-bundle: 1.0.*
- tourze/user-tag-contracts: 1.0.*
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.5
- symfony/phpunit-bridge: ^7.3
- tourze/phpunit-base: 1.*
- tourze/phpunit-doctrine-entity: 1.*
- tourze/phpunit-enum: 1.*
- tourze/phpunit-symfony-kernel-test: 1.0.*
- tourze/phpunit-symfony-unit-test: 1.*
- tourze/phpunit-symfony-web-test: 1.*
- tourze/symfony-dependency-service-loader: 1.0.*
Suggests
- tourze/campaign-coupon-bundle: Add coupon reward support to campaigns
- tourze/campaign-credit-bundle: Add credit/points reward support to campaigns
- tourze/campaign-product-bundle: Add SKU/SPU qualification reward support to campaigns
This package is auto-updated.
Last update: 2025-11-18 08:31:58 UTC
README
[]
(https://packagist.org/packages/tourze/campaign-bundle)
[
]
(https://packagist.org/packages/tourze/campaign-bundle)
[
]
(https://packagist.org/packages/tourze/campaign-bundle)
[
]
(https://github.com/tourze/php-monorepo/actions)
[
]
(https://coveralls.io/github/tourze/php-monorepo?branch=master)
A comprehensive campaign management bundle for Symfony applications, supporting various marketing campaign types including lottery events, coupon distribution, and credit rewards with flexible participation rules and automated lifecycle management.
Table of Contents
- Features
- Installation
- Configuration
- Commands
- Usage
- Core Components
- Dependencies
- Advanced Usage
- Contributing
- License
Features
- Comprehensive Campaign Management: Support for various marketing campaigns with configurable participation rules
- Flexible Reward System: Configurable rewards including coupons, credits, SKU/SPU purchase qualifications
- Smart Limitation System: Daily, weekly, monthly, quarterly, yearly, and total limits for reward distribution
- User Participation Tracking: Complete event logging and user interaction tracking with context data
- Automated Lifecycle Management: Built-in commands for campaign and user chance expiration
- EasyAdmin Integration: Ready-to-use admin interface for campaign management
- JSON-RPC API: 7 comprehensive API endpoints for campaign operations
- Expression Language Support: Configurable participation conditions using Symfony Expression Language
- Extensible Architecture: Easy to extend with custom reward types and limitation rules
Installation
composer require tourze/campaign-bundle
Configuration
Add the bundle to your bundles.php:
return [ // ... other bundles CampaignBundle\CampaignBundle::class => ['all' => true], ];
Commands
The bundle provides several console commands for campaign management:
campaign:chance-expire
Automatically processes expired user chances/opportunities in campaigns.
php bin/console campaign:chance-expire
This command runs every minute via cron job to:
- Find expired user chances
- Mark them as invalid
- Update expiration remarks
campaign:check-expired-campaign
Automatically disables campaigns that have passed their end time.
php bin/console campaign:check-expired-campaign
This command runs every minute via cron job to:
- Find campaigns past their end time
- Mark them as invalid
- Ensure proper campaign lifecycle management
Usage
Basic Campaign Creation
use CampaignBundle\Entity\Campaign; use CampaignBundle\Entity\Award; use CampaignBundle\Enum\AwardType; use CampaignBundle\Enum\AwardLimitType; // Create campaign $campaign = new Campaign(); $campaign->setCode('SUMMER2024'); $campaign->setName('Summer Sale'); $campaign->setStartTime(new \DateTime('2024-07-01')); $campaign->setEndTime(new \DateTime('2024-07-31')); $campaign->setValid(true); // Add coupon reward $award = new Award(); $award->setCampaign($campaign); $award->setEvent('join'); $award->setType(AwardType::COUPON); $award->setValue('COUPON_CODE_001'); $award->setPrizeQuantity(1000); $award->setAwardLimitType(AwardLimitType::BUY_TOTAL); $award->setTimes(1);
Setting Participation Conditions
// Configure participation expression $campaign->setRequestExpression(' user.getCreatedAt() < date("-30 days") and user.hasTag("VIP") ');
JSON-RPC API
The bundle provides 7 JSON-RPC procedures for API integration:
RequestCampaignChance: Request participation opportunity in a campaignConsumeCampaignChance: Consume a campaign opportunity and receive rewardsGetCampaignConfig: Retrieve complete campaign configurationGetCampaignCategoryList: Get campaign categories with paginationGetCampaignRewards: Retrieve user's rewards from specific campaignGetCampaignEventLogs: Retrieve campaign event logs with filteringReportCampaignEventLog: Report user events in campaign
Example API Usage
// Request participation chance $response = $jsonRpcClient->call('RequestCampaignChance', [ 'campaignCode' => 'SUMMER2024' ]); // Consume chance and get reward $response = $jsonRpcClient->call('ConsumeCampaignChance', [ 'chanceId' => $chanceId, 'event' => 'join' ]);
Core Components
Entities
- Campaign: Main campaign entity with status management and time-based lifecycle
- Award: Reward configurations with quantity and limitation controls
- Chance: User participation opportunities with expiration management
- Reward: User reward records with unique serial numbers
- EventLog: Comprehensive event tracking with arbitrary data support
- Limit: Flexible limitation rules for reward distribution
- Category: Hierarchical campaign categorization
- Attribute: Custom key-value properties for campaigns
Reward Types
- Coupons: Both local and external coupon support
- Credits: Configurable credit point rewards
- Purchase Qualifications: SKU/SPU purchase rights
- Custom Rewards: Extensible reward system
Limitation Types
- Daily/Weekly/Monthly/Quarterly/Yearly: Time-based limitations
- Total Limit: Overall quantity restrictions
- User Tag Based: User group specific limitations
- Chance Based: Participation opportunity limitations
Dependencies
This bundle requires:
- PHP: 8.1 or higher
- Symfony: 6.4 or higher
- Doctrine ORM: 3.0 or higher
- EasyAdmin: 4.0 or higher
Required Symfony Bundles
doctrine/doctrine-bundleeasycorp/easyadmin-bundlesymfony/security-bundlesymfony/framework-bundle
Optional Dependencies
tourze/coupon-core-bundle: For coupon reward supporttourze/credit-bundle: For credit point rewardstourze/product-core-bundle: For SKU/SPU purchase qualificationstourze/order-core-bundle: For order-related campaign features
Advanced Usage
Custom Reward Types
Extend the reward system with custom reward types:
use CampaignBundle\Enum\AwardType; use CampaignBundle\Service\CampaignService; // Create custom reward handler class CustomRewardHandler { public function handleCustomReward(Award $award, UserInterface $user): Reward { // Custom reward logic here $reward = new Reward(); $reward->setType(AwardType::CUSTOM); $reward->setValue($award->getValue()); $reward->setUser($user); return $reward; } }
Expression Language Functions
Use built-in expression language functions for complex participation conditions:
// Available functions: // - hasChance(user, campaign): Check if user has valid chance // - getChanceCount(user, campaign): Get user's remaining chances // - hasTag(user, tagName): Check if user has specific tag // - getTagValue(user, tagName): Get user tag value $campaign->setRequestExpression(' hasChance(user, campaign) and user.getCreatedAt() < date("-30 days") and hasTag(user, "VIP") and getTagValue(user, "level") >= 5 ');
Event System Integration
Integrate with Symfony's event system for advanced workflows:
use CampaignBundle\Event\UserEventReportEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class CampaignEventSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents(): array { return [ UserEventReportEvent::class => 'onUserEventReport', ]; } public function onUserEventReport(UserEventReportEvent $event): void { // Custom event processing logic $this->logger->info('User event reported', [ 'user_id' => $event->getUser()->getId(), 'event' => $event->getEvent(), 'data' => $event->getData(), ]); } }
Database Optimization
For high-traffic campaigns, consider these optimizations:
# Use dedicated database connections for campaign operations # Configure in doctrine.yaml: doctrine: dbal: connections: campaign: url: '%env(resolve:DATABASE_CAMPAIGN_URL)%' default: url: '%env(resolve:DATABASE_URL)%'
// Use database-level locks for concurrent reward distribution use Tourze\Symfony\AopDoctrineBundle\Attribute\Transactional; class CampaignService { #[Transactional] public function distributeRewardWithLock(Award $award, UserInterface $user): Reward { // Atomic reward distribution with database locks return $this->rewardUser($user, $award); } }
Performance Monitoring
Monitor campaign performance with built-in metrics:
// Track campaign metrics $metrics = [ 'total_participants' => $this->getParticipantCount($campaign), 'total_rewards_distributed' => $this->getRewardCount($campaign), 'conversion_rate' => $this->getConversionRate($campaign), 'average_participation_time' => $this->getAverageParticipationTime($campaign), ];
Contributing
Please see CONTRIBUTING.md for details.
License
The MIT License (MIT). Please see License File for more information.