1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\Integration; |
4
|
|
|
|
5
|
|
|
use Magento\Integration\Model\Integration; |
6
|
|
|
use Magento\Integration\Model\ResourceModel\Integration\Collection; |
7
|
|
|
use N98\Magento\Command\AbstractMagentoCommand; |
8
|
|
|
use N98\Util\Console\Helper\Table\Renderer\RendererFactory; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ListCommand |
15
|
|
|
* @package N98\Magento\Command\Integration |
16
|
|
|
*/ |
17
|
|
|
class ListCommand extends AbstractMagentoCommand |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var \Magento\Integration\Model\IntegrationFactory |
21
|
|
|
*/ |
22
|
|
|
private $integrationFactory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \Magento\Integration\Model\OauthService |
26
|
|
|
*/ |
27
|
|
|
private $oauthService; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \Magento\Integration\Model\AuthorizationService |
31
|
|
|
*/ |
32
|
|
|
private $authorizationService; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \Magento\Integration\Model\Oauth\TokenFactory |
36
|
|
|
*/ |
37
|
|
|
private $tokenFactory; |
38
|
|
|
|
39
|
|
|
protected function configure() |
40
|
|
|
{ |
41
|
|
|
$this |
42
|
|
|
->setName('integration:list') |
43
|
|
|
->setDescription('List all existing integrations.') |
44
|
|
|
->addOption( |
45
|
|
|
'format', |
46
|
|
|
null, |
47
|
|
|
InputOption::VALUE_OPTIONAL, |
48
|
|
|
'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']' |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function inject( |
53
|
|
|
\Magento\Integration\Model\IntegrationFactory $integrationFactory, |
54
|
|
|
\Magento\Integration\Model\OauthService $oauthService, |
55
|
|
|
\Magento\Integration\Model\AuthorizationService $authorizationService, |
56
|
|
|
\Magento\Integration\Model\Oauth\TokenFactory $tokenFactory |
57
|
|
|
) { |
58
|
|
|
$this->integrationFactory = $integrationFactory; |
59
|
|
|
$this->oauthService = $oauthService; |
60
|
|
|
$this->authorizationService = $authorizationService; |
61
|
|
|
$this->tokenFactory = $tokenFactory; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
66
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
67
|
|
|
* @return int|void |
68
|
|
|
* @throws \Exception |
69
|
|
|
*/ |
70
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
71
|
|
|
{ |
72
|
|
|
$integrationModel = $this->integrationFactory->create(); |
73
|
|
|
|
74
|
|
|
/** @var Collection $collection */ |
75
|
|
|
$collection = $integrationModel->getCollection(); |
76
|
|
|
|
77
|
|
|
$integrations = $collection->getItems(); |
78
|
|
|
|
79
|
|
|
/** @var Integration $integration */ |
80
|
|
|
$table = []; |
81
|
|
|
foreach ($integrations as $integration) { |
82
|
|
|
switch ($integration->getStatus()) { |
83
|
|
|
case Integration::STATUS_ACTIVE: |
84
|
|
|
break; |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$table[] = [ |
89
|
|
|
$integration->getId(), |
90
|
|
|
$integration->getName(), |
91
|
|
|
$integration->getEmail(), |
92
|
|
|
$integration->getEndpoint(), |
93
|
|
|
// return type is not int as defined in Magento. Do not check type strict here. |
94
|
|
|
$integration->getSetupType() == Integration::TYPE_MANUAL ? 'Manual' : 'Config', |
95
|
|
|
Integration::STATUS_ACTIVE ? 'Active' : 'Inactive', |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->getHelper('table') |
|
|
|
|
100
|
|
|
->setHeaders(['id', 'name', 'email', 'endpoint', 'type', 'status']) |
101
|
|
|
->renderByFormat($output, $table, $input->getOption('format')); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: