Spaces:
No application file
No application file
File size: 1,989 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 |
<?php
namespace Mautic\LeadBundle\Segment\Stat;
use Doctrine\ORM\EntityManager;
use Mautic\CampaignBundle\Model\CampaignModel;
use Mautic\CoreBundle\Helper\CacheStorageHelper;
class SegmentCampaignShare
{
public function __construct(
private CampaignModel $campaignModel,
private CacheStorageHelper $cacheStorageHelper,
private EntityManager $entityManager
) {
}
/**
* @param int $segmentId
* @param array $campaignIds
*
* @return array
*/
public function getCampaignsSegmentShare($segmentId, $campaignIds = [])
{
$campaigns = $this->campaignModel->getRepository()->getCampaignsSegmentShare($segmentId, $campaignIds);
foreach ($campaigns as $campaign) {
$this->cacheStorageHelper->set($this->getCachedKey($segmentId, $campaign['id']), $campaign['segmentCampaignShare']);
}
return $campaigns;
}
/**
* @param int $segmentId
*
* @return array
*/
public function getCampaignList($segmentId)
{
$q = $this->entityManager->getConnection()->createQueryBuilder();
$q->select('c.id, c.name, null as share')
->from(MAUTIC_TABLE_PREFIX.'campaigns', 'c')
->where($this->campaignModel->getRepository()->getPublishedByDateExpression($q))
->orderBy('c.id', 'DESC');
$campaigns = $q->executeQuery()->fetchAllAssociative();
foreach ($campaigns as &$campaign) {
// just load from cache If exists
if ($share = $this->cacheStorageHelper->get($this->getCachedKey($segmentId, $campaign['id']))) {
$campaign['share'] = $share;
}
}
return $campaigns;
}
/**
* @param int $segmentId
* @param int $campaignId
*/
private function getCachedKey($segmentId, $campaignId): string
{
return sprintf('%s|%s|%s|%s|%s', 'campaign', $campaignId, 'segment', $segmentId, 'share');
}
}
|