Similarity::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 20
Ratio 100 %

Importance

Changes 0
Metric Value
dl 20
loc 20
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
nop 4
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(
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...
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(
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->_factory->create(...MILARITY_ENGINE_PORT))) can also be of type object<predictionio\EventClient>. However, the property $_engineClient is declared as type object<predictionio\EngineClient>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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