Completed
Push — master ( 3260ca...0c0ec5 )
by Christian
02:12
created

ListCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 3
dl 0
loc 87
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 12 1
A inject() 0 11 1
A execute() 0 33 5
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')
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method setHeaders() does only exist in the following implementations of said interface: N98\Util\Console\Helper\TableHelper.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
100
            ->setHeaders(['id', 'name', 'email', 'endpoint', 'type', 'status'])
101
            ->renderByFormat($output, $table, $input->getOption('format'));
102
    }
103
}
104