| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- <?php
- declare(strict_types=1);
- namespace app\miniapi\service;
- use app\miniapi\http\Fields;
- use plugin\saiadmin\app\service\promotion\PromotionReferrals;
- final class PromotionService
- {
- public function __construct(private readonly Access $access)
- {
- }
- public function code(int $enterprise, array $input): array
- {
- return $this->command($enterprise, $input, 'code');
- }
- public function bind(int $enterprise, array $input): array
- {
- return $this->command($enterprise, $input, 'bind');
- }
- private function command(int $enterprise, array $input, string $action): array
- {
- $this->access->enterprise($enterprise, true);
- Fields::only($input, $action === 'code' ? ['request_key'] : ['request_key', 'invite_code']);
- $key = Fields::text($input, 'request_key', 80);
- $code = $action === 'bind' ? strtoupper(Fields::text($input, 'invite_code', 24)) : '';
- return Commands::run($this->access->actor, $key, 'promotion.' . $action . ':' . $enterprise, $input, function () use ($enterprise, $action, $code) {
- $this->access->enterprise($enterprise, true);
- $service = new PromotionReferrals();
- return $action === 'code' ? $service->code($enterprise)
- : $service->bind($enterprise, $code, (int) $this->access->actor->userId);
- }, fn () => $this->access->enterprise($enterprise, true));
- }
- }
|