HookController   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 130
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
B sidebarAction() 0 40 4
B unmarkAction() 0 26 2
A findOneOr404() 0 14 2
B buildSubscription() 0 28 2
A returnResponse() 0 7 1
1
<?php
2
3
/**
4
 * @author Rafał Muszyński <[email protected]>
5
 * @copyright 2015 Sourcefabric z.ú.
6
 * @license http://www.gnu.org/licenses/gpl-3.0.txt
7
 */
8
9
namespace Newscoop\PaywallBundle\Controller;
10
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
12
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\JsonResponse;
15
use Newscoop\PaywallBundle\Entity\Subscription;
16
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
17
use Newscoop\PaywallBundle\Entity\SubscriptionSpecification;
18
use Newscoop\Entity\Article;
19
use Doctrine\Common\Collections\ArrayCollection;
20
use Newscoop\PaywallBundle\Permissions;
21
22
class HookController extends BaseController
23
{
24
    /**
25
     * @Route("/admin/paywall_plugin/sidebar/{articleNumber}/{articleLanguage}/{allowed}", options={"expose"=true})
26
     * @Method("POST")
27
     */
28
    public function sidebarAction(Request $request, $articleNumber, $articleLanguage, $allowed = false)
29
    {
30
        $entityManager = $this->get('em');
31
        $templateId = $request->request->get('paywallTemplateSubscriptionId');
32
        $templatesProvider = $this->get('newscoop_paywall.subscription_template_provider');
33
        $article = $this->findOneOr404($articleNumber, $articleLanguage);
34
        $specification = $entityManager
35
            ->getRepository('Newscoop\PaywallBundle\Entity\SubscriptionSpecification')
36
            ->findSpecification(
37
                $article->getNumber(),
38
                $article->getPublicationId()
39
            );
40
41
        $templates = $templatesProvider->getAvailableTemplates('article', $article->getLanguageCode());
42
        $subscription = $templatesProvider->getOneTemplate($templateId);
43
        if (!$subscription) {
44
            return $this->returnResponse(array(
45
                'status' => false,
46
            ));
47
        }
48
49
        if ($specification) {
50
            $specification->getSubscription()->setIsActive(false);
51
            $specification->setIsActive(false);
52
        } else {
53
            // make switch unchecked when subscription exists for given article
54
            $article->setPublic('N');
55
        }
56
57
        $specification = $this->buildSubscription($subscription, $article);
58
59
        return $this->returnResponse(array(
60
            'specification' => $specification,
61
            'templates' => $templates,
62
            'articleNumber' => $article->getNumber(),
63
            'articleLanguage' => $article->getLanguageId(),
64
            'isPublic' => $article->getPublic() === 'Y' ? true : false,
65
            'hasPermission' => $allowed,
66
        ));
67
    }
68
69
    /**
70
     * @Route("/admin/paywall_plugin/sidebar/{articleNumber}/{articleLanguage}", options={"expose"=true})
71
     * @Method("PATCH")
72
     */
73
    public function unmarkAction($articleNumber, $articleLanguage)
74
    {
75
        $this->hasPermission(Permissions::SIDEBAR);
76
        $entityManager = $this->get('em');
77
        $article = $this->findOneOr404($articleNumber, $articleLanguage);
78
        $specification = $entityManager
79
            ->getRepository('Newscoop\PaywallBundle\Entity\SubscriptionSpecification')
80
            ->findSpecification(
81
                $article->getNumber(),
82
                $article->getPublicationId()
83
            );
84
85
        if (!$specification) {
86
            return new JsonResponse(array(
87
                'status' => false,
88
            ));
89
        }
90
91
        $specification->getSubscription()->setIsActive(false);
92
        $specification->setIsActive(false);
93
        $entityManager->flush();
94
95
        return new JsonResponse(array(
96
            'status' => true,
97
        ));
98
    }
99
100
    private function findOneOr404($articleNumber, $articleLanguage)
101
    {
102
        $entityManager = $this->get('em');
103
        $article = $entityManager->getRepository('Newscoop\Entity\Article')->findOneBy(array(
104
            'number' => $articleNumber,
105
            'language' => $articleLanguage,
106
        ));
107
108
        if (!$article) {
109
            throw new NotFoundHttpException('The article does not exist.');
110
        }
111
112
        return $article;
113
    }
114
115
    private function buildSubscription(Subscription $subscription, Article $article)
116
    {
117
        $entityManager = $this->get('em');
118
        $subscription = clone $subscription;
119
        $subscription->setCreatedAt(new \DateTime());
120
        $subscription->setName($subscription->getName().'-'.$article->getNumber());
121
        $subscription->setIsTemplate(false);
122
        $ranges = new ArrayCollection();
123
        foreach ($subscription->getRanges() as $value) {
124
            $value = clone $value;
125
            $value->setSubscription($subscription);
126
            $ranges->add($value);
127
        }
128
129
        $subscription->setRanges($ranges);
130
        $entityManager->persist($subscription);
131
        $specification = new SubscriptionSpecification();
132
        $specification->setSubscription($subscription);
133
        $specification->setPublication($article->getPublicationId());
134
        $specification->setIssue($article->getIssue()->getNumber());
135
        $specification->setSection($article->getSection()->getNumber());
136
        $specification->setArticle($article->getNumber());
137
138
        $entityManager->persist($specification);
139
        $entityManager->flush();
140
141
        return $specification;
142
    }
143
144
    private function returnResponse(array $data)
145
    {
146
        return $this->container->get('templating')->renderResponse(
147
            'NewscoopPaywallBundle:Hook:sidebar.html.twig',
148
            $data
149
        );
150
    }
151
}
152