Completed
Pull Request — develop (#22)
by Steven
03:56
created

ProductView   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 25 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 15 15 1
A execute() 0 19 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
}