<?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 * @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 !'); } } }