Completed
Push — master ( 9804bd...abf53e )
by Rafał
06:11 queued 02:57
created

modifier.has_subscription.php ➔ smarty_modifier_has_subscription()   C

Complexity

Conditions 22
Paths 64

Size

Total Lines 56
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 22
eloc 36
c 1
b 0
f 1
nc 64
nop 1
dl 0
loc 56
rs 6.4788

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license http://www.gnu.org/licenses/gpl-3.0.txt
5
 */
6
7
/**
8
 * Function to check if user has access to content.
9
 *
10
 * @param array  $params
0 ignored issues
show
Bug introduced by
There is no parameter named $params. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
11
 * @param object $smarty
0 ignored issues
show
Bug introduced by
There is no parameter named $smarty. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
12
 *
13
 * @return string
14
 */
15
function smarty_modifier_has_subscription($user)
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
16
{
17
    $context = \CampTemplate::singleton()->context();
18
    $user = $context->user;
19
    if (!$user) {
20
        return false;
21
    }
22
23
    $em = \Zend_Registry::get('container')->getService('em');
24
    $userSubscriptions = $em->getRepository("Newscoop\PaywallBundle\Entity\UserSubscription")
25
        ->getValidSubscriptionsBy($user->identifier)->getArrayResult();
26
27
    $publication = $context->publication;
28
    $issue = $context->issue;
29
    $section = $context->section;
30
    $article = $context->article;
31
32
    try {
33
        foreach ($userSubscriptions as $key => $value) {
34
            $specification = $value['subscription']['specification'][0];
35
            if ($value['subscription']['type'] === 'publication' && $publication) {
36
                if ($specification['publication'] == $publication->identifier) {
37
                    return true;
38
                }
39
            }
40
41
            if ($value['subscription']['type'] === 'issue' && $issue) {
42
                if ($specification['issue'] == $issue->number &&
43
                    $specification['publication'] == $issue->publication->identifier) {
44
                    return true;
45
                }
46
            }
47
48
            if ($value['subscription']['type'] === 'section' && $section) {
49
                if ($specification['section'] == $section->number &&
50
                    $specification['issue'] == $section->issue->number &&
51
                    $specification['publication'] == $issue->publication->identifier) {
52
                    return true;
53
                }
54
            }
55
56
            if ($value['subscription']['type'] === 'article' && $article) {
57
                if ($specification['article'] == $article->number &&
58
                    $specification['publication'] == $article->publication->identifier &&
59
                    $specification['issue'] == $article->issue->number &&
60
                    $specification['section'] == $article->section->number) {
61
                    return true;
62
                }
63
            }
64
        }
65
    } catch (\Exception $e) {
66
        return false;
67
    }
68
69
    return false;
70
}
71