MetaSubscription::__construct()   B
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 22
nc 7
nop 1
dl 0
loc 30
rs 8.439
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.p
7
 * @license http://www.gnu.org/licenses/gpl-3.0.txt
8
 */
9
10
namespace Newscoop\PaywallBundle\Meta;
11
12
use Newscoop\PaywallBundle\Entity\UserSubscription;
13
14
/**
15
 * Meta subscriptions class.
16
 */
17
class MetaSubscription
18
{
19
    public $identifier;
20
    public $currency;
21
    public $price;
22
    public $name;
23
    public $type;
24
    public $start_date;
25
    public $expiration_date;
26
    public $is_active;
27
    public $publication;
28
    public $defined;
29
    public $expire_in_days;
30
    private $subscription;
31
32
    public function __construct($subscription = null)
33
    {
34
        if (!$subscription instanceof UserSubscription) {
35
            $em = \Zend_Registry::get('container')->getService('em');
36
            if (!$subscription) {
37
                return;
38
            }
39
40
            $this->subscription = $em->getReference('Newscoop\PaywallBundle\Entity\UserSubscription', $subscription);
41
            if (!$this->subscription) {
42
                $this->subscription = new UserSubscription();
43
            }
44
        } else {
45
            $this->subscription = $subscription;
46
        }
47
48
        $this->identifier = $this->subscription->getId();
49
        $this->currency = $this->subscription->getCurrency();
50
        $this->price = $this->subscription->getToPay();
51
        $this->name = $this->subscription->getSubscription()->getName();
52
        $this->type = $this->getType();
53
        $this->start_date = $this->subscription->getCreatedAt();
54
        $this->expiration_date = $this->subscription->getExpireAt();
55
        $this->is_active = $this->isActive();
56
        $this->publication = $this->getPublication();
57
        if ($this->subscription->getExpireAt()) {
58
            $this->expire_in_days = $this->subscription->getExpireAt()
59
                ->diff($this->subscription->getCreatedAt())->format('%a');
60
        }
61
    }
62
63
    protected function getType()
64
    {
65
        $type = $this->subscription->getType();
66
67
        return $type == 'T' ? 'trial' : 'paid';
68
    }
69
70
    protected function getStartDate()
71
    {
72
        $startDate = null;
73
        $em = \Zend_Registry::get('container')->getService('em');
74
        $sections = $em->getRepository("Newscoop\PaywallBundle\Entity\Section")
75
            ->createQueryBuilder('s')
76
            ->where('s.subscription = :subscriptionId')
77
            ->setParameters(array(
78
                'subscriptionId' => $this->subscription->getId(),
79
            ))
80
            ->getQuery()
81
            ->getResult();
82
83
        foreach ($sections as $section) {
84
            $sectionStartDate = $section->getStartDate();
85
            if ($sectionStartDate < $startDate || is_null($startDate)) {
86
                $startDate = $sectionStartDate;
87
            }
88
        }
89
90
        return $startDate;
91
    }
92
93
    protected function isActive()
94
    {
95
        return $this->subscription->isActive();
96
    }
97
98
    protected function getPublication()
99
    {
100
        return $this->subscription->getPublicationName();
101
    }
102
}
103