MetaSubscriptions::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 11
rs 9.4285
c 1
b 1
f 0
1
<?php
2
3
/**
4
 * @author Rafał Muszyński <[email protected]>
5
 * @author Paweł Mikołajczuk <[email protected]>
6
 * @copyright 2014 Sourcefabric o.p.s.
7
 * @license http://www.gnu.org/licenses/gpl-3.0.txt
8
 */
9
namespace Newscoop\PaywallBundle\Meta;
10
11
/**
12
 * Meta subscriptions class.
13
 */
14
class MetaSubscriptions
15
{
16
    protected $subscriptions;
17
18
    public function __construct()
19
    {
20
        $this->subscriptions = $this->getSubscriptions(
21
            \CampTemplate::singleton()->context()->publication->identifier,
22
            \CampTemplate::singleton()->context()->user->identifier
23
        );
24
25
        if (count($this->subscriptions) == 0) {
26
            $this->subscriptions = array();
27
        }
28
    }
29
30
    /**
31
     * Get all subscriptions by given publication id and user id.
32
     *
33
     * @param int $publicationId Publication id
34
     * @param int $userId        User id
35
     *
36
     * @return array Returns array of subscriptions' ids
37
     */
38
    protected function getSubscriptions($publicationId, $userId)
39
    {
40
        $em = \Zend_Registry::get('container')->getService('em');
41
42
        $subscriptions = $em->getRepository("Newscoop\PaywallBundle\Entity\UserSubscription")
43
            ->createQueryBuilder('s')
44
            ->select('s.id')
45
            ->where('s.publication = :publicationId')
46
            ->andWhere('s.user = :userId')
47
            ->setParameters(array(
48
                'publicationId' => $publicationId,
49
                'userId' => $userId,
50
            ))
51
            ->orderBy('s.created_at', 'asc')
52
            ->getQuery()
53
            ->getArrayResult();
54
55
        return $subscriptions;
56
    }
57
}
58