Spaces:
No application file
No application file
File size: 4,489 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
<?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]);
}
}
|