1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Richdynamix\PersonalisedProducts\Console\Command; |
4
|
|
|
|
5
|
|
|
use \Symfony\Component\Console\Command\Command; |
6
|
|
|
use \Magento\Customer\Model\CustomerFactory; |
7
|
|
|
use \Richdynamix\PersonalisedProducts\Model\PredictionIO\EventClient\Client; |
8
|
|
|
use \Symfony\Component\Config\Definition\Exception\Exception; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class AbstractCustomerCommand |
12
|
|
|
* |
13
|
|
|
* @category Richdynamix |
14
|
|
|
* @package PersonalisedProducts |
15
|
|
|
* @author Steven Richardson ([email protected]) @mage_gizmo |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractCustomerCommand extends Command |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var CustomerFactory |
21
|
|
|
*/ |
22
|
|
|
private $_customerFactory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Client |
26
|
|
|
*/ |
27
|
|
|
private $_eventClient; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* AbstractCustomerCommand constructor. |
31
|
|
|
* @param CustomerFactory $customerFactory |
32
|
|
|
* @param Client $eventClient |
33
|
|
|
*/ |
34
|
|
|
public function __construct(CustomerFactory $customerFactory, Client $eventClient) |
35
|
|
|
{ |
36
|
|
|
$this->_customerFactory = $customerFactory; |
37
|
|
|
$this->_eventClient = $eventClient; |
38
|
|
|
parent::__construct(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Send the customer data to PredictionIO using the event client |
43
|
|
|
* |
44
|
|
|
* @param $collection |
45
|
|
|
* @return int |
46
|
|
|
*/ |
47
|
|
|
protected function _sendCustomerData($collection) |
48
|
|
|
{ |
49
|
|
|
$collectionCount = count($collection); |
50
|
|
|
$sentCustomersCount = 0; |
51
|
|
|
foreach ($collection as $customerId) { |
52
|
|
|
if ($this->_eventClient->saveCustomerData($customerId)) { |
53
|
|
|
++$sentCustomersCount; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ($collectionCount != $sentCustomersCount) { |
58
|
|
|
throw new Exception( |
59
|
|
|
'There was a problem sending the customer data, check the log file for more information' |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $sentCustomersCount; |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get a collection of all customer ID's, regardless if they have been exported before. |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
protected function _getCustomerCollection() |
73
|
|
|
{ |
74
|
|
|
$customer = $this->_customerFactory->create(); |
75
|
|
|
return $customer->getCollection()->getAllIds(); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|