Skip to content

Commit

Permalink
Add config setting for user ID source
Browse files Browse the repository at this point in the history
  • Loading branch information
henkelund committed Feb 12, 2017
1 parent 4aac1e0 commit 3cc9128
Show file tree
Hide file tree
Showing 13 changed files with 327 additions and 37 deletions.
43 changes: 33 additions & 10 deletions CustomerData/Customer/CustomerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,52 @@ class CustomerPlugin
protected $_dataHelper;

/**
* User ID Provider
* User ID provider pool
*
* @var \Henhed\Piwik\Model\UserId\ProviderInterface $_uidProvider
* @var \Henhed\Piwik\UserId\Provider\Pool $_uidProviderPool
*/
protected $_uidProvider;
protected $_uidProviderPool;

/**
* Constructor
*
* @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
* @param \Henhed\Piwik\Helper\Data $dataHelper
* @param \Henhed\Piwik\Model\UserId\ProviderInterface $uidProvider
* @param \Henhed\Piwik\UserId\Provider\Pool $uidProviderPool
*/
public function __construct(
\Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
\Henhed\Piwik\Helper\Data $dataHelper,
\Henhed\Piwik\Model\UserId\ProviderInterface $uidProvider
\Henhed\Piwik\UserId\Provider\Pool $uidProviderPool
) {
$this->_currentCustomer = $currentCustomer;
$this->_dataHelper = $dataHelper;
$this->_uidProvider = $uidProvider;
$this->_uidProviderPool = $uidProviderPool;
}

/**
* Get configured Piwik User ID provider or NULL
*
* @return \Henhed\Piwik\UserId\Provider\ProviderInterface|null
*/
protected function _getUserIdProvider()
{
$code = $this->_dataHelper->getUserIdProviderCode();
return $code ? $this->_uidProviderPool->getProviderByCode($code) : null;
}

/**
* Get Piwik User ID for current customer
*
* @return string
*/
protected function _getUserId()
{
$provider = $this->_getUserIdProvider();
$customerId = $this->_currentCustomer->getCustomerId();
return ($provider && $customerId)
? (string) $provider->getUserId($customerId)
: '';
}

/**
Expand All @@ -76,10 +101,8 @@ public function afterGetSectionData(
\Magento\Customer\CustomerData\Customer $subject,
$result
) {
if ($this->_dataHelper->isUserIdTrackingEnabled()
&& ($customerId = $this->_currentCustomer->getCustomerId())
) {
$userId = (string) $this->_uidProvider->getUserId($customerId);
if ($this->_dataHelper->isTrackingEnabled()) {
$userId = $this->_getUserId();
if ($userId !== '') {
$result['piwikUserId'] = $userId;
}
Expand Down
14 changes: 7 additions & 7 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
const XML_PATH_SITE_ID = 'piwik/tracking/site_id';
const XML_PATH_LINK_ENABLED = 'piwik/tracking/link_enabled';
const XML_PATH_LINK_DELAY = 'piwik/tracking/link_delay';
const XML_PATH_UID_ENABLED = 'piwik/tracking/uid_enabled';
const XML_PATH_UID_PROVIDER = 'piwik/tracking/uid_provider';

/**
* Check if Piwik is enabled
Expand Down Expand Up @@ -236,17 +236,17 @@ public function getLinkTrackingDelay($store = null)
}

/**
* Check if Piwik user ID tracking is enabled
* Get provider code for Piwik user ID tracking
*
* @param null|string|bool|int|Store $store
* @return bool
* @return string
*/
public function isUserIdTrackingEnabled($store = null)
public function getUserIdProviderCode($store = null)
{
return $this->scopeConfig->isSetFlag(
self::XML_PATH_UID_ENABLED,
return $this->scopeConfig->getValue(
self::XML_PATH_UID_PROVIDER,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$store
) && $this->isTrackingEnabled($store);
);
}
}
63 changes: 63 additions & 0 deletions Model/Config/Source/UserId/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Copyright 2016-2017 Henrik Hedelund
*
* This file is part of Henhed_Piwik.
*
* Henhed_Piwik is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Henhed_Piwik is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Henhed_Piwik. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Henhed\Piwik\Model\Config\Source\UserId;

/**
* User ID provider config source model
*
*/
class Provider implements \Magento\Framework\Option\ArrayInterface
{

/**
* User ID provider pool
*
* @var \Henhed\Piwik\UserId\Provider\Pool $_pool
*/
protected $_pool;

/**
* Constructor
*
* @param \Henhed\Piwik\UserId\Provider\Pool $pool
*/
public function __construct(\Henhed\Piwik\UserId\Provider\Pool $pool)
{
$this->_pool = $pool;
}

/**
* Return array of user ID providers as value-label pairs
*
* @return array
*/
public function toOptionArray()
{
$options = [['value' => '', 'label' => __('No')]];
foreach ($this->_pool->getAllProviders() as $code => $provider) {
$options[] = [
'value' => $code,
'label' => sprintf('%s (%s)', __('Yes'), $provider->getTitle())
];
}
return $options;
}
}
54 changes: 43 additions & 11 deletions Test/Unit/CustomerData/Customer/CustomerPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ class CustomerPluginTest extends \PHPUnit_Framework_TestCase
*/
protected $_dataHelperMock;

/**
* Piwik user ID provider pool mock object
*
* @var \PHPUnit_Framework_MockObject_MockObject $_uidProviderPoolMock
*/
protected $_uidProviderPoolMock;

/**
* Piwik user ID provider mock object
*
Expand Down Expand Up @@ -77,7 +84,11 @@ public function setUp()
$this->_customerPlugin = $objectManager->getObject($className, $args);
$this->_currentCustomerMock = $args['currentCustomer'];
$this->_dataHelperMock = $args['dataHelper'];
$this->_uidProviderMock = $args['uidProvider'];
$this->_uidProviderPoolMock = $args['uidProviderPool'];
$this->_uidProviderMock = $this->getMock(
'Henhed\Piwik\UserId\Provider\ProviderInterface',
['getUserId', 'getTitle'], [], '', false
);
$this->_customerDataMock = $this->getMock(
'Magento\Customer\CustomerData\Customer', [], [], '', false
);
Expand All @@ -91,10 +102,11 @@ public function setUp()
public function testafterGetSectionDataDataProvider()
{
return [
[false, 1, 'UID1'],
[true, null, 'UID2'],
[true, 3, ''],
[true, 4, 'UID4']
[false, 1, 'p', 'UID1'],
[true, null, 'p', 'UID2'],
[true, 3, 'p', ''],
[true, 4, null, 'UID4'],
[true, 5, 'p', 'UID5']
];
}

Expand All @@ -103,30 +115,50 @@ public function testafterGetSectionDataDataProvider()
*
* @param boolean $enabled
* @param int $customerId
* @param string|null $provider
* @param string $userId
* @return void
* @dataProvider testafterGetSectionDataDataProvider
*/
public function testafterGetSectionData($enabled, $customerId, $userId)
{
public function testafterGetSectionData(
$enabled, $customerId, $provider, $userId
) {
$expectedResult = [];
if ($enabled && $customerId && $userId) {
if ($enabled && $customerId && $provider && $userId) {
$expectedResult['piwikUserId'] = $userId;
}

// Enable tracking
$this->_dataHelperMock
->expects($this->once())
->method('isUserIdTrackingEnabled')
->method('isTrackingEnabled')
->willReturn($enabled);

$this->_dataHelperMock
->expects($enabled ? $this->once() : $this->never())
->method('getUserIdProviderCode')
->willReturn($provider);

$this->_currentCustomerMock
->expects($enabled ? $this->once() : $this->never())
->method('getCustomerId')
->willReturn($customerId);

$this->_uidProviderPoolMock
->expects(
($enabled && $provider)
? $this->once()
: $this->never()
)
->method('getProviderByCode')
->with($provider)
->willReturn($this->_uidProviderMock);

$this->_uidProviderMock
->expects($enabled && $customerId ? $this->once() : $this->never())
->expects(
($enabled && $customerId && $provider)
? $this->once()
: $this->never()
)
->method('getUserId')
->with($customerId)
->willReturn($userId);
Expand Down
68 changes: 68 additions & 0 deletions UserId/Provider/EmailProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Copyright 2016-2017 Henrik Hedelund
*
* This file is part of Henhed_Piwik.
*
* Henhed_Piwik is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Henhed_Piwik is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Henhed_Piwik. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Henhed\Piwik\UserId\Provider;

use Magento\Customer\Api\CustomerRepositoryInterface;

/**
* Customer email provider
*
*/
class EmailProvider implements ProviderInterface
{

/**
* Customer repository
*
* @var CustomerRepositoryInterface $_customerRepository
*/
protected $_customerRepository;

/**
* Constructor
*
* @param CustomerRepositoryInterface $customerRepository
*/
public function __construct(CustomerRepositoryInterface $customerRepository)
{
$this->_customerRepository = $customerRepository;
}

/**
* {@inheritDoc}
*/
public function getUserId($customerId)
{
try {
return $this->_customerRepository->getById($customerId)->getEmail();
} catch (\Exception $e) {
return false;
}
}

/**
* {@inheritDoc}
*/
public function getTitle()
{
return __('Customer E-mail');
}
}
14 changes: 11 additions & 3 deletions Model/UserId/Provider.php → UserId/Provider/EntityIdProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
* along with Henhed_Piwik. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Henhed\Piwik\Model\UserId;
namespace Henhed\Piwik\UserId\Provider;

/**
* User ID provider
* Customer entity ID provider
*
*/
class Provider implements ProviderInterface
class EntityIdProvider implements ProviderInterface
{

/**
Expand All @@ -34,4 +34,12 @@ public function getUserId($customerId)
{
return (string) $customerId;
}

/**
* {@inheritDoc}
*/
public function getTitle()
{
return __('Customer Entity ID');
}
}
Loading

0 comments on commit 3cc9128

Please sign in to comment.