Sitewards_B2BProfessional_Helper_Customer   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 1
dl 0
loc 216
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
B isCustomerLoggedIn() 0 29 4
A isCustomerActivationGlobal() 0 4 1
A isCustomerActiveForStore() 0 21 2
A isCustomerActiveForWebsite() 0 13 1
A isCustomerAdminCreation() 0 4 1
A isCustomerGroupActive() 0 9 1
A isLoginRequired() 0 4 1
A isExtensionActivatedByCustomerGroup() 0 4 1
A getActivatedCustomerGroupIds() 0 17 2
1
<?php
2
3
/**
4
 * Sitewards_B2BProfessional_Helper_Customer
5
 *  - Helper containing the checks for
6
 *      - customer is active
7
 *      - if login is required
8
 *
9
 * @category    Sitewards
10
 * @package     Sitewards_B2BProfessional
11
 * @copyright   Copyright (c) 2014 Sitewards GmbH (http://www.sitewards.com/)
12
 */
13
class Sitewards_B2BProfessional_Helper_Customer extends Sitewards_B2BProfessional_Helper_Core
14
{
15
    /**
16
     * Path for the config for extension requires login
17
     */
18
    const CONFIG_EXTENSION_REQUIRES_LOGIN = 'b2bprofessional/requirelogin/requirelogin';
19
20
    /**
21
     * Path for the config for extension is active by customer group
22
     */
23
    const CONFIG_EXTENSION_ACTIVE_BY_CUSTOMER_GROUP = 'b2bprofessional/activatebycustomersettings/activebycustomer';
24
25
    /**
26
     * Path for the config for customer groups that are activated
27
     */
28
    const CONFIG_EXTENSION_ACTIVE_CUSTOMER_GROUPS = 'b2bprofessional/activatebycustomersettings/activecustomers';
29
30
    /**
31
     * Path for the config for global customers activation
32
     */
33
    const CONFIG_EXTENSION_ACTIVE_CUSTOMER_GLOBALLY = 'b2bprofessional/generalsettings/activecustomers';
34
35
    /**
36
     * Path for the config for checking customer website and not customer store
37
     */
38
    const CONFIG_EXTENSION_CHECK_WEBSITE = 'b2bprofessional/generalsettings/check_website';
39
40
    /**
41
     * Flag if the extension is set to require login
42
     *
43
     * @var bool
44
     */
45
    protected $bLoginRequired;
46
47
    /**
48
     * Flag if the extension is active by customer group
49
     *
50
     * @var bool
51
     */
52
    protected $bExtensionActiveByCustomerGroup;
53
54
    /**
55
     * Array of activated customer group ids
56
     *
57
     * @var int[]
58
     */
59
    protected $aActivatedCustomerGroupIds = array();
60
61
    /**
62
     * Check to see if the customer is logged in
63
     *  - Is the session logged in,
64
     *  - Are customers activated globally,
65
     *  - Is this one customer active for the current store,
66
     *
67
     * @return bool
68
     */
69
    public function isCustomerLoggedIn()
70
    {
71
        if (Mage::helper('customer')->isLoggedIn() === true) {
72
            /*
73
             * If customer activation is global
74
             *  - then any customer can access any store
75
             */
76
            if ($this->isCustomerActivationGlobal()) {
77
                return true;
78
            }
79
80
            /* @var $oCustomerSession Mage_Customer_Model_Session */
81
            $oCustomerSession = Mage::getSingleton('customer/session');
82
            /* @var $oCustomer Mage_Customer_Model_Customer */
83
            $oCustomer = $oCustomerSession->getCustomer();
84
            /* @var bool $bCheckCustomerWebsite */
85
            $bCheckCustomerWebsite = Mage::getStoreConfigFlag(self::CONFIG_EXTENSION_CHECK_WEBSITE);
86
87
            if ($bCheckCustomerWebsite) {
88
                /* checking if customer website matches the current one */
89
                return $this->isCustomerActiveForWebsite($oCustomer);
90
            } else {
91
                /* checking if customer store matches the current one */
92
                return $this->isCustomerActiveForStore($oCustomer);
93
            }
94
        }
95
96
        return false;
97
    }
98
99
    /**
100
     * Check if the extension has customers activated globally
101
     *
102
     * @return bool
103
     */
104
    private function isCustomerActivationGlobal()
105
    {
106
        return Mage::getStoreConfigFlag(self::CONFIG_EXTENSION_ACTIVE_CUSTOMER_GLOBALLY);
107
    }
108
109
    /**
110
     * Check if a given customer is allowed for the current store
111
     *  - NOTE: users created via the admin section cannot be attached to a front end store and so are globally active
112
     *
113
     * @param Mage_Customer_Model_Customer $oCustomer
114
     * @return bool
115
     */
116
    private function isCustomerActiveForStore(Mage_Customer_Model_Customer $oCustomer)
117
    {
118
        /*
119
         * Check to see if the user was created via the admin section
120
         *  - Note: users created via the admin section cannot be attached to a front end store
121
         */
122
        if ($this->isCustomerAdminCreation($oCustomer)) {
123
            return true;
124
        }
125
126
        /*
127
         * Get user's store and current store for comparison
128
         */
129
        $iUserStoreId    = $oCustomer->getStoreId();
130
        $iCurrentStoreId = Mage::app()->getStore()->getId();
131
        /*
132
         * Return true if:
133
         *  - the user's store id matches the current store id
134
         */
135
        return $iUserStoreId === $iCurrentStoreId;
136
    }
137
138
    /**
139
     * Check if a given customer is allowed for the current website
140
     *
141
     * @param Mage_Customer_Model_Customer $oCustomer
142
     * @return bool
143
     */
144
    private function isCustomerActiveForWebsite(Mage_Customer_Model_Customer $oCustomer)
145
    {
146
        /*
147
         * Get user's website and current website for comparison
148
         */
149
        $iUserWebsiteId    = $oCustomer->getWebsiteId();
150
        $iCurrentWebsiteId = Mage::app()->getWebsite()->getId();
151
        /*
152
         * Return true if:
153
         *  - the user's website id matches the current website id
154
         */
155
        return $iUserWebsiteId === $iCurrentWebsiteId;
156
    }
157
158
    /**
159
     * Check to see if the user has been created via the admin section
160
     *
161
     * @param Mage_Customer_Model_Customer $oCustomer
162
     * @return bool
163
     */
164
    private function isCustomerAdminCreation(Mage_Customer_Model_Customer $oCustomer)
165
    {
166
        return $oCustomer->getStoreId() === Mage_Core_Model_App::ADMIN_STORE_ID;
167
    }
168
169
    /**
170
     * Check to see if the customer's group is active
171
     *  - If they are not logged in then we do not need to check any further
172
     *
173
     * @return bool
174
     */
175
    public function isCustomerGroupActive()
176
    {
177
        /* @var $oCustomerSession Mage_Customer_Model_Session */
178
        $oCustomerSession        = Mage::getModel('customer/session');
179
        $iCurrentCustomerGroupId = $oCustomerSession->getCustomerGroupId();
180
        $aActiveCustomerGroupIds = $this->getActivatedCustomerGroupIds();
181
182
        return in_array($iCurrentCustomerGroupId, $aActiveCustomerGroupIds);
183
    }
184
185
    /**
186
     * Check to see if the website is set-up to require a user login to view pages
187
     *
188
     * @return bool
189
     */
190
    public function isLoginRequired()
191
    {
192
        return $this->getStoreFlag(self::CONFIG_EXTENSION_REQUIRES_LOGIN, 'bLoginRequired');
193
    }
194
195
    /**
196
     * Check to see if the extension is activated by customer group
197
     *
198
     * @return bool
199
     */
200
    public function isExtensionActivatedByCustomerGroup()
201
    {
202
        return $this->getStoreFlag(self::CONFIG_EXTENSION_ACTIVE_BY_CUSTOMER_GROUP, 'bExtensionActiveByCustomerGroup');
203
    }
204
205
    /**
206
     * Get an array of all the activated customer group ids
207
     *  - always include the 'NOT LOGGED IN' group
208
     *
209
     * @return int[]
210
     */
211
    private function getActivatedCustomerGroupIds()
212
    {
213
        if (empty($this->aActivatedCustomerGroupIds)) {
214
            /*
215
             * Customer group ids are saved in the config in format
216
             *  - "group1,group2"
217
             */
218
            $sActivatedCustomerGroups         = Mage::getStoreConfig(self::CONFIG_EXTENSION_ACTIVE_CUSTOMER_GROUPS);
219
            $this->aActivatedCustomerGroupIds = explode(',', $sActivatedCustomerGroups);
220
221
            /*
222
             * Always add the guest user group id when activated by customer group
223
             */
224
            $this->aActivatedCustomerGroupIds[] = Mage_Customer_Model_Group::NOT_LOGGED_IN_ID;
225
        }
226
        return $this->aActivatedCustomerGroupIds;
227
    }
228
}