Passed
Push[email protected] ( 356814...3dff17 )
by Bruno
20:21 queued 18:30
created

Delete::deleteWebhook()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 14
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 20
rs 9.7998
1
<?php
2
/**
3
 * Copyright © Wirecard Brasil. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See COPYING.txt for license details.
7
 */
8
9
declare(strict_types=1);
10
11
namespace Moip\Magento2\Model\Console\Command\Preference;
12
13
use Exception;
14
use Magento\Framework\App\Config\ScopeConfigInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Config\ScopeConfigInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Magento\Framework\App\State;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\State was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Magento\Framework\HTTP\ZendClient;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\HTTP\ZendClient was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Magento\Framework\HTTP\ZendClientFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\HTTP\ZendClientFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Magento\Framework\Serialize\Serializer\Json;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Serialize\Serializer\Json was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use Moip\Magento2\Gateway\Config\Config as MoipConfig;
20
use Moip\Magento2\Model\Console\Command\AbstractModel;
21
use Psr\Log\LoggerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
23
/**
24
 * Class Delete Preference Webhook.
25
 */
26
class Delete extends AbstractModel
27
{
28
    /**
29
     * State.
30
     *
31
     * @var \Magento\Framework\App\State
32
     */
33
    private $state;
34
35
    /**
36
     * ScopeConfigInterface.
37
     *
38
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
39
     */
40
    protected $scopeConfig;
41
42
    /**
43
     * moipConfig.
44
     *
45
     * @var Moip\Magento2\Gateway\Config\Config
0 ignored issues
show
Bug introduced by
The type Moip\Magento2\Model\Cons...2\Gateway\Config\Config was not found. Did you mean Moip\Magento2\Gateway\Config\Config? If so, make sure to prefix the type with \.
Loading history...
46
     */
47
    private $moipConfig;
48
49
    /**
50
     * ZendClientFactory.
51
     *
52
     * @var \Magento\Framework\HTTP\ZendClientFactory
53
     */
54
    private $httpClientFactory;
55
56
    /**
57
     * Json.
58
     *
59
     * @var \Magento\Framework\Serialize\Serializer\Json
60
     */
61
    private $json;
62
63
    /**
64
     * All constructor.
65
     *
66
     * @param LoggerInterface      $logger
67
     * @param ScopeConfigInterface $scopeConfig
68
     * @param State                $state
69
     * @param MoipConfig           $moipConfig
70
     * @param Json                 $json
71
     * @param ZendClientFactory    $httpClientFactory
72
     */
73
    public function __construct(
74
        LoggerInterface $logger,
75
        ScopeConfigInterface $scopeConfig,
76
        State $state,
77
        MoipConfig $moipConfig,
78
        Json $json,
79
        ZendClientFactory $httpClientFactory
80
    ) {
81
        parent::__construct(
82
            $logger
83
        );
84
        $this->state = $state;
85
        $this->scopeConfig = $scopeConfig;
86
        $this->moipConfig = $moipConfig;
87
        $this->json = $json;
88
        $this->httpClientFactory = $httpClientFactory;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function delete($ids = [])
95
    {
96
        $this->writeln('Delete Preference');
97
        foreach ($ids as $id) {
98
            $preference = $this->deleteWebhook($id);
99
            if (!$preference) {
100
                $this->writeln(__('<error>error deleting %1: %2</error>', $id, $preference['error']));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

100
                $this->writeln(/** @scrutinizer ignore-call */ __('<error>error deleting %1: %2</error>', $id, $preference['error']));
Loading history...
101
102
                return $this;
103
            }
104
            $this->writeln(__('<info>Deleted preference %1</info>', $id));
105
        }
106
        $this->writeln(__('Finished'));
107
108
        return $this;
109
    }
110
111
    /*
112
     * Delete Preference Webhooks
113
     *
114
     * @return array
115
     */
116
    private function deleteWebhook(string $id): array
117
    {
118
        $uri = $this->moipConfig->getApiUrl();
119
        $apiBearer = $this->moipConfig->getMerchantGatewayOauth();
120
        $client = $this->httpClientFactory->create();
121
122
        $client->setUri($uri.'preferences/notifications/'.$id);
123
        $client->setConfig(['maxredirects' => 0, 'timeout' => 30]);
124
        $client->setHeaders('Authorization', 'Bearer '.$apiBearer);
125
        $client->setMethod(ZendClient::DELETE);
126
127
        try {
128
            $status = $client->request()->getStatus();
129
130
            return [
131
                'success' => true,
132
                'status'  => $status,
133
            ];
134
        } catch (Exception $e) {
135
            return ['success' => true, 'error' =>  $e->getMessage()];
136
        }
137
    }
138
}
139