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

All::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 6
dl 0
loc 16
rs 10
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 All Preference Webhook.
25
 */
26
class All 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 all()
95
    {
96
        $this->writeln('List All Preference');
97
        $preference = $this->getPreferenceWebhooks();
98
        if (!$preference) {
99
            $this->writeln(__('<error>Error %1</error>', $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

99
            $this->writeln(/** @scrutinizer ignore-call */ __('<error>Error %1</error>', $preference['error']));
Loading history...
100
101
            return $this;
102
        }
103
104
        foreach ($preference['preference'] as $webhooks) {
105
            if (isset($webhooks['id'])) {
106
                $this->writeln(__('<info>Preference ID: %1 Target Uri: %2</info>', $webhooks['id'], $webhooks['target']));
107
            }
108
        }
109
        $this->writeln(__('Finished'));
110
111
        return $this;
112
    }
113
114
    /*
115
     * List All Preference Webhooks
116
     *
117
     * @return array
118
     */
119
    private function getPreferenceWebhooks(): array
120
    {
121
        $uri = $this->moipConfig->getApiUrl();
122
        $apiBearer = $this->moipConfig->getMerchantGatewayOauth();
123
        $client = $this->httpClientFactory->create();
124
125
        $client->setUri($uri.'preferences/notifications');
126
        $client->setConfig(['maxredirects' => 0, 'timeout' => 30]);
127
        $client->setHeaders('Authorization', 'Bearer '.$apiBearer);
128
        $client->setMethod(ZendClient::GET);
129
130
        try {
131
            $result = $client->request()->getBody();
132
133
            return [
134
                'success'    => true,
135
                'preference' => $this->json->unserialize($result),
136
            ];
137
        } catch (Exception $e) {
138
            return ['success' => true, 'error' =>  $e->getMessage()];
139
        }
140
    }
141
}
142