Client::_addProperties()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Richdynamix\PersonalisedProducts\Model\PredictionIO\EventClient;
4
5
use \Richdynamix\PersonalisedProducts\Helper\Config;
6
use \Richdynamix\PersonalisedProducts\Helper\Urls;
7
use \Richdynamix\PersonalisedProducts\Logger\PersonalisedProductsLogger;
8
use \Richdynamix\PersonalisedProducts\Api\Data\EventClientInterface;
9
use \Richdynamix\PersonalisedProducts\Model\PredictionIO\Factory;
10
11
/**
12
 * Class EventClient for sending date and actions to PredictionIO
13
 *
14
 * @category  Richdynamix
15
 * @package   PersonalisedProducts
16
 * @author    Steven Richardson ([email protected]) @mage_gizmo
17
 */
18
class Client implements EventClientInterface
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 null|\predictionio\EngineClient|\predictionio\EventClient
42
     */
43
    private $_eventClient;
44
45
    /**
46
     * Client 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->_eventClient = $this->_factory->create(
64
            'event',
65
            $this->_urls->buildUrl(
66
                $this->_config->getItem(Config::EVENT_SERVER_URL),
67
                $this->_config->getItem(Config::EVENT_SERVER_PORT)
68
            ),
69
            $this->_config->getItem(Config::EVENT_SERVER_ACCESS_KEY)
70
        );
71
72
    }
73
74
    /**
75
     * Send customer data to PredictionIO
76
     *
77
     * @param int $customerId
78
     * @return bool
79
     */
80
    public function saveCustomerData($customerId)
81
    {
82
        return $this->_setEntity('user', $customerId);
83
    }
84
85
    /**
86
     * Send product data to PredictionIO
87
     *
88
     * @param int $productId
89
     * @param array $categoryIds
90
     * @return bool
91
     */
92
    public function saveProductData($productId, array $categoryIds = [])
93
    {
94
        return $this->_setEntity('item', $productId, $categoryIds);
95
    }
96
97
    /**
98
     * Send customer-views-product event to PredictionIO
99
     *
100
     * @param int $customerId
101
     * @param int $productId
102
     * @return bool
103
     */
104
    public function saveCustomerViewProduct($customerId, $productId)
105
    {
106
        return $this->_setCustomerToItemAction('view', $customerId, $productId);
107
    }
108
109
    /**
110
     * Send customer-buys-product event to PredictionIO
111
     *
112
     * @param int $customerId
113
     * @param int $productId
114
     * @return bool
115
     */
116
    public function saveCustomerBuyProduct($customerId, $productId)
117
    {
118
        return $this->_setCustomerToItemAction('buy', $customerId, $productId);
119
    }
120
121
    /**
122
     * Method for sending user-action-item events
123
     *
124
     * @param $action
125
     * @param $customerId
126
     * @param $productId
127
     * @return bool
128
     */
129
    private function _setCustomerToItemAction($action, $customerId, $productId)
130
    {
131
        try {
132
            $this->_eventClient->createEvent(array(
0 ignored issues
show
Bug introduced by
The method createEvent does only exist in predictionio\EventClient, but not in predictionio\EngineClient.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
133
                'event' => $action,
134
                'entityType' => 'user',
135
                'entityId' => $customerId,
136
                'targetEntityType' => 'item',
137
                'targetEntityId' => $productId
138
            ));
139
140
            return true;
141
        } catch (\Exception $e) {
142
            $this->_logger->addCritical($e);
143
        }
144
145
        return false;
146
147
    }
148
149
    /**
150
     * Method to send individual entities
151
     *
152
     * @param $entityType
153
     * @param $entityId
154
     * @param null $properties
155
     * @return bool
156
     */
157
    private function _setEntity($entityType, $entityId, $properties = null)
158
    {
159
        try {
160
            $data = $this->_addProperties(
161
                [
162
                    'event' => '$set',
163
                    'entityType' => $entityType,
164
                    'entityId' => $entityId
165
                ],
166
                $properties
167
            );
168
            $this->_eventClient->createEvent($data);
0 ignored issues
show
Bug introduced by
The method createEvent does only exist in predictionio\EventClient, but not in predictionio\EngineClient.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
169
            return true;
170
        } catch (\Exception $e) {
171
            $this->_logger->addCritical($e);
172
        }
173
174
        return false;
175
    }
176
177
    /**
178
     * Add properties to query
179
     *
180
     * @param $data
181
     * @param $properties
182
     * @return mixed
183
     */
184
    private function _addProperties($data, $properties)
185
    {
186
        if (null !== $properties) {
187
            $data['properties'] = ['categories' => $properties];
188
        }
189
190
        return $data;
191
    }
192
}
193