Skip to content

Commit

Permalink
Refactor code to dedup.
Browse files Browse the repository at this point in the history
  • Loading branch information
shinde-rahul committed Jan 2, 2023
1 parent 729775d commit 4ebab80
Showing 1 changed file with 15 additions and 35 deletions.
50 changes: 15 additions & 35 deletions app/bundles/LeadBundle/Entity/LeadCategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,7 @@ public function getLeadCategories(Lead $lead)
*/
public function getSubscribedAndNewCategoryIds(Lead $lead, array $types): array
{
$qb = $this->_em->getConnection()->createQueryBuilder();

// Fetch the records from categories.
$parentQ = clone $qb;
$parentQ->select('c.id');
$parentQ->from(MAUTIC_TABLE_PREFIX.'categories', 'c');
$parentQ->where('c.is_published = 1');
$parentQ->andWhere($qb->expr()->in('c.bundle', ':bundles'));
$parentQ->setParameter('bundles', $types, Connection::PARAM_STR_ARRAY);

// Get the category ids for particular lead
$subQ = clone $qb;
$subQ->select('lc.category_id');
$subQ->from(MAUTIC_TABLE_PREFIX.'lead_categories', 'lc');
$subQ->where($qb->expr()->eq('lc.lead_id', ':leadId'));
$subQ->andWhere($qb->expr()->eq('lc.manually_removed', 1));
$subQ->setParameter('leadId', $lead->getId(), Types::INTEGER);

// Add sub-query
$parentQ->andWhere($qb->expr()->notIn('c.id', $subQ->getSQL()));

// Add sub-query parameter.
$parentQ->setParameter('leadId', $lead->getId(), Types::INTEGER);

$leadCategories = $parentQ->execute()
->fetchAllAssociative();

$leadCategoryList = [];
foreach ($leadCategories as $category) {
$id = (int) $category['id'];

$leadCategoryList[$id] = $id;
}

return $leadCategoryList;
return $this->getLeadCategoriesMapping($lead, $types, true);
}

/**
Expand All @@ -79,6 +45,16 @@ public function getSubscribedAndNewCategoryIds(Lead $lead, array $types): array
* @return array<int, int>
*/
public function getNonAssociatedCategoryIdsForAContact(Lead $lead, array $types): array
{
return $this->getLeadCategoriesMapping($lead, $types);
}

/**
* @param string[] $types
*
* @return array<int, int>
*/
private function getLeadCategoriesMapping(Lead $lead, array $types, bool $manuallyRemoved = false): array
{
$qb = $this->_em->getConnection()->createQueryBuilder();

Expand All @@ -97,6 +73,10 @@ public function getNonAssociatedCategoryIdsForAContact(Lead $lead, array $types)
$subQ->where($qb->expr()->eq('lc.lead_id', ':leadId'));
$subQ->setParameter('leadId', $lead->getId(), Types::INTEGER);

if ($manuallyRemoved) {
$subQ->andWhere($qb->expr()->eq('lc.manually_removed', 1));
}

// Add sub-query
$parentQ->andWhere($qb->expr()->notIn('c.id', $subQ->getSQL()));

Expand Down

0 comments on commit 4ebab80

Please sign in to comment.