getActionsPermissions()   D
last analyzed

Complexity

Conditions 11
Paths 128

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 4.9629
c 0
b 0
f 0
cc 11
eloc 16
nc 128
nop 2

How to fix   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
namespace Oro\Bundle\MagentoBundle\Datagrid;
4
5
use Oro\Bundle\DataGridBundle\Datasource\ResultRecordInterface;
6
use Oro\Bundle\SecurityBundle\SecurityFacade;
7
use Oro\Bundle\MagentoBundle\Entity\NewsletterSubscriber;
8
use Oro\Bundle\MagentoBundle\Entity\Customer;
9
10
class NewsletterSubscriberPermissionProvider extends AbstractTwoWaySyncActionPermissionProvider
11
{
12
    /**
13
     * @var SecurityFacade
14
     */
15
    protected $securityFacade;
16
17
    /**
18
     * @var bool|null
19
     */
20
    protected $subscribeGranted;
21
22
    /**
23
     * @var bool|null
24
     */
25
    protected $unsubscribeGranted;
26
27
    /**
28
     * @param ResultRecordInterface $record
29
     * @param array $actions
30
     *
31
     * @return array
32
     */
33
    public function getActionsPermissions(ResultRecordInterface $record, array $actions)
34
    {
35
        $actions = array_keys($actions);
36
        $permissions = [];
37
        foreach ($actions as $action) {
38
            $permissions[$action] = true;
39
        }
40
41
        $isChannelApplicable = $this->isChannelApplicable($record);
42
        $customerId = $record->getValue(self::CUSTOMER_ID);
43
        $customerOriginId = $record->getValue(self::CUSTOMER_ORIGIN_ID);
44
45
        $isActionAllowed = $isChannelApplicable && (($customerOriginId && $customerId) || !$customerId);
46
47
        $statusId = (int)$record->getValue('newsletterSubscriberStatusId');
48
        $isSubscribed = $statusId === NewsletterSubscriber::STATUS_SUBSCRIBED;
49
50
        if (array_key_exists('subscribe', $permissions)) {
51
            $permissions['subscribe'] = $this->isSubscribeGranted() && $isActionAllowed && !$isSubscribed;
52
        }
53
54
        if (array_key_exists('unsubscribe', $permissions)) {
55
            $permissions['unsubscribe'] = $this->isUnsubscribeGranted() && $isActionAllowed && $isSubscribed;
56
        }
57
58
        return $permissions;
59
    }
60
61
    /**
62
     * @param SecurityFacade $securityFacade
63
     * @return NewsletterSubscriberPermissionProvider
64
     */
65
    public function setSecurityFacade(SecurityFacade $securityFacade)
66
    {
67
        $this->securityFacade = $securityFacade;
68
69
        return $this;
70
    }
71
72
    /**
73
     * Check if channel integration is applicable for magento customer.
74
     * Get channel integration id from customer channel if it's does not exist in grid record
75
     *
76
     * @param ResultRecordInterface $record
77
     * @param bool $checkExtension
78
     *
79
     * @return bool
80
     */
81 View Code Duplication
    protected function isChannelApplicable(ResultRecordInterface $record, $checkExtension = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
    {
83
        $channelId = $this->getChannelId($record);
84
85
        if (!$channelId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $channelId of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
86
            return false;
87
        }
88
89
        return $this->channelSettingsProvider->isChannelApplicable($channelId, $checkExtension);
90
    }
91
92
    /**
93
     * @param ResultRecordInterface $record
94
     *
95
     * @return int|null
96
     */
97
    protected function getChannelId(ResultRecordInterface $record)
98
    {
99
        $channelId = $record->getValue(self::CHANNEL_KEY);
100
        if (!$channelId) {
101
            $customer = $record->getValue(self::CUSTOMER);
102
            if ($customer instanceof Customer || $customer instanceof NewsletterSubscriber) {
103
                $channel = $customer->getChannel();
104
                if ($channel) {
105
                    $channelId = $customer->getChannel()->getId();
106
                }
107
            }
108
        }
109
110
        return $channelId;
111
    }
112
113
    /**
114
     * @return bool
115
     */
116
    protected function isSubscribeGranted()
117
    {
118
        if ($this->subscribeGranted === null) {
119
            $this->subscribeGranted = $this->securityFacade
120
                ->isGranted('oro_magento_newsletter_subscriber_subscribe_customer');
121
        }
122
123
        return $this->subscribeGranted;
124
    }
125
126
    /**
127
     * @return bool
128
     */
129
    protected function isUnsubscribeGranted()
130
    {
131
        if ($this->unsubscribeGranted === null) {
132
            $this->unsubscribeGranted = $this->securityFacade
133
                ->isGranted('oro_magento_newsletter_subscriber_unsubscribe_customer');
134
        }
135
136
        return $this->unsubscribeGranted;
137
    }
138
}
139