![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/mautic.corals.io/app/bundles/PointBundle/Model/ |
<?php declare(strict_types=1); namespace Mautic\PointBundle\Model; use Mautic\CoreBundle\Model\FormModel as CommonFormModel; use Mautic\LeadBundle\Entity\Lead; use Mautic\PointBundle\Entity\Group; use Mautic\PointBundle\Entity\GroupContactScore; use Mautic\PointBundle\Entity\GroupRepository; use Mautic\PointBundle\Event as Events; use Mautic\PointBundle\Form\Type\GroupType; use Mautic\PointBundle\PointGroupEvents; use Symfony\Component\Form\FormFactory; use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; use Symfony\Contracts\EventDispatcher\Event; /** * @extends CommonFormModel<Group> */ class PointGroupModel extends CommonFormModel { public function getRepository(): GroupRepository { return $this->em->getRepository(Group::class); } public function getPermissionBase(): string { return 'point:groups'; } /** * @param object $entity * @param FormFactory $formFactory * @param string|null $action * @param array<string,string> $options * * @throws MethodNotAllowedHttpException */ public function createForm($entity, $formFactory, $action = null, $options = []): FormInterface { if (!$entity instanceof Group) { throw new MethodNotAllowedHttpException(['Group']); } if (!empty($action)) { $options['action'] = $action; } return $formFactory->create(GroupType::class, $entity, $options); } /** * Get a specific entity or generate a new one if id is empty. * * @param int $id */ public function getEntity($id = null): ?Group { if (null === $id) { return new Group(); } return parent::getEntity($id); } /** * @throws MethodNotAllowedHttpException */ protected function dispatchEvent($action, &$entity, $isNew = false, Event $event = null): ?Event { if (!$entity instanceof Group) { throw new MethodNotAllowedHttpException(['Group']); } switch ($action) { case 'pre_save': $name = PointGroupEvents::GROUP_PRE_SAVE; break; case 'post_save': $name = PointGroupEvents::GROUP_POST_SAVE; break; case 'pre_delete': $name = PointGroupEvents::GROUP_PRE_DELETE; break; case 'post_delete': $name = PointGroupEvents::GROUP_POST_DELETE; break; default: return null; } if ($this->dispatcher->hasListeners($name)) { if (empty($event)) { $event = new Events\GroupEvent($entity); } $this->dispatcher->dispatch($event, $name); return $event; } return null; } public function adjustPoints(Lead $contact, Group $group, int $points, string $operator = Lead::POINTS_ADD): Lead { $contactScore = $contact->getGroupScore($group); if (empty($contactScore)) { $contactScore = new GroupContactScore(); $contactScore->setContact($contact); $contactScore->setGroup($group); $contactScore->setScore(0); $contact->addGroupScore($contactScore); } $oldScore = $contactScore->getScore(); $newScore = $oldScore; match ($operator) { Lead::POINTS_ADD => $newScore += $points, Lead::POINTS_SUBTRACT => $newScore -= $points, Lead::POINTS_MULTIPLY => $newScore *= $points, Lead::POINTS_DIVIDE => $newScore /= $points, Lead::POINTS_SET => $newScore = $points, default => throw new \UnexpectedValueException('Invalid operator'), }; $contactScore->setScore($newScore); $this->em->persist($contactScore); $this->em->flush(); $scoreChangeEvent = new Events\GroupScoreChangeEvent($contactScore, $oldScore, $newScore); $this->dispatcher->dispatch($scoreChangeEvent, PointGroupEvents::SCORE_CHANGE); return $contact; } public static function isAllowedPointOperation(string $operator): bool { return in_array($operator, [Lead::POINTS_ADD, Lead::POINTS_SUBTRACT, Lead::POINTS_MULTIPLY, Lead::POINTS_DIVIDE, Lead::POINTS_SET]); } }