GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( db9143...ea09f8 )
by Andreas
03:37
created

OrderBuilder::formatDateTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 */
5
6
namespace CommerceLeague\ActiveCampaign\Gateway\Request;
7
8
use CommerceLeague\ActiveCampaign\Api\CustomerRepositoryInterface;
9
use CommerceLeague\ActiveCampaign\Helper\Config as ConfigHelper;
10
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
11
use Magento\Sales\Api\Data\OrderInterface as MagentoOrderInterface;
12
use Magento\Sales\Model\Order as MagentoOrder;
13
14
/**
15
 * Class OrderBuilder
16
 */
17
class OrderBuilder
18
{
19
    /**
20
     * @var ConfigHelper
21
     */
22
    private $configHelper;
23
24
    /**
25
     * @var CustomerRepositoryInterface
26
     */
27
    private $customerRepository;
28
29
    /**
30
     * @var TimezoneInterface
31
     */
32
    private $timezone;
33
34
    /**
35
     * @param ConfigHelper $configHelper
36
     * @param CustomerRepositoryInterface $customerRepository
37
     * @param TimezoneInterface $timezone
38
     */
39
    public function __construct(
40
        ConfigHelper $configHelper,
41
        CustomerRepositoryInterface $customerRepository,
42
        TimezoneInterface $timezone
43
    ) {
44
        $this->configHelper = $configHelper;
45
        $this->customerRepository = $customerRepository;
46
        $this->timezone = $timezone;
47
    }
48
49
    /**
50
     * @param MagentoOrderInterface|MagentoOrder $magentoOrder
51
     * @return array
52
     * @throws \Exception
53
     */
54
    public function build(MagentoOrderInterface $magentoOrder): array
55
    {
56
        $customer = $this->customerRepository->getByMagentoCustomerId($magentoOrder->getCustomerId());
57
58
        $request = [
59
            'externalid' => $magentoOrder->getId(),
60
            'source' => 1,
61
            'email' => $magentoOrder->getCustomerEmail(),
62
            'orderNumber' => $magentoOrder->getIncrementId(),
63
            'orderDate' => $this->formatDateTime($magentoOrder->getCreatedAt()),
0 ignored issues
show
Bug introduced by
It seems like $magentoOrder->getCreatedAt() can also be of type null; however, parameter $date of CommerceLeague\ActiveCam...ilder::formatDateTime() 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

63
            'orderDate' => $this->formatDateTime(/** @scrutinizer ignore-type */ $magentoOrder->getCreatedAt()),
Loading history...
64
            'shippingMethod' => $magentoOrder->getShippingMethod(),
0 ignored issues
show
Bug introduced by
The method getShippingMethod() does not exist on Magento\Sales\Api\Data\OrderInterface. Did you maybe mean getShippingAmount()? ( Ignorable by Annotation )

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

64
            'shippingMethod' => $magentoOrder->/** @scrutinizer ignore-call */ getShippingMethod(),

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...
65
            'totalPrice' => $this->convertToCent((float)$magentoOrder->getGrandTotal()),
66
            'currency' => $magentoOrder->getBaseCurrencyCode(),
67
            'connectionid' => $this->configHelper->getConnectionId(),
68
            'customerid' => $customer->getActiveCampaignId(),
69
            'orderProducts' => []
70
        ];
71
72
        /** @var MagentoOrder\Item $magentoOrderItem */
73
        foreach ($magentoOrder->getAllVisibleItems() as $magentoOrderItem) {
74
            $request['orderProducts'][] = [
75
                'externalid' => $magentoOrderItem->getSku(),
76
                'name' => $magentoOrderItem->getName(),
77
                'price' => $this->convertToCent((float)$magentoOrderItem->getPriceInclTax()),
78
                'quantity' => (int)$magentoOrderItem->getQtyOrdered()
79
            ];
80
        }
81
82
        return $request;
83
    }
84
85
    /**
86
     * @param float $amount
87
     * @return int
88
     */
89
    private function convertToCent(float $amount): int
90
    {
91
        return (int)($amount * 100);
92
    }
93
94
    /**
95
     * @param string $date
96
     * @return string
97
     * @throws \Exception
98
     */
99
    private function formatDateTime(string $date): string
100
    {
101
        return (new \DateTime($date))->format(\DateTime::W3C);
102
    }
103
}
104