Completed
Pull Request — develop (#22)
by Steven
07:52 queued 04:02
created

ProductView   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 4
c 4
b 1
f 1
lcom 1
cbo 0
dl 0
loc 54
rs 10

2 Methods

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