File size: 2,512 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
<?php

namespace MauticPlugin\MauticSocialBundle\Model;

use Mautic\CoreBundle\Model\AbstractCommonModel;
use MauticPlugin\MauticSocialBundle\Entity\PostCount;

/**
 * @extends AbstractCommonModel<PostCount>
 */
class PostCountModel extends AbstractCommonModel
{
    /**
     * Get a specific entity or generate a new one if id is empty.
     */
    public function getEntity($id = null): ?PostCount
    {
        if (null !== $id) {
            $repo = $this->getRepository();
            if (method_exists($repo, 'getEntity')) {
                return $repo->getEntity($id);
            }

            return $repo->find($id);
        }

        return new PostCount();
    }

    /**
     * Get this model's repository.
     *
     * @return \MauticPlugin\MauticSocialBundle\Entity\PostCountRepository
     */
    public function getRepository()
    {
        return $this->em->getRepository(PostCount::class);
    }

    /*
     * Updates a monitor record's post count on a daily basis
     *
     * @return boolean
     */
    public function updatePostCount($monitor, \DateTime $postDate): bool
    {
        // query the db for posts on this date
        $q    = $this->getRepository()->createQueryBuilder($this->getRepository()->getTableAlias());
        $expr = $q->expr()->eq($this->getRepository()->getTableAlias().'.postDate', ':date');

        $q->setParameter('date', $postDate, 'date');
        $q->where($expr);
        $args['qb'] = $q;

        // ignore paginator so we can use the array later
        $args['ignore_paginator'] = true;

        /** @var \MauticPlugin\MauticSocialBundle\Entity\PostCountRepository $postCountsRepository */
        $postCountsRepository = $this->getRepository();

        // get any existing records
        $postCounts = $postCountsRepository->getEntities($args);

        // if there isn't anything then create it
        if (!count($postCounts)) {
            /** @var PostCount $postCount */
            $postCount = $this->getEntity();
            $postCount->setMonitor($monitor);
            $postCount->setPostDate($postDate); // $postDate->format('m-d-Y')
        } else {
            // use the first record to increment it.
            $postCount = $this->getEntity($postCounts[0]->getId());
        }

        // increment
        $postCount->setPostCount($postCount->getPostCount() + 1);

        // now save it
        $postCountsRepository->saveEntity($postCount);

        // nothing went wrong so return true here
        return true;
    }
}