<?php

/**
 * Product:       Xtento_ProductExport
 * ID:            %!uniqueid!%
 * Last Modified: 2016-04-14T15:37:57+00:00
 * File:          Model/Destination/Http.php.sample
 * Copyright:     Copyright (c) XTENTO GmbH & Co. KG <info@xtento.com> / All rights reserved.
 */

namespace Xtento\ProductExport\Model\Destination;

class Http extends AbstractClass
{
    /*
     * !!!!! IMPORTANT !!!!!
     *
     * Modify below this line. Add custom functions, similar to the function below. There MUST be one parameter which will contain the files which have been generated by the module in the format array($filename => $fileContent)
     */
    public function yourFunctionName($fileArray)
    {
        // Do whatever - sample code for a HTTP request below.
        foreach ($fileArray as $filename => $fileContent) {
            $curlClient = curl_init();
            curl_setopt($curlClient, CURLOPT_URL, '');
            curl_setopt($curlClient, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curlClient, CURLOPT_POST, 1);
            curl_setopt($curlClient, CURLOPT_POSTFIELDS, $fileContent);
            //curl_setopt($curlClient, CURLOPT_POSTFIELDS, array('SampleParameter' => $fileContent)); // Send as a parameter instead
            /*curl_setopt($curlClient, CURLOPT_HTTPHEADER, array(
                'Authorization: Basic ' . base64_encode("user:password"), // HTTP Basic Authorization
                'Accept: text/html', // Sample header Accept
            ));*/
            curl_setopt($curlClient, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($curlClient, CURLOPT_SSL_VERIFYHOST, 0);
            $result = curl_exec($curlClient);
            curl_close($curlClient);

            // Log result
            $logEntry = $this->_registry->registry('productexport_log');
            #$logEntry->setResult(\Xtento\ProductExport\Model\Log::RESULT_WARNING);
            $logEntry->addResultMessage(__('Destination "%1" (ID: %2): %3', $this->getDestination()->getName(), $this->getDestination()->getId(), htmlentities($result)));
        }
    }

    /*
     * !!!!! Do not modify below this line !!!!!
     */
    public function testConnection()
    {
        $this->initConnection();
        if (!$this->getDestination()->getBackupDestination()) {
            $this->getDestination()->setLastResult($this->getTestResult()->getSuccess())->setLastResultMessage($this->getTestResult()->getMessage())->save();
        }
        return $this->getTestResult();
    }

    public function initConnection()
    {
        $this->setDestination($this->destinationFactory->create()->load($this->getDestination()->getId()));
        $testResult = new \Magento\Framework\DataObject();
        $this->setTestResult($testResult);
        if (!@method_exists($this, $this->getDestination()->getCustomFunction())) {
            $this->getTestResult()->setSuccess(false)->setMessage(__('Custom function/method \'%1\' not found in %2.', $this->getDestination()->getCustomFunction(), __FILE__));
        } else {
            $this->getTestResult()->setSuccess(true)->setMessage(__('Custom function/method found and ready to use.', __FILE__));
        }
        return true;
    }

    public function saveFiles($fileArray)
    {
        if (empty($fileArray)) {
            return [];
        }
        // Init connection
        $this->initConnection();
        // Call custom function
        @$this->{$this->getDestination()->getCustomFunction()}($fileArray);
        return array_keys($fileArray);
    }
}