Completed
Push — master ( b14877...e76a9e )
by
unknown
23s queued 11s
created

Index::getResolverCountry()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Pagantis\Pagantis\Controller\Payment;
4
5
use Magento\Framework\App\Action\Action;
6
use Magento\Framework\App\Action\Context;
7
use Magento\Quote\Model\QuoteRepository;
8
use Magento\Sales\Model\ResourceModel\Order\Collection as OrderCollection;
9
use Magento\Checkout\Model\Session;
10
use Pagantis\OrdersApiClient\Model\Order;
11
use Pagantis\Pagantis\Helper\Config;
0 ignored issues
show
Bug introduced by pgarces
This use statement conflicts with another class in this namespace, Pagantis\Pagantis\Controller\Payment\Config. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
use Pagantis\Pagantis\Helper\ExtraConfig;
13
use Magento\Framework\App\ResourceConnection;
14
use Magento\Framework\App\ProductMetadataInterface;
15
use Magento\Framework\Module\ModuleList;
16
use Magento\Store\Api\Data\StoreInterface;
17
use Pagantis\OrdersApiClient\Model\Order\User\Address;
18
use Magento\Framework\DB\Ddl\Table;
19
use Pagantis\OrdersApiClient\Model\Order\User;
20
use Pagantis\OrdersApiClient\Model\Order\User\OrderHistory;
21
use Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details;
22
use Pagantis\OrdersApiClient\Model\Order\ShoppingCart;
23
use Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details\Product;
24
use Pagantis\OrdersApiClient\Model\Order\Metadata;
25
use Pagantis\OrdersApiClient\Model\Order\Configuration\Urls;
26
use Pagantis\OrdersApiClient\Model\Order\Configuration\Channel;
27
use Pagantis\OrdersApiClient\Model\Order\Configuration;
28
use Pagantis\OrdersApiClient\Client;
29
30
/**
31
 * Class Index
32
 * @package Pagantis\Pagantis\Controller\Payment
33
 */
34
class Index extends Action
35
{
36
    /** Orders tablename */
37
    const ORDERS_TABLE = 'cart_process';
38
39
    /** Concurrency tablename */
40
    const LOGS_TABLE = 'Pagantis_logs';
41
42
    /** @var Context $context */
43
    protected $context;
44
45
    /** @var QuoteRepository  $quoteRepository */
46
    protected $quoteRepository;
47
48
    /** @var OrderCollection $orderCollection */
49
    protected $orderCollection;
50
51
    /** @var Session $session */
52
    protected $session;
53
54
    /** @var mixed $config */
55
    protected $config;
56
57
    /** @var ResourceConnection $dbObject */
58
    protected $dbObject;
59
60
    /** @var ProductMetadataInterface $productMetadataInterface */
61
    protected $productMetadataInterface;
62
63
    /** @var ModuleList $moduleList */
64
    protected $moduleList;
65
66
    /** @var ExtraConfig $extraConfig */
67
    protected $extraConfig;
68
    
69
    /** @var StoreInterface $store */
70
    protected $store;
71
72
    /**
73
     * Index constructor.
74
     *
75
     * @param Context                  $context
76
     * @param QuoteRepository          $quoteRepository
77
     * @param OrderCollection          $orderCollection
78
     * @param Session                  $session
79
     * @param Config                   $config
80
     * @param ResourceConnection       $dbObject
81
     * @param ProductMetadataInterface $productMetadataInterface
82
     * @param ModuleList               $moduleList
83
     * @param ExtraConfig              $extraConfig
84
     * @param StoreInterface           $storeInterface
85
     */
86
    public function __construct(
87
        Context $context,
88
        QuoteRepository $quoteRepository,
89
        OrderCollection $orderCollection,
90
        Session $session,
91
        Config $config,
92
        ResourceConnection $dbObject,
93
        ProductMetadataInterface $productMetadataInterface,
94
        ModuleList $moduleList,
95
        ExtraConfig $extraConfig,
96
        StoreInterface $storeInterface
97
    ) {
98
        parent::__construct($context);
99
        $this->session = $session;
100
        $this->context = $context;
101
        $this->config = $config->getConfig();
102
        $this->quoteRepository = $quoteRepository;
103
        $this->orderCollection = $orderCollection;
104
        $this->dbObject = $dbObject;
105
        $this->moduleList = $moduleList;
106
        $this->productMetadataInterface = $productMetadataInterface;
107
        $this->extraConfig = $extraConfig->getExtraConfig();
0 ignored issues
show
Documentation Bug introduced by pgarces
It seems like $extraConfig->getExtraConfig() of type array or array is incompatible with the declared type Pagantis\Pagantis\Helper\ExtraConfig of property $extraConfig.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
108
        $this->store = $storeInterface;
109
    }
110
111
    /**
112
     * Main function
113
     *
114
     * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|void
115
     * @throws \Zend_Db_Exception
116
     */
117
    public function execute()
118
    {
119
        try {
120
            $cancelUrl = $this->_url->getUrl('checkout', ['_fragment' => 'payment']);
121
            $quote = $this->session->getQuote();
122
            /** @var Order $order */
123
            $lastOrder = $this->session->getLastRealOrder();
0 ignored issues
show
Unused Code introduced by pgarces
The assignment to $lastOrder is dead and can be removed.
Loading history...
124
            $params = $this->getRequest()->getParams();
125
            $customer = $quote->getCustomer();
126
            $shippingAddress = $quote->getShippingAddress();
127
128
            if (isset($params['email']) && $params['email']!='') {
129
                $this->session->setEmail($params['email']); //Get guest email after refresh page
130
                $customer->setEmail($params['email']);
0 ignored issues
show
Bug introduced by pgarces
The method setEmail() does not exist on Magento\Framework\Api\ExtensibleDataInterface. It seems like you code against a sub-type of Magento\Framework\Api\ExtensibleDataInterface such as Magento\AsynchronousOperations\Model\AsyncResponse or Magento\Sales\Api\Data\OrderAddressInterface or Magento\AsynchronousOper...s\Model\OperationStatus or Magento\AsynchronousOperations\Model\BulkSummary or Magento\Sales\Model\Order\Shipment\Track or Magento\Store\Model\Website or Magento\CatalogInventory\Model\Stock\Status or Magento\Sales\Model\Order\Creditmemo\Item or Magento\Tax\Model\Sales\Quote\QuoteDetails or Magento\Catalog\Model\ProductLink\Link or Magento\Framework\Model\AbstractExtensibleModel or Magento\Quote\Api\Data\AddressInterface or Magento\Customer\Api\Data\CustomerInterface or Magento\CatalogRule\Model\Rule or Magento\Catalog\Model\Product or Magento\Quote\Model\AddressAdditionalData or Magento\Quote\Model\Cart\TotalsAdditionalData or Magento\Framework\View\E...t\DataProvider\Document or Magento\Checkout\Model\TotalsInformation or Magento\CatalogRule\Model\Data\Condition or Magento\Customer\Model\Data\Customer or Magento\Quote\Model\EstimateAddress or Magento\Catalog\Model\Category or Magento\Eav\Model\Entity...ibute\AbstractAttribute or Magento\Catalog\Model\ResourceModel\Eav\Attribute or Magento\Catalog\Model\Category\Attribute or Magento\Checkout\Model\ShippingInformation or Magento\Tax\Model\Calculation\Rule or Magento\Sales\Model\Order\Status\History or Magento\Store\Model\Group or Magento\Catalog\Model\Product\Option or Magento\Catalog\Model\Product\Gallery\Entry or Magento\Quote\Model\Quote\ProductOption or Magento\Tax\Model\Sales\Quote\ItemDetails or Magento\Tax\Model\TaxDetails\AppliedTax or Magento\Sales\Model\Order\Tax\Item or Magento\Bundle\Model\Option or Magento\Eav\Model\Entity\Attribute\Set or Magento\Quote\Model\Cart\Currency or Magento\Sales\Model\Order\Shipment\Comment or Magento\Catalog\Model\Product\Price\Cost or Magento\Quote\Model\Cart\Totals or Magento\Sales\Model\Order\Invoice\Item or Magento\Downloadable\Model\File\Content or Magento\Catalog\Model\ProductRender\PriceInfo or Magento\CatalogInventory\Model\Stock or Magento\Quote\Model\ShippingAssignment or Magento\Quote\Model\Quote\Payment or Magento\Checkout\Model\PaymentDetails or Magento\Catalog\Model\ProductRender\Image or Magento\Tax\Model\TaxClass\Key or Magento\Sales\Model\Order\Creditmemo\Comment or Magento\Sales\Model\Order\Payment or Magento\Sales\Model\Order\Payment\Transaction or Magento\GiftMessage\Model\Message or Magento\Sales\Model\Order\Shipment\Item or Magento\Catalog\Model\Product\Price\TierPrice or Magento\Catalog\Model\Product\Attribute\Type or Magento\Tax\Model\TaxDetails\ItemDetails or Magento\Store\Model\Store or Magento\Catalog\Model\CustomOptions\CustomOption or Magento\Catalog\Model\Pr...nder\FormattedPriceInfo or Magento\Catalog\Model\ProductRender or Magento\Sales\Model\Order or Magento\Catalog\Model\Product\Option\Type or Magento\Quote\Model\Quote or Magento\Tax\Model\Calculation\Rate or Magento\Catalog\Model\Product\TierPrice or Magento\Tax\Model\Sales\Order\Details or Magento\Framework\Api\Te...erator\ExtensibleSample or Magento\Sales\Model\Order\Invoice\Comment or Magento\Sales\Model\Order\ShippingAssignment or Magento\Msrp\Model\ProductRender\MsrpPriceInfo or Magento\Sales\Model\Order\Item or Magento\Sales\Model\Order\Creditmemo or Magento\Sales\Model\Order\Invoice or Magento\Quote\Model\Shipping or Magento\AsynchronousOperations\Model\Operation or Magento\Catalog\Model\Product\Price\BasePrice or Magento\Downloadable\Model\Sample or Magento\Eav\Model\Entity\Attribute\Group or Magento\Tax\Model\Sales\Order\Tax or Magento\Quote\Model\Cart\TotalSegment or Magento\Downloadable\Model\Link or Magento\CatalogInventory\Model\Stock\Item or Magento\Catalog\Model\Pr...Price\PriceUpdateResult or Magento\Tax\Model\Calculation\Rate\Title or Magento\Sales\Model\Order\Shipment or Magento\Catalog\Model\Product\Price\SpecialPrice or Magento\Tax\Model\ClassModel or Magento\Bundle\Model\Link or Magento\Bundle\Model\BundleOption or Magento\Tax\Model\TaxDetails\AppliedTaxRate or Magento\Bundle\Model\Source\Option\Type or Magento\Sales\Model\Order\Shipping or Magento\Quote\Model\Quote\Item or Magento\SalesRule\Model\Coupon or Magento\Tax\Model\TaxDetails\TaxDetails or Magento\Sales\Model\Order\ShippingTotal or Magento\Catalog\Model\ProductOption or Magento\Catalog\Model\ProductRender\Button. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

130
                $customer->/** @scrutinizer ignore-call */ 
131
                           setEmail($params['email']);
Loading history...
131
                $quote->setCheckoutMethod('guest');
132
                $quote->getBillingAddress()->setEmail($params['email']);
133
            } elseif ($customer->getEmail()=='') {
0 ignored issues
show
Bug introduced by pgarces
The method getEmail() does not exist on Magento\Framework\Api\ExtensibleDataInterface. It seems like you code against a sub-type of Magento\Framework\Api\ExtensibleDataInterface such as Magento\AsynchronousOperations\Model\AsyncResponse or Magento\Sales\Api\Data\OrderAddressInterface or Magento\AsynchronousOper...s\Model\OperationStatus or Magento\AsynchronousOperations\Model\BulkSummary or Magento\Sales\Model\Order\Shipment\Track or Magento\Store\Model\Website or Magento\CatalogInventory\Model\Stock\Status or Magento\Sales\Model\Order\Creditmemo\Item or Magento\Tax\Model\Sales\Quote\QuoteDetails or Magento\Catalog\Model\ProductLink\Link or Magento\Framework\Model\AbstractExtensibleModel or Magento\Quote\Api\Data\AddressInterface or Magento\Customer\Api\Data\CustomerInterface or Magento\CatalogRule\Model\Rule or Magento\Catalog\Model\Product or Magento\Quote\Model\AddressAdditionalData or Magento\Quote\Model\Cart\TotalsAdditionalData or Magento\Framework\View\E...t\DataProvider\Document or Magento\Checkout\Model\TotalsInformation or Magento\CatalogRule\Model\Data\Condition or Magento\Customer\Model\Data\Customer or Magento\Quote\Model\EstimateAddress or Magento\Catalog\Model\Category or Magento\Eav\Model\Entity...ibute\AbstractAttribute or Magento\Catalog\Model\ResourceModel\Eav\Attribute or Magento\Catalog\Model\Category\Attribute or Magento\Checkout\Model\ShippingInformation or Magento\Tax\Model\Calculation\Rule or Magento\Sales\Model\Order\Status\History or Magento\Store\Model\Group or Magento\Catalog\Model\Product\Option or Magento\Catalog\Model\Product\Gallery\Entry or Magento\Quote\Model\Quote\ProductOption or Magento\Tax\Model\Sales\Quote\ItemDetails or Magento\Tax\Model\TaxDetails\AppliedTax or Magento\Sales\Model\Order\Tax\Item or Magento\Bundle\Model\Option or Magento\Eav\Model\Entity\Attribute\Set or Magento\Quote\Model\Cart\Currency or Magento\Sales\Model\Order\Shipment\Comment or Magento\Catalog\Model\Product\Price\Cost or Magento\Quote\Model\Cart\Totals or Magento\Sales\Model\Order\Invoice\Item or Magento\Downloadable\Model\File\Content or Magento\Catalog\Model\ProductRender\PriceInfo or Magento\CatalogInventory\Model\Stock or Magento\Quote\Model\ShippingAssignment or Magento\Quote\Model\Quote\Payment or Magento\Checkout\Model\PaymentDetails or Magento\Catalog\Model\ProductRender\Image or Magento\Tax\Model\TaxClass\Key or Magento\Sales\Model\Order\Creditmemo\Comment or Magento\Sales\Model\Order\Payment or Magento\Sales\Model\Order\Payment\Transaction or Magento\GiftMessage\Model\Message or Magento\Sales\Model\Order\Shipment\Item or Magento\Catalog\Model\Product\Price\TierPrice or Magento\Catalog\Model\Product\Attribute\Type or Magento\Tax\Model\TaxDetails\ItemDetails or Magento\Store\Model\Store or Magento\Catalog\Model\CustomOptions\CustomOption or Magento\Catalog\Model\Pr...nder\FormattedPriceInfo or Magento\Catalog\Model\ProductRender or Magento\Sales\Model\Order or Magento\Catalog\Model\Product\Option\Type or Magento\Quote\Model\Quote or Magento\Tax\Model\Calculation\Rate or Magento\Catalog\Model\Product\TierPrice or Magento\Tax\Model\Sales\Order\Details or Magento\Framework\Api\Te...erator\ExtensibleSample or Magento\Sales\Model\Order\Invoice\Comment or Magento\Sales\Model\Order\ShippingAssignment or Magento\Msrp\Model\ProductRender\MsrpPriceInfo or Magento\Sales\Model\Order\Item or Magento\Sales\Model\Order\Creditmemo or Magento\Sales\Model\Order\Invoice or Magento\Quote\Model\Shipping or Magento\AsynchronousOperations\Model\Operation or Magento\Catalog\Model\Product\Price\BasePrice or Magento\Downloadable\Model\Sample or Magento\Eav\Model\Entity\Attribute\Group or Magento\Tax\Model\Sales\Order\Tax or Magento\Quote\Model\Cart\TotalSegment or Magento\Downloadable\Model\Link or Magento\CatalogInventory\Model\Stock\Item or Magento\Catalog\Model\Pr...Price\PriceUpdateResult or Magento\Tax\Model\Calculation\Rate\Title or Magento\Sales\Model\Order\Shipment or Magento\Catalog\Model\Product\Price\SpecialPrice or Magento\Tax\Model\ClassModel or Magento\Bundle\Model\Link or Magento\Bundle\Model\BundleOption or Magento\Tax\Model\TaxDetails\AppliedTaxRate or Magento\Bundle\Model\Source\Option\Type or Magento\Sales\Model\Order\Shipping or Magento\Quote\Model\Quote\Item or Magento\SalesRule\Model\Coupon or Magento\Tax\Model\TaxDetails\TaxDetails or Magento\Sales\Model\Order\ShippingTotal or Magento\Catalog\Model\ProductOption or Magento\Catalog\Model\ProductRender\Button. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

133
            } elseif ($customer->/** @scrutinizer ignore-call */ getEmail()=='') {
Loading history...
134
                $customer->setEmail($this->session->getEmail());
0 ignored issues
show
Bug introduced by pgarces
It seems like $this->session->getEmail() can also be of type Magento\Checkout\Model\Session; however, parameter $email of Magento\Customer\Api\Dat...erInterface::setEmail() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

134
                $customer->setEmail(/** @scrutinizer ignore-type */ $this->session->getEmail());
Loading history...
135
                $quote->setCheckoutMethod('guest');
136
                $quote->getBillingAddress()->setEmail($this->session->getEmail());
137
            }
138
139
            /** @var Quote $currentQuote */
140
            $currentQuote = $this->quoteRepository->get($quote->getId());
141
            $currentQuote->setCustomerEmail($customer->getEmail());
142
            $this->quoteRepository->save($currentQuote);
143
144
            $userAddress =  new Address();
145
            $userAddress
146
                ->setZipCode($shippingAddress->getPostcode())
147
                ->setFullName($shippingAddress->getFirstname()." ".$shippingAddress->getLastname())
148
                ->setCountryCode($shippingAddress->getCountry())
149
                ->setCity($shippingAddress->getCity())
150
                ->setAddress($shippingAddress->getStreetFull())
151
            ;
152
153
            $tax_id = $this->getTaxId($quote->getBillingAddress());
0 ignored issues
show
Bug introduced by pgarces
Are you sure the assignment to $tax_id is correct as $this->getTaxId($quote->getBillingAddress()) targeting Pagantis\Pagantis\Contro...yment\Index::getTaxId() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
154
            $orderShippingAddress = new Address();
155
            $orderShippingAddress
156
                ->setZipCode($shippingAddress->getPostcode())
157
                ->setFullName($shippingAddress->getFirstname()." ".$shippingAddress->getLastname())
158
                ->setCountryCode($shippingAddress->getCountry())
159
                ->setCity($shippingAddress->getCity())
160
                ->setAddress($shippingAddress->getStreetFull())
161
                ->setFixPhone($shippingAddress->getTelephone())
162
                ->setMobilePhone($shippingAddress->getTelephone())
163
                ->setTaxId($tax_id)
164
            ;
165
166
            $orderBillingAddress =  new Address();
167
            $billingAddress = $quote->getBillingAddress();
168
            $orderBillingAddress
169
                ->setZipCode($billingAddress->getPostcode())
170
                ->setFullName($billingAddress->getFirstname()." ".$shippingAddress->getLastname())
171
                ->setCountryCode($billingAddress->getCountry())
172
                ->setCity($billingAddress->getCity())
173
                ->setAddress($billingAddress->getStreetFull())
174
                ->setFixPhone($billingAddress->getTelephone())
175
                ->setMobilePhone($billingAddress->getTelephone())
176
                ->setTaxId($tax_id)
177
            ;
178
179
            $orderUser = new User();
180
            $billingAddress->setEmail($customer->getEmail());
181
            $orderUser
182
                ->setAddress($userAddress)
183
                ->setFullName($shippingAddress->getFirstname()." ".$shippingAddress->getLastname())
184
                ->setBillingAddress($orderBillingAddress)
185
                ->setEmail($customer->getEmail())
186
                ->setFixPhone($shippingAddress->getTelephone())
187
                ->setMobilePhone($shippingAddress->getTelephone())
188
                ->setShippingAddress($orderShippingAddress)
189
                ->setTaxId($tax_id)
190
            ;
191
192
            if ($customer->getDob()) {
0 ignored issues
show
Bug introduced by pgarces
The method getDob() does not exist on Magento\Framework\Api\ExtensibleDataInterface. It seems like you code against a sub-type of Magento\Framework\Api\ExtensibleDataInterface such as Magento\AsynchronousOperations\Model\AsyncResponse or Magento\AsynchronousOper...s\Model\OperationStatus or Magento\AsynchronousOperations\Model\BulkSummary or Magento\Sales\Model\Order\Shipment\Track or Magento\Store\Model\Website or Magento\CatalogInventory\Model\Stock\Status or Magento\Sales\Model\Order\Creditmemo\Item or Magento\Tax\Model\Sales\Quote\QuoteDetails or Magento\Catalog\Model\ProductLink\Link or Magento\Framework\Model\AbstractExtensibleModel or Magento\Customer\Api\Data\CustomerInterface or Magento\CatalogRule\Model\Rule or Magento\Catalog\Model\Product or Magento\Quote\Model\AddressAdditionalData or Magento\Quote\Model\Quote\Address or Magento\Quote\Model\Cart\TotalsAdditionalData or Magento\Framework\View\E...t\DataProvider\Document or Magento\Checkout\Model\TotalsInformation or Magento\CatalogRule\Model\Data\Condition or Magento\Customer\Model\Data\Customer or Magento\Quote\Model\EstimateAddress or Magento\Catalog\Model\Category or Magento\Eav\Model\Entity...ibute\AbstractAttribute or Magento\Catalog\Model\ResourceModel\Eav\Attribute or Magento\Catalog\Model\Category\Attribute or Magento\Checkout\Model\ShippingInformation or Magento\Tax\Model\Calculation\Rule or Magento\Sales\Model\Order\Status\History or Magento\Store\Model\Group or Magento\Catalog\Model\Product\Option or Magento\Catalog\Model\Product\Gallery\Entry or Magento\Quote\Model\Quote\ProductOption or Magento\Tax\Model\Sales\Quote\ItemDetails or Magento\Tax\Model\TaxDetails\AppliedTax or Magento\Sales\Model\Order\Tax\Item or Magento\Bundle\Model\Option or Magento\Eav\Model\Entity\Attribute\Set or Magento\Quote\Model\Cart\Currency or Magento\Sales\Model\Order\Shipment\Comment or Magento\Catalog\Model\Product\Price\Cost or Magento\Quote\Model\Cart\Totals or Magento\Sales\Model\Order\Invoice\Item or Magento\Downloadable\Model\File\Content or Magento\Catalog\Model\ProductRender\PriceInfo or Magento\CatalogInventory\Model\Stock or Magento\Quote\Model\ShippingAssignment or Magento\Quote\Model\Quote\Payment or Magento\Checkout\Model\PaymentDetails or Magento\Catalog\Model\ProductRender\Image or Magento\Sales\Model\Order\Address or Magento\Tax\Model\TaxClass\Key or Magento\Sales\Model\Order\Creditmemo\Comment or Magento\Sales\Model\Order\Payment or Magento\Sales\Model\Order\Payment\Transaction or Magento\GiftMessage\Model\Message or Magento\Sales\Model\Order\Shipment\Item or Magento\Catalog\Model\Product\Price\TierPrice or Magento\Catalog\Model\Product\Attribute\Type or Magento\Tax\Model\TaxDetails\ItemDetails or Magento\Store\Model\Store or Magento\Catalog\Model\CustomOptions\CustomOption or Magento\Catalog\Model\Pr...nder\FormattedPriceInfo or Magento\Catalog\Model\ProductRender or Magento\Sales\Model\Order or Magento\Catalog\Model\Product\Option\Type or Magento\Quote\Model\Quote or Magento\Tax\Model\Calculation\Rate or Magento\Catalog\Model\Product\TierPrice or Magento\Tax\Model\Sales\Order\Details or Magento\Framework\Api\Te...erator\ExtensibleSample or Magento\Sales\Model\Order\Invoice\Comment or Magento\Sales\Model\Order\ShippingAssignment or Magento\Msrp\Model\ProductRender\MsrpPriceInfo or Magento\Sales\Model\Order\Item or Magento\Sales\Model\Order\Creditmemo or Magento\Sales\Model\Order\Invoice or Magento\Quote\Model\Shipping or Magento\AsynchronousOperations\Model\Operation or Magento\Catalog\Model\Product\Price\BasePrice or Magento\Downloadable\Model\Sample or Magento\Eav\Model\Entity\Attribute\Group or Magento\Tax\Model\Sales\Order\Tax or Magento\Quote\Model\Cart\TotalSegment or Magento\Downloadable\Model\Link or Magento\CatalogInventory\Model\Stock\Item or Magento\Catalog\Model\Pr...Price\PriceUpdateResult or Magento\Tax\Model\Calculation\Rate\Title or Magento\Sales\Model\Order\Shipment or Magento\Catalog\Model\Product\Price\SpecialPrice or Magento\Tax\Model\ClassModel or Magento\Bundle\Model\Link or Magento\Bundle\Model\BundleOption or Magento\Tax\Model\TaxDetails\AppliedTaxRate or Magento\Bundle\Model\Source\Option\Type or Magento\Sales\Model\Order\Shipping or Magento\Quote\Model\Quote\Item or Magento\SalesRule\Model\Coupon or Magento\Tax\Model\TaxDetails\TaxDetails or Magento\Sales\Model\Order\ShippingTotal or Magento\Catalog\Model\ProductOption or Magento\Catalog\Model\ProductRender\Button. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

192
            if ($customer->/** @scrutinizer ignore-call */ getDob()) {
Loading history...
193
                $orderUser->setDateOfBirth($customer->getDob());
194
            }
195
            if ($customer->getTaxvat()!='') {
0 ignored issues
show
Bug introduced by pgarces
The method getTaxvat() does not exist on Magento\Framework\Api\ExtensibleDataInterface. It seems like you code against a sub-type of Magento\Framework\Api\ExtensibleDataInterface such as Magento\AsynchronousOperations\Model\AsyncResponse or Magento\AsynchronousOper...s\Model\OperationStatus or Magento\AsynchronousOperations\Model\BulkSummary or Magento\Sales\Model\Order\Shipment\Track or Magento\Store\Model\Website or Magento\CatalogInventory\Model\Stock\Status or Magento\Sales\Model\Order\Creditmemo\Item or Magento\Tax\Model\Sales\Quote\QuoteDetails or Magento\Catalog\Model\ProductLink\Link or Magento\Framework\Model\AbstractExtensibleModel or Magento\Customer\Api\Data\CustomerInterface or Magento\CatalogRule\Model\Rule or Magento\Catalog\Model\Product or Magento\Quote\Model\AddressAdditionalData or Magento\Quote\Model\Quote\Address or Magento\Quote\Model\Cart\TotalsAdditionalData or Magento\Framework\View\E...t\DataProvider\Document or Magento\Checkout\Model\TotalsInformation or Magento\CatalogRule\Model\Data\Condition or Magento\Customer\Model\Data\Customer or Magento\Quote\Model\EstimateAddress or Magento\Catalog\Model\Category or Magento\Eav\Model\Entity...ibute\AbstractAttribute or Magento\Catalog\Model\ResourceModel\Eav\Attribute or Magento\Catalog\Model\Category\Attribute or Magento\Checkout\Model\ShippingInformation or Magento\Tax\Model\Calculation\Rule or Magento\Sales\Model\Order\Status\History or Magento\Store\Model\Group or Magento\Catalog\Model\Product\Option or Magento\Catalog\Model\Product\Gallery\Entry or Magento\Quote\Model\Quote\ProductOption or Magento\Tax\Model\Sales\Quote\ItemDetails or Magento\Tax\Model\TaxDetails\AppliedTax or Magento\Sales\Model\Order\Tax\Item or Magento\Bundle\Model\Option or Magento\Eav\Model\Entity\Attribute\Set or Magento\Quote\Model\Cart\Currency or Magento\Sales\Model\Order\Shipment\Comment or Magento\Catalog\Model\Product\Price\Cost or Magento\Quote\Model\Cart\Totals or Magento\Sales\Model\Order\Invoice\Item or Magento\Downloadable\Model\File\Content or Magento\Catalog\Model\ProductRender\PriceInfo or Magento\CatalogInventory\Model\Stock or Magento\Quote\Model\ShippingAssignment or Magento\Quote\Model\Quote\Payment or Magento\Checkout\Model\PaymentDetails or Magento\Catalog\Model\ProductRender\Image or Magento\Sales\Model\Order\Address or Magento\Tax\Model\TaxClass\Key or Magento\Sales\Model\Order\Creditmemo\Comment or Magento\Sales\Model\Order\Payment or Magento\Sales\Model\Order\Payment\Transaction or Magento\GiftMessage\Model\Message or Magento\Sales\Model\Order\Shipment\Item or Magento\Catalog\Model\Product\Price\TierPrice or Magento\Catalog\Model\Product\Attribute\Type or Magento\Tax\Model\TaxDetails\ItemDetails or Magento\Store\Model\Store or Magento\Catalog\Model\CustomOptions\CustomOption or Magento\Catalog\Model\Pr...nder\FormattedPriceInfo or Magento\Catalog\Model\ProductRender or Magento\Sales\Model\Order or Magento\Catalog\Model\Product\Option\Type or Magento\Quote\Model\Quote or Magento\Tax\Model\Calculation\Rate or Magento\Catalog\Model\Product\TierPrice or Magento\Tax\Model\Sales\Order\Details or Magento\Framework\Api\Te...erator\ExtensibleSample or Magento\Sales\Model\Order\Invoice\Comment or Magento\Sales\Model\Order\ShippingAssignment or Magento\Msrp\Model\ProductRender\MsrpPriceInfo or Magento\Sales\Model\Order\Item or Magento\Sales\Model\Order\Creditmemo or Magento\Sales\Model\Order\Invoice or Magento\Quote\Model\Shipping or Magento\AsynchronousOperations\Model\Operation or Magento\Catalog\Model\Product\Price\BasePrice or Magento\Downloadable\Model\Sample or Magento\Eav\Model\Entity\Attribute\Group or Magento\Tax\Model\Sales\Order\Tax or Magento\Quote\Model\Cart\TotalSegment or Magento\Downloadable\Model\Link or Magento\CatalogInventory\Model\Stock\Item or Magento\Catalog\Model\Pr...Price\PriceUpdateResult or Magento\Tax\Model\Calculation\Rate\Title or Magento\Sales\Model\Order\Shipment or Magento\Catalog\Model\Product\Price\SpecialPrice or Magento\Tax\Model\ClassModel or Magento\Bundle\Model\Link or Magento\Bundle\Model\BundleOption or Magento\Tax\Model\TaxDetails\AppliedTaxRate or Magento\Bundle\Model\Source\Option\Type or Magento\Sales\Model\Order\Shipping or Magento\Quote\Model\Quote\Item or Magento\SalesRule\Model\Coupon or Magento\Tax\Model\TaxDetails\TaxDetails or Magento\Sales\Model\Order\ShippingTotal or Magento\Catalog\Model\ProductOption or Magento\Catalog\Model\ProductRender\Button. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

195
            if ($customer->/** @scrutinizer ignore-call */ getTaxvat()!='') {
Loading history...
196
                $orderUser->setDni($customer->getTaxvat());
0 ignored issues
show
Bug introduced by pgarces
The method setDni() does not exist on Pagantis\OrdersApiClient\Model\Order\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

196
                $orderUser->/** @scrutinizer ignore-call */ 
197
                            setDni($customer->getTaxvat());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
197
                $orderBillingAddress->setDni($customer->getTaxvat());
0 ignored issues
show
Bug introduced by pgarces
The method setDni() does not exist on Pagantis\OrdersApiClient\Model\Order\User\Address. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

197
                $orderBillingAddress->/** @scrutinizer ignore-call */ 
198
                                      setDni($customer->getTaxvat());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
198
                $orderShippingAddress->setDni($customer->getTaxvat());
199
                $orderUser->setNationalId($customer->getTaxvat());
200
                $orderBillingAddress->setNationalId($customer->getTaxvat());
201
                $orderShippingAddress->setNationalId($customer->getTaxvat());
202
            }
203
204
            $previousOrders = $this->getOrders($customer->getId());
0 ignored issues
show
Bug introduced by pgarces
The method getId() does not exist on Magento\Framework\Api\ExtensibleDataInterface. It seems like you code against a sub-type of Magento\Framework\Api\ExtensibleDataInterface such as Magento\Store\Api\Data\WebsiteInterface or Magento\AsynchronousOperations\Model\AsyncResponse or Magento\Tax\Api\Data\TaxRuleInterface or Magento\Store\Api\Data\GroupInterface or Magento\Customer\Api\Data\GroupInterface or Magento\Catalog\Api\Data...iaGalleryEntryInterface or Magento\Directory\Api\Da...tryInformationInterface or Magento\Store\Api\Data\StoreInterface or Magento\Catalog\Api\Data\ProductRenderInterface or Magento\Quote\Api\Data\CartInterface or Magento\Store\Api\Data\StoreConfigInterface or Magento\Tax\Api\Data\TaxRateInterface or Magento\Framework\Bulk\OperationInterface or Magento\Downloadable\Api\Data\SampleInterface or Magento\Directory\Api\Da...ionInformationInterface or Magento\Downloadable\Api\Data\LinkInterface or Magento\AsynchronousOper...s\Model\OperationStatus or Magento\Bundle\Api\Data\LinkInterface or Magento\AsynchronousOperations\Model\BulkSummary or Magento\Sales\Model\Order\Shipment\Track or Magento\CatalogInventory\Model\Stock\Status or Magento\Sales\Model\Order\Creditmemo\Item or Magento\Tax\Model\Sales\Quote\QuoteDetails or Magento\Catalog\Model\ProductLink\Link or Magento\Framework\Model\AbstractExtensibleModel or Magento\Catalog\Api\Data\ProductInterface or Magento\Quote\Api\Data\AddressInterface or Magento\Customer\Api\Data\CustomerInterface or Magento\Framework\Api\Search\DocumentInterface or Magento\Catalog\Api\Data\CategoryInterface or Magento\Customer\Api\Data\AddressInterface or Magento\CatalogRule\Model\Rule or Magento\Quote\Model\AddressAdditionalData or Magento\Quote\Model\Cart\TotalsAdditionalData or Magento\Checkout\Model\TotalsInformation or Magento\CatalogRule\Model\Data\Condition or Magento\Store\Model\Data\StoreConfig or Magento\Customer\Model\Data\Customer or Magento\Directory\Model\Data\RegionInformation or Magento\Customer\Model\Data\Group or Magento\Directory\Model\Data\CountryInformation or Magento\Customer\Model\Data\Address or Magento\Quote\Model\EstimateAddress or Magento\Eav\Model\Entity...ibute\AbstractAttribute or Magento\Catalog\Model\ResourceModel\Eav\Attribute or Magento\Catalog\Model\Category\Attribute or Magento\Checkout\Model\ShippingInformation or Magento\Sales\Model\Order\Status\History or Magento\Catalog\Model\Product\Option or Magento\Quote\Model\Quote\ProductOption or Magento\Tax\Model\Sales\Quote\ItemDetails or Magento\Tax\Model\TaxDetails\AppliedTax or Magento\Sales\Model\Order\Tax\Item or Magento\Bundle\Model\Option or Magento\Eav\Model\Entity\Attribute\Set or Magento\Quote\Model\Cart\Currency or Magento\Sales\Model\Order\Shipment\Comment or Magento\Catalog\Model\Product\Price\Cost or Magento\Quote\Model\Cart\Totals or Magento\Sales\Model\Order\Invoice\Item or Magento\Downloadable\Model\File\Content or Magento\Catalog\Model\ProductRender\PriceInfo or Magento\CatalogInventory\Model\Stock or Magento\Quote\Model\ShippingAssignment or Magento\Quote\Model\Quote\Payment or Magento\Checkout\Model\PaymentDetails or Magento\Catalog\Model\ProductRender\Image or Magento\Sales\Model\Order\Address or Magento\Tax\Model\TaxClass\Key or Magento\Sales\Model\Order\Creditmemo\Comment or Magento\Sales\Model\Order\Payment or Magento\Sales\Model\Order\Payment\Transaction or Magento\GiftMessage\Model\Message or Magento\Sales\Model\Order\Shipment\Item or Magento\Catalog\Model\Product\Price\TierPrice or Magento\Catalog\Model\Product\Attribute\Type or Magento\Tax\Model\TaxDetails\ItemDetails or Magento\Catalog\Model\CustomOptions\CustomOption or Magento\Catalog\Model\Pr...nder\FormattedPriceInfo or Magento\Sales\Model\Order or Magento\Catalog\Model\Product\Option\Type or Magento\Catalog\Model\Product\TierPrice or Magento\Tax\Model\Sales\Order\Details or Magento\Framework\Api\Te...erator\ExtensibleSample or Magento\Sales\Model\Order\Invoice\Comment or Magento\Sales\Model\Order\ShippingAssignment or Magento\Msrp\Model\ProductRender\MsrpPriceInfo or Magento\Sales\Model\Order\Item or Magento\Sales\Model\Order\Creditmemo or Magento\Sales\Model\Order\Invoice or Magento\Quote\Model\Shipping or Magento\Catalog\Model\Product\Price\BasePrice or Magento\Eav\Model\Entity\Attribute\Group or Magento\Tax\Model\Sales\Order\Tax or Magento\Quote\Model\Cart\TotalSegment or Magento\CatalogInventory\Model\Stock\Item or Magento\Catalog\Model\Pr...Price\PriceUpdateResult or Magento\Tax\Model\Calculation\Rate\Title or Magento\Sales\Model\Order\Shipment or Magento\Catalog\Model\Product\Price\SpecialPrice or Magento\Tax\Model\ClassModel or Magento\Bundle\Model\BundleOption or Magento\Tax\Model\TaxDetails\AppliedTaxRate or Magento\Bundle\Model\Source\Option\Type or Magento\Sales\Model\Order\Shipping or Magento\Quote\Model\Quote\Item or Magento\SalesRule\Model\Coupon or Magento\Tax\Model\TaxDetails\TaxDetails or Magento\Sales\Model\Order\ShippingTotal or Magento\Catalog\Model\ProductOption or Magento\Catalog\Model\ProductRender\Button. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

204
            $previousOrders = $this->getOrders($customer->/** @scrutinizer ignore-call */ getId());
Loading history...
205
            foreach ($previousOrders as $orderElement) {
206
                $orderHistory = new OrderHistory();
207
                $orderHistory
208
                    ->setAmount(intval(100 * $orderElement['grand_total']))
209
                    ->setDate(new \DateTime($orderElement['created_at']))
0 ignored issues
show
Bug introduced by pgarces
new DateTime($orderElement['created_at']) of type DateTime is incompatible with the type string expected by parameter $date of Pagantis\OrdersApiClient...OrderHistory::setDate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

209
                    ->setDate(/** @scrutinizer ignore-type */ new \DateTime($orderElement['created_at']))
Loading history...
210
                ;
211
                $orderUser->addOrderHistory($orderHistory);
212
            }
213
214
            $metadataOrder = new Metadata();
215
            $metadata = $this->getMetadata();
216
            foreach ($metadata as $key => $metadatum) {
217
                $metadataOrder->addMetadata($key, $metadatum);
218
            }
219
220
            $details = new Details();
221
            $shippingCost = $quote->collectTotals()->getTotals()['shipping']->getData('value');
222
            $details->setShippingCost(intval(strval(100 * $shippingCost)));
223
            $items = $quote->getAllVisibleItems();
224
            $promotedAmount = 0;
225
            foreach ($items as $key => $item) {
226
                $product = new Product();
227
                $product
228
                    ->setAmount(intval(100 * $item->getPrice()))
229
                    ->setQuantity($item->getQty())
230
                    ->setDescription($item->getName());
231
                $details->addProduct($product);
232
233
                $promotedProduct = $this->isPromoted($item);
234
                if ($promotedProduct == 'true') {
235
                    $promotedAmount+=$product->getAmount()*$item->getQty();
236
                    $promotedMessage = 'Promoted Item: ' . $item->getName() .
237
                                       ' Price: ' . $item->getPrice() .
238
                                       ' Qty: ' . $item->getQty() .
239
                                       ' Item ID: ' . $item->getItemId();
240
                    $metadataOrder->addMetadata('promotedProduct', $promotedMessage);
241
                }
242
            }
243
244
            $orderShoppingCart = new ShoppingCart();
245
            $orderShoppingCart
246
                ->setDetails($details)
247
                ->setOrderReference($quote->getId())
248
                ->setPromotedAmount(0)
249
                ->setTotalAmount(intval(strval(100 * $quote->getGrandTotal())))
250
            ;
251
252
            $orderConfigurationUrls = new Urls();
253
            $quoteId = $quote->getId();
254
            $okUrl = $this->_url->getUrl(
255
                'pagantis/notify/index',
256
                ['_query' => ['quoteId'=>$quoteId]]
257
            );
258
            if (version_compare($metadata['magento'], '2.3.0') >= 0) {
259
                $okUrl = $this->_url->getUrl('pagantis/notify/indexV2', ['_query' => ['quoteId'=>$quoteId]]);
260
            }
261
262
            $orderConfigurationUrls
263
                ->setCancel($cancelUrl)
264
                ->setKo($okUrl)
265
                ->setAuthorizedNotificationCallback($okUrl)
266
                ->setRejectedNotificationCallback($okUrl)
267
                ->setOk($okUrl)
268
            ;
269
270
            $orderChannel = new Channel();
271
            $orderChannel
272
                ->setAssistedSale(false)
273
                ->setType(Channel::ONLINE)
274
            ;
275
276
            $haystack  = ($this->store->getLocale()!=null) ? $this->store->getLocale() : $this->getResolverCountry();
277
            $language = strstr($haystack, '_', true);
278
            $orderConfiguration = new Configuration();
279
            $orderConfiguration
280
                ->setChannel($orderChannel)
281
                ->setUrls($orderConfigurationUrls)
282
                ->setPurchaseCountry($language)
283
            ;
284
285
286
            $order = new Order();
287
            $order
288
                ->setConfiguration($orderConfiguration)
289
                ->setMetadata($metadataOrder)
290
                ->setShoppingCart($orderShoppingCart)
291
                ->setUser($orderUser)
292
            ;
293
294
            if ($this->config['pagantis_public_key']=='' || $this->config['pagantis_private_key']=='') {
295
                throw new \Exception('Public and Secret Key not found');
296
            }
297
298
            $orderClient = new Client(
299
                $this->config['pagantis_public_key'],
300
                $this->config['pagantis_private_key']
301
            );
302
303
            $order = $orderClient->createOrder($order);
304
            if ($order instanceof Order) {
0 ignored issues
show
introduced by pgarces
$order is always a sub-type of Pagantis\OrdersApiClient\Model\Order.
Loading history...
305
                $url = $order->getActionUrls()->getForm();
306
                $result = $this->insertRow($quote->getId(), $order->getId());
307
                if (!$result) {
308
                    throw new \Exception('Unable to save pagantis-order-id');
309
                }
310
            } else {
311
                throw new \Exception('Order not created');
312
            }
313
        } catch (\Exception $exception) {
314
            $this->insertLog($exception);
315
            echo $cancelUrl;
316
            exit;
0 ignored issues
show
Best Practice introduced by pgarces
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
317
        }
318
319
        $displayMode = $this->extraConfig['PAGANTIS_FORM_DISPLAY_TYPE'];
320
        if ($displayMode==='0') {
321
            echo $url;
322
            exit;
0 ignored issues
show
Best Practice introduced by pgarces
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
323
        } else {
324
            $iframeUrl = $this->_url->getUrl(
325
                "pagantis/Payment/iframe",
326
                ['_query' => ["orderId"=>$order->getId()]]
327
            );
328
            echo $iframeUrl;
329
            exit;
0 ignored issues
show
Best Practice introduced by pgarces
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
330
        }
331
    }
332
333
    /**
334
     * Get the orders of a customer
335
     * @param $customerId
336
     *
337
     * @return array
338
     */
339
    private function getOrders($customerId)
340
    {
341
        $orderCollection = array();
342
        if ($customerId!='') {
343
            $this->orderCollection->addAttributeToFilter('customer_id', $customerId)
344
                            ->addAttributeToFilter(
345
                                'status',
346
                                ['in' => ['processing','pending','complete']]
347
                            )
348
                            ->load();
349
            $orderCollection = $this->orderCollection->getData();
350
        }
351
        return $orderCollection;
352
    }
353
354
    /**
355
     * @return void|\Zend_Db_Statement_Interface
356
     * @throws \Zend_Db_Exception
357
     */
358
    private function checkDbTable()
359
    {
360
        $dbConnection = $this->dbObject->getConnection();
361
        $tableName = $this->dbObject->getTableName(self::ORDERS_TABLE);
362
        if (!$dbConnection->isTableExists($tableName)) {
363
            $table = $dbConnection
364
                ->newTable($tableName)
365
                ->addColumn('id', Table::TYPE_SMALLINT, null, array('primary'=>true, 'nullable' => false))
366
                ->addColumn('order_id', Table::TYPE_TEXT, 50)
367
                ->addColumn('mg_order_id', Table::TYPE_TEXT, 50);
368
            return $dbConnection->createTable($table);
369
        }
370
371
        return;
372
    }
373
374
    /**
375
     * Create relationship between quote_id & Pagantis_order_id
376
     * @param $quoteId
377
     * @param $pagantisOrderId
378
     *
379
     * @return int
380
     * @throws \Zend_Db_Exception
381
     */
382
    private function insertRow($quoteId, $pagantisOrderId)
383
    {
384
        $this->checkDbTable();
385
        $dbConnection = $this->dbObject->getConnection();
386
        $tableName = $this->dbObject->getTableName(self::ORDERS_TABLE);
387
        return $dbConnection->insertOnDuplicate(
388
            $tableName,
389
            array('id'=>$quoteId,'order_id'=>$pagantisOrderId),
390
            array('order_id')
391
        );
392
    }
393
394
    /**
395
     * @return array
396
     */
397
    private function getMetadata()
398
    {
399
        $curlInfo = curl_version();
400
        $curlVersion = $curlInfo['version'];
401
        $magentoVersion = $this->productMetadataInterface->getVersion();
402
        $moduleInfo = $this->moduleList->getOne('Pagantis_Pagantis');
403
        return array(  'magento' => $magentoVersion,
404
                       'pagantis' => $moduleInfo['setup_version'],
405
                       'php' => phpversion(),
406
                       'curl' => $curlVersion);
407
    }
408
409
    /**
410
     * Check if log table exists, otherwise create it
411
     *
412
     * @return void|\Zend_Db_Statement_Interface
413
     * @throws \Zend_Db_Exception
414
     */
415
    private function checkDbLogTable()
416
    {
417
        /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */
418
        $dbConnection = $this->dbObject->getConnection();
419
        $tableName = $this->dbObject->getTableName(self::LOGS_TABLE);
420
        if (!$dbConnection->isTableExists($tableName)) {
421
            $table = $dbConnection
422
                ->newTable($tableName)
423
                ->addColumn('id', Table::TYPE_SMALLINT, null, array('nullable'=>false, 'auto_increment'=>true, 'primary'=>true))
424
                ->addColumn('log', Table::TYPE_TEXT, null, array('nullable'=>false))
425
                ->addColumn('createdAt', Table::TYPE_TIMESTAMP, null, array('nullable'=>false, 'default'=>Table::TIMESTAMP_INIT));
426
            return $dbConnection->createTable($table);
427
        }
428
429
        return;
430
    }
431
432
    /**
433
     * @param $exceptionMessage
434
     *
435
     * @throws \Zend_Db_Exception
436
     */
437
    private function insertLog($exceptionMessage)
438
    {
439
        if ($exceptionMessage instanceof \Exception) {
440
            $this->checkDbLogTable();
441
            $logObject          = new \stdClass();
442
            $logObject->message = $exceptionMessage->getMessage();
443
            $logObject->code    = $exceptionMessage->getCode();
444
            $logObject->line    = $exceptionMessage->getLine();
445
            $logObject->file    = $exceptionMessage->getFile();
446
            $logObject->trace   = $exceptionMessage->getTraceAsString();
447
448
            /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */
449
            $dbConnection = $this->dbObject->getConnection();
450
            $tableName    = $this->dbObject->getTableName(self::LOGS_TABLE);
451
            $dbConnection->insert($tableName, array('log' => json_encode($logObject)));
452
        }
453
    }
454
455
    /**
456
     * @param $billingAdd
457
     *
458
     * @return null
459
     */
460
    private function getTaxId($billingAdd)
461
    {
462
        if (isset($billingAdd['vat_id'])) {
463
            return $billingAdd['vat_id'];
464
        } elseif (isset($billingAdd['cod_fisc'])) {
465
            return $billingAdd['cod_fisc'];
466
        } else {
467
            return null;
468
        }
469
    }
470
471
    /**
472
     * @param $item
473
     *
474
     * @return string
475
     */
476
    private function isPromoted($item)
477
    {
478
        $magentoProductId = $item->getProductId();
479
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
480
        $product = $objectManager->create('Magento\Catalog\Model\Product')->load($magentoProductId);
481
        return ($product->getData('pagantis_promoted') === '1') ? 'true' : 'false';
482
    }
483
484
    /**
485
     * @return mixed
486
     */
487
    private function getResolverCountry()
488
    {
489
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
490
        $store = $objectManager->get('Magento\Framework\Locale\Resolver');
491
492
        if (method_exists($store, 'getLocale')) {
493
            return $store->getLocale();
494
        }
495
496
        return null;
497
    }
498
}
499