Newer
Older
<?php
namespace NicolasBejean\Customer\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\Exception\FileSystemException;
use Magento\Store\Api\StoreRepositoryInterface;
use Magento\Framework\Filesystem;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\File\Csv;
use Magento\Framework\Exception\NoSuchEntityException;
/**
* Class SendCustomer
*
* @category PHP
* @package NicolasBejean\Customer\Helper
* @author Nicolas Béjean <nicolas@bejean.eu>
* @license https://lab.frogg.it/bejean-developpement/magento-2/modules/customer/-/blob/master/LICENCE GPL3 Licence
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
* @link https://www.bejean.eu
*/
class SendCustomer extends AbstractHelper
{
/**
* @var array
*/
protected $data;
/**
* @var StoreRepositoryInterface
*/
protected $storeRepository;
/**
* @var Filesystem
*/
protected $filesystem;
/**
* @var DirectoryList
*/
protected $directoryList;
/**
* @var Csv
*/
protected $csvProcessor;
/**
* SendCustomer constructor.
*
* @param Context $context
* @param StoreRepositoryInterface $storeRepository
* @param Filesystem $filesystem
* @param DirectoryList $directoryList
* @param Csv $csvProcessor
*/
public function __construct(
Context $context,
StoreRepositoryInterface $storeRepository,
Filesystem $filesystem,
DirectoryList $directoryList,
Csv $csvProcessor
) {
$this->storeRepository = $storeRepository;
$this->filesystem = $filesystem;
$this->directoryList = $directoryList;
$this->csvProcessor = $csvProcessor;
parent::__construct($context);
}
/**
* Récupère les données du customer et l'origine
* Mappe les données dans le tableau pour l'écrire dans le CSV
*
* @param string $origin
* @param CustomerInterface $customer
*/
public function execute(string $origin, CustomerInterface $customer)
{
/* Mise en place des données du customer dans le tableau de data */
$this->data[0]['id'] = $customer->getId();
$this->data[0]['firstname'] = $customer->getFirstname();
$this->data[0]['lastname'] = $customer->getLastname();
$this->data[0]['email'] = $customer->getEmail();
$this->data[0]['created_at'] = $customer->getCreatedAt();
$this->data[0]['origin'] = $origin;
try {
/* Récupére le nom du store */
$store = $this->storeRepository->getById($customer->getStoreId());
$this->data[0]['storeName'] = $store->getName();
} catch (NoSuchEntityException $e) {
/* Si le nom du store ne peut pas être récupéré, on force la valeur de storeName */
$this->data[0]['storeName'] = 'unknown';
}
$this->writeToCsv();
}
/**
* Ecrit les données dans le fichier CSV
*/
private function writeToCsv () {
try {
/* Récupère le dossier où seront exportées les données */
$dirPath = $this->directoryList->getPath(DirectoryList::VAR_DIR) . '/exportCustomer';
} catch (FileSystemException $e) {
die ('Unable to get dirPath !');
}
/* Vérifie si le dossier existe */
if (!is_dir($dirPath)) {
/* Créé le dossier si il n'existe pas */
mkdir($dirPath, 0750, true);
}
/* Définit le nom du fichier qui sera créé */
$filename = 'customer_' . $this->data[0]['id'];
/* Défini le path du fichier */
$filePath = $dirPath . '/' . $filename . '.csv';
try {
/* Enregistre les données dans le fichier */
$this->csvProcessor->appendData($filePath, $this->data);
} catch (FileSystemException $e) {
die ('Unable to save file !');
}
}
}