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

ProductView::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 11
rs 9.4286
cc 1
eloc 9
nc 1
nop 4
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
}