1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Richdynamix\PersonalisedProducts\Controller\Products; |
4
|
|
|
|
5
|
|
|
use \Magento\Framework\App\Action\Action; |
6
|
|
|
use \Magento\Framework\Session\SessionManager; |
7
|
|
|
use \Magento\Customer\Model\Session as CustomerSession; |
8
|
|
|
use \Magento\Framework\Controller\Result\JsonFactory; |
9
|
|
|
use \Magento\Framework\App\Action\Context; |
10
|
|
|
use \Richdynamix\PersonalisedProducts\Model\PredictionIO\EventClient\Client; |
11
|
|
|
use \Richdynamix\PersonalisedProducts\Model\ProductView as ProductViewModel; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ProductView |
15
|
|
|
* |
16
|
|
|
* @category Richdynamix |
17
|
|
|
* @package PersonalisedProducts |
18
|
|
|
* @author Steven Richardson ([email protected]) @mage_gizmo |
19
|
|
|
*/ |
20
|
|
|
class ProductView extends Action { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var SessionManager |
24
|
|
|
*/ |
25
|
|
|
protected $_sessionManager; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var JsonFactory |
29
|
|
|
*/ |
30
|
|
|
protected $_resultJsonFactory; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* ProductView constructor. |
34
|
|
|
* @param Context $context |
35
|
|
|
* @param JsonFactory $resultJsonFactory |
36
|
|
|
* @param SessionManager $sessionManager |
37
|
|
|
* @param CustomerSession $customerSession |
38
|
|
|
* @param Client $eventClient |
39
|
|
|
* @param ProductViewModel $productView |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
42
|
|
|
Context $context, |
43
|
|
|
JsonFactory $resultJsonFactory, |
44
|
|
|
SessionManager $sessionManager, |
45
|
|
|
CustomerSession $customerSession, |
46
|
|
|
Client $eventClient, |
47
|
|
|
ProductViewModel $productView |
48
|
|
|
) { |
49
|
|
|
$this->_resultJsonFactory = $resultJsonFactory; |
50
|
|
|
$this->_sessionManager = $sessionManager; |
51
|
|
|
$this->_customerSession = $customerSession; |
52
|
|
|
$this->_eventClient = $eventClient; |
53
|
|
|
$this->_productView = $productView; |
54
|
|
|
parent::__construct($context); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function execute() |
61
|
|
|
{ |
62
|
|
|
$this->_sessionManager->start(); |
63
|
|
|
|
64
|
|
|
$result = $this->_resultJsonFactory->create(); |
65
|
|
|
|
66
|
|
|
$productId = $this->getRequest()->getParam('id'); |
67
|
|
|
if (empty($productId)) { |
68
|
|
|
return $result->setData(['error' => true, 'message' => 'Product ID has not been supplied']); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$pageViewResult = $this->_productView->processViews($productId); |
72
|
|
|
|
73
|
|
|
if (!$pageViewResult) { |
74
|
|
|
return $result->setData(['error' => true, 'message' => 'There was an error processing the product view.']); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $result->setData(['success' => true, 'message' => 'Product view logged in PredictionIO']); |
78
|
|
|
} |
79
|
|
|
} |
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.