1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Richdynamix\PersonalisedProducts\Model\PredictionIO\EngineClient; |
4
|
|
|
|
5
|
|
|
use \Richdynamix\PersonalisedProducts\Api\Data\EngineInterface; |
6
|
|
|
use \Richdynamix\PersonalisedProducts\Helper\Config; |
7
|
|
|
use Richdynamix\PersonalisedProducts\Helper\Urls; |
8
|
|
|
use \Richdynamix\PersonalisedProducts\Logger\PersonalisedProductsLogger; |
9
|
|
|
use \Richdynamix\PersonalisedProducts\Model\PredictionIO\Factory; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Similarity |
13
|
|
|
* |
14
|
|
|
* @category Richdynamix |
15
|
|
|
* @package PersonalisedProducts |
16
|
|
|
* @author Steven Richardson ([email protected]) @mage_gizmo |
17
|
|
|
*/ |
18
|
|
|
class Similarity implements EngineInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Factory |
22
|
|
|
*/ |
23
|
|
|
private $_factory; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var PersonalisedProductsLogger |
27
|
|
|
*/ |
28
|
|
|
private $_logger; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Config |
32
|
|
|
*/ |
33
|
|
|
private $_config; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Urls |
37
|
|
|
*/ |
38
|
|
|
private $_urls; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var \predictionio\EngineClient |
42
|
|
|
*/ |
43
|
|
|
private $_engineClient; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Similarity constructor. |
47
|
|
|
* @param Factory $factory |
48
|
|
|
* @param PersonalisedProductsLogger $logger |
49
|
|
|
* @param Config $config |
50
|
|
|
* @param Urls $urls |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
53
|
|
|
Factory $factory, |
54
|
|
|
PersonalisedProductsLogger $logger, |
55
|
|
|
Config $config, |
56
|
|
|
Urls $urls |
57
|
|
|
) { |
58
|
|
|
$this->_factory = $factory; |
59
|
|
|
$this->_logger = $logger; |
60
|
|
|
$this->_config = $config; |
61
|
|
|
$this->_urls = $urls; |
62
|
|
|
|
63
|
|
|
$this->_engineClient = $this->_factory->create( |
|
|
|
|
64
|
|
|
'engine', |
65
|
|
|
$this->_urls->buildUrl( |
66
|
|
|
$this->_config->getItem(Config::SIMILARITY_ENGINE_URL), |
67
|
|
|
$this->_config->getItem(Config::SIMILARITY_ENGINE_PORT) |
68
|
|
|
) |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Send the query to PredictionIO engine for product data set |
75
|
|
|
* |
76
|
|
|
* @param array $productIds |
77
|
|
|
* @param array $categories |
78
|
|
|
* @param array $whitelist |
79
|
|
|
* @param array $blacklist |
80
|
|
|
* @return array|bool |
81
|
|
|
*/ |
82
|
|
|
public function sendQuery(array $productIds, array $categories = [], array $whitelist = [], array $blacklist = []) |
83
|
|
|
{ |
84
|
|
|
try { |
85
|
|
|
$data = [ |
86
|
|
|
|
87
|
|
|
'items' => $productIds, |
88
|
|
|
'num' => (int) $this->_config->getProductCount(Config::SIMILARITY_PRODUCT_COUNT), |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
$this->_addProperties('categories', $data, $categories); |
92
|
|
|
$this->_addProperties('whitelist', $data, $whitelist); |
93
|
|
|
$this->_addProperties('blacklist', $data, $blacklist); |
94
|
|
|
|
95
|
|
|
return $this->_engineClient->sendQuery($data); |
96
|
|
|
} catch (\Exception $e) { |
97
|
|
|
$this->_logger->addCritical($e); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return false; |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Add query properties to the data in the query for PredictionIO engine |
106
|
|
|
* |
107
|
|
|
* @param $property |
108
|
|
|
* @param $data |
109
|
|
|
* @param $propertyData |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
|
|
private function _addProperties($property, &$data, $propertyData) |
113
|
|
|
{ |
114
|
|
|
if ($propertyData) { |
115
|
|
|
$data[$property] = $propertyData; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
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.