Issues (3627)

LeadBundle/Segment/Stat/SegmentCampaignShare.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2019 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\LeadBundle\Segment\Stat;
13
14
use Doctrine\ORM\EntityManager;
15
use Mautic\CampaignBundle\Model\CampaignModel;
16
use Mautic\CoreBundle\Helper\CacheStorageHelper;
17
18
class SegmentCampaignShare
19
{
20
    /**
21
     * @var CampaignModel
22
     */
23
    private $campaignModel;
24
25
    /**
26
     * @var CacheStorageHelper
27
     */
28
    private $cacheStorageHelper;
29
30
    /**
31
     * @var EntityManager
32
     */
33
    private $entityManager;
34
35
    /**
36
     * SegmentCampaignShare constructor.
37
     */
38
    public function __construct(CampaignModel $campaignModel, CacheStorageHelper $cacheStorageHelper, EntityManager $entityManager)
39
    {
40
        $this->campaignModel      = $campaignModel;
41
        $this->cacheStorageHelper = $cacheStorageHelper;
42
        $this->entityManager      = $entityManager;
43
    }
44
45
    /**
46
     * @param int   $segmentId
47
     * @param array $campaignIds
48
     *
49
     * @return array
50
     */
51
    public function getCampaignsSegmentShare($segmentId, $campaignIds = [])
52
    {
53
        $campaigns = $this->campaignModel->getRepository()->getCampaignsSegmentShare($segmentId, $campaignIds);
54
        foreach ($campaigns as $campaign) {
55
            $this->cacheStorageHelper->set($this->getCachedKey($segmentId, $campaign['id']), $campaign['segmentCampaignShare']);
56
        }
57
58
        return $campaigns;
59
    }
60
61
    /**
62
     * @param int $segmentId
63
     *
64
     * @return array
65
     */
66
    public function getCampaignList($segmentId)
67
    {
68
        $q = $this->entityManager->getConnection()->createQueryBuilder();
69
        $q->select('c.id, c.name, null as share')
70
            ->from(MAUTIC_TABLE_PREFIX.'campaigns', 'c')
71
            ->where($this->campaignModel->getRepository()->getPublishedByDateExpression($q))
72
            ->orderBy('c.id', 'DESC');
73
74
        $campaigns = $q->execute()->fetchAll();
75
        foreach ($campaigns as &$campaign) {
76
            // just load from cache If exists
77
            if ($share  = $this->cacheStorageHelper->get($this->getCachedKey($segmentId, $campaign['id']))) {
0 ignored issues
show
Deprecated Code introduced by
The function Mautic\CoreBundle\Helper\CacheStorageHelper::get() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

77
            if ($share  = /** @scrutinizer ignore-deprecated */ $this->cacheStorageHelper->get($this->getCachedKey($segmentId, $campaign['id']))) {
Loading history...
78
                $campaign['share'] = $share;
79
            }
80
        }
81
82
        return $campaigns;
83
    }
84
85
    /**
86
     * @param int $segmentId
87
     * @param int $campaignId
88
     *
89
     * @return string
90
     */
91
    private function getCachedKey($segmentId, $campaignId)
92
    {
93
        return sprintf('%s|%s|%s|%s|%s', 'campaign', $campaignId, 'segment', $segmentId, 'share');
94
    }
95
}
96