TransactionMap.php 5.16 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Braintree\Model\Report\Row;

use Braintree\Transaction;
use DateTime;
use Magento\Framework\Api\AttributeValueFactory;
use Magento\Framework\Api\Search\DocumentInterface;

/**
 * Class TransactionMap
 */
class TransactionMap implements DocumentInterface
{
    const TRANSACTION_FIELD_MAP_DELIMITER = '_';

    /**
     * @var AttributeValueFactory
     */
    private $attributeValueFactory;

    /**
     * @var Transaction
     */
    private $transaction;

    /**
     * @var array
     */
    public static $simpleFieldsMap = [
        'id',
        'merchantAccountId',
        'orderId',
        'paymentInstrumentType',
        'paypalDetails_paymentId',
        'type',
        'createdAt',
        'amount',
        'processorSettlementResponseCode',
        'status',
        'processorSettlementResponseText',
        'refundIds',
        'settlementBatchId',
        'currencyIsoCode'
    ];

    /**
     * @param AttributeValueFactory $attributeValueFactory
     * @param Transaction $transaction
     */
    public function __construct(
        AttributeValueFactory $attributeValueFactory,
        Transaction $transaction
    ) {
        $this->attributeValueFactory = $attributeValueFactory;
        $this->transaction = $transaction;
    }

    /**
     * Get Id
     *
     * @return string
     */
    public function getId()
    {
        return $this->getMappedValue('id');
    }

    /**
     * Set Id
     *
     * @param int $id
     * @return void
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function setId($id)
    {
    }

    /**
     * Get an attribute value.
     *
     * @param string $attributeCode
     * @return \Magento\Framework\Api\AttributeInterface|null
     */
    public function getCustomAttribute($attributeCode)
    {
        /** @var \Magento\Framework\Api\AttributeInterface $attributeValue */
        $attributeValue = $this->attributeValueFactory->create();
        $attributeValue->setAttributeCode($attributeCode);
        $attributeValue->setValue($this->getMappedValue($attributeCode));
        return $attributeValue;
    }

    /**
     * Set an attribute value for a given attribute code
     *
     * @param string $attributeCode
     * @param mixed $attributeValue
     * @return $this
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function setCustomAttribute($attributeCode, $attributeValue)
    {
        return $this;
    }

    /**
     * Retrieve custom attributes values.
     *
     * @return \Magento\Framework\Api\AttributeInterface[]|null
     */
    public function getCustomAttributes()
    {
        $shouldBeLocalized = ['paymentInstrumentType', 'type', 'status'];
        $output = [];
        foreach ($this->getMappedValues() as $key => $value) {
            $attribute = $this->attributeValueFactory->create();
            if (in_array($key, $shouldBeLocalized)) {
                $value = __($value);
            }
            $output[] = $attribute->setAttributeCode($key)->setValue($value);
        }
        return $output;
    }

    /**
     * Set array of custom attributes
     *
     * @param \Magento\Framework\Api\AttributeInterface[] $attributes
     * @return $this
     * @throws \LogicException
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function setCustomAttributes(array $attributes)
    {
        return $this;
    }

    /**
     * Get mapped value
     *
     * @param string $key
     * @return mixed
     */
    private function getMappedValue($key)
    {
        if (!in_array($key, static::$simpleFieldsMap)) {
            return null;
        }

        $val = $this->getTransactionFieldValue($key);
        $val = $this->convertToText($val);
        return $val;
    }

    /**
     * @return array
     */
    private function getMappedValues()
    {
        $result = [];

        foreach (static::$simpleFieldsMap as $key) {
            $val = $this->getTransactionFieldValue($key);
            $val = $this->convertToText($val);
            $result[$key] = $val;
        }

        return $result;
    }

    /**
     * Recursive get transaction field value
     *
     * @param string $key
     * @return Transaction|mixed|null
     */
    private function getTransactionFieldValue($key)
    {
        $keys = explode(self::TRANSACTION_FIELD_MAP_DELIMITER, $key);
        $result = $this->transaction;
        foreach ($keys as $k) {
            if (!isset($result->$k)) {
                $result = null;
                break;
            }
            $result = $result->$k;
        }
        return $result;
    }

    /**
     * Convert value to text representation
     *
     * @param string $val
     * @return string
     */
    private function convertToText($val)
    {
        if (is_object($val)) {
            switch (get_class($val)) {
                case 'DateTime':
                    /** @var DateTime $val */
                    $val = $val->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
            }
        } elseif (is_array($val)) {
            $val = implode(', ', $val);
        }

        return (string) $val;
    }
}