PromotionService.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\miniapi\service;
  4. use app\miniapi\http\Fields;
  5. use plugin\saiadmin\app\service\promotion\PromotionReferrals;
  6. final class PromotionService
  7. {
  8. public function __construct(private readonly Access $access)
  9. {
  10. }
  11. public function code(int $enterprise, array $input): array
  12. {
  13. return $this->command($enterprise, $input, 'code');
  14. }
  15. public function bind(int $enterprise, array $input): array
  16. {
  17. return $this->command($enterprise, $input, 'bind');
  18. }
  19. private function command(int $enterprise, array $input, string $action): array
  20. {
  21. $this->access->enterprise($enterprise, true);
  22. Fields::only($input, $action === 'code' ? ['request_key'] : ['request_key', 'invite_code']);
  23. $key = Fields::text($input, 'request_key', 80);
  24. $code = $action === 'bind' ? strtoupper(Fields::text($input, 'invite_code', 24)) : '';
  25. return Commands::run($this->access->actor, $key, 'promotion.' . $action . ':' . $enterprise, $input, function () use ($enterprise, $action, $code) {
  26. $this->access->enterprise($enterprise, true);
  27. $service = new PromotionReferrals();
  28. return $action === 'code' ? $service->code($enterprise)
  29. : $service->bind($enterprise, $code, (int) $this->access->actor->userId);
  30. }, fn () => $this->access->enterprise($enterprise, true));
  31. }
  32. }