InfoCommand::addVendors()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 3
eloc 8
nc 3
nop 0
1
<?php
2
3
namespace N98\Magento\Command\System;
4
5
use N98\Magento\Command\AbstractMagentoCommand;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use N98\Util\Console\Helper\Table\Renderer\RendererFactory;
11
use Magento\Framework\App\State as AppState;
12
13
class InfoCommand extends AbstractMagentoCommand
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $infos = [];
19
20
    /**
21
     * @var \Magento\Framework\App\ProductMetadataInterface
22
     */
23
    protected $productMetadata;
24
25
    /**
26
     * @var \Magento\Customer\Model\CustomerFactory
27
     */
28
    protected $customerFactory;
29
30
    /**
31
     * @var \Magento\Catalog\Model\ProductFactory
32
     */
33
    protected $productFactory;
34
35
    /**
36
     * @var \Magento\Catalog\Model\CategoryFactory
37
     */
38
    protected $categoryFactory;
39
40
    /**
41
     * @var \Magento\Eav\Model\Entity\AttributeFactory
42
     */
43
    protected $attributeFactory;
44
45
    /**
46
     * @var \Magento\Framework\App\Cache\Type\FrontendPool
47
     */
48
    protected $frontendPool;
49
50
    /**
51
     * @var \Magento\Framework\Module\ModuleListInterface
52
     */
53
    protected $moduleList;
54
55
    /**
56
     * @var \Magento\Framework\App\DeploymentConfig
57
     */
58
    protected $deploymentConfig;
59
60
    protected function configure()
61
    {
62
        $this
63
            ->setName('sys:info')
64
            ->setDescription('Prints infos about the current magento system.')
65
            ->addArgument(
66
                'key',
67
                InputArgument::OPTIONAL,
68
                'Only output value of named param like "version". Key is case insensitive.'
69
            )
70
            ->addOption(
71
                'format',
72
                null,
73
                InputOption::VALUE_OPTIONAL,
74
                'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']'
75
            )
76
        ;
77
    }
78
79
    /**
80
     * @param \Magento\Framework\App\ProductMetadataInterface $productMetadata
81
     * @param \Magento\Customer\Model\CustomerFactory $customerFactory
82
     * @param \Magento\Catalog\Model\ProductFactory $productFactory
83
     * @param \Magento\Catalog\Model\CategoryFactory $categoryFactory
84
     * @param \Magento\Eav\Model\Entity\AttributeFactory $attributeFactory
85
     * @param \Magento\Framework\App\Cache\Type\FrontendPool $frontendPool
86
     * @param \Magento\Framework\App\DeploymentConfig $deploymentConfig
87
     * @param \Magento\Framework\Module\ModuleListInterface $moduleList
88
     */
89
    public function inject(
90
        \Magento\Framework\App\ProductMetadataInterface $productMetadata,
91
        \Magento\Customer\Model\CustomerFactory $customerFactory,
92
        \Magento\Catalog\Model\ProductFactory $productFactory,
93
        \Magento\Catalog\Model\CategoryFactory $categoryFactory,
94
        \Magento\Eav\Model\Entity\AttributeFactory $attributeFactory,
95
        \Magento\Framework\App\Cache\Type\FrontendPool $frontendPool,
96
        \Magento\Framework\App\DeploymentConfig $deploymentConfig,
97
        \Magento\Framework\Module\ModuleListInterface $moduleList
98
99
    ) {
100
        $this->productMetadata = $productMetadata;
101
        $this->customerFactory = $customerFactory;
102
        $this->productFactory = $productFactory;
103
        $this->categoryFactory = $categoryFactory;
104
        $this->attributeFactory = $attributeFactory;
105
        $this->frontendPool = $frontendPool;
106
        $this->deploymentConfig = $deploymentConfig;
107
        $this->moduleList = $moduleList;
108
    }
109
110
    public function hasInfo()
111
    {
112
        return !empty($this->infos);
113
    }
114
115
    public function getInfo($key = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
116
    {
117
        if (is_null($key)) {
118
            return $this->infos;
119
        }
120
121
        return isset($this->infos[$key]) ? $this->infos[$key] : null;
122
    }
123
124
    /**
125
     * @param \Symfony\Component\Console\Input\InputInterface $input
126
     * @param \Symfony\Component\Console\Output\OutputInterface $output
127
     * @return int|void
128
     */
129
    protected function execute(InputInterface $input, OutputInterface $output)
130
    {
131
        if ($input->getOption('format') == null && $input->getArgument('key') == null) {
132
            $this->writeSection($output, 'Magento System Information');
133
        }
134
135
        $this->addVersionInfo();
136
        $this->addDeploymentInfo();
137
        $this->addCacheInfos();
138
        $this->addVendors();
139
        $this->addAttributeCount();
140
        $this->addCustomerCount();
141
        $this->addCategoryCount();
142
        $this->addProductCount();
143
144
        $table = array();
145
        foreach ($this->infos as $key => $value) {
146
            $table[] = array($key, $value);
147
        }
148
149
        if (($settingArgument = $input->getArgument('key')) !== null) {
150
            $settingArgument = strtolower($settingArgument);
151
            $this->infos = array_change_key_case($this->infos, CASE_LOWER);
152
            if (!isset($this->infos[$settingArgument])) {
153
                throw new InvalidArgumentException('Unknown key: ' . $settingArgument);
154
            }
155
            $output->writeln((string) $this->infos[$settingArgument]);
156 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157
            $this->getHelper('table')
158
                ->setHeaders(array('name', 'value'))
159
                ->renderByFormat($output, $table, $input->getOption('format'));
160
        }
161
    }
162
163
    /**
164
     * @todo there is also the product repository API...?!
165
     */
166
    protected function addProductCount()
167
    {
168
        $this->infos['Product Count'] = $this->productFactory
169
                                                ->create()
170
                                                ->getCollection()
171
                                                ->getSize();
172
    }
173
174
    protected function addCustomerCount()
175
    {
176
        $this->infos['Customer Count'] = $this->customerFactory->create()
177
                                                ->getCollection()
178
                                                ->getSize();
179
    }
180
181
    protected function addCategoryCount()
182
    {
183
        $this->infos['Category Count'] = $this->categoryFactory
184
                                                ->create()
185
                                                ->getCollection()
186
                                                ->getSize();
187
    }
188
189
    protected function addAttributeCount()
190
    {
191
        $this->infos['Attribute Count'] = $this->attributeFactory
192
                                                ->create()
193
                                                ->getCollection()
194
                                                ->getSize();
195
    }
196
197
    protected function addCacheInfos()
198
    {
199
        $cachePool = $this->frontendPool;
200
201
        $this->infos['Cache Backend'] = get_class($cachePool->get('config')->getBackend());
202
203
        switch (get_class($cachePool->get('config')->getBackend())) {
204
            case 'Zend_Cache_Backend_File':
205
            case 'Cm_Cache_Backend_File':
206
                // @TODO Where are the cache options?
207
                //$cacheDir = $cachePool->get('config')->getBackend()->getOptions()->getCacheDir();
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
208
                //$this->infos['Cache Directory'] = $cacheDir;
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
209
                break;
210
211
            default:
212
        }
213
    }
214
215
    protected function addDeploymentInfo()
216
    {
217
        $this->infos['Application Mode'] = $this->deploymentConfig->get(AppState::PARAM_MODE);
218
        $this->infos['Session'] = $this->deploymentConfig->get('session/save');
219
        $this->infos['Crypt Key'] = $this->deploymentConfig->get('crypt/key');
220
        $this->infos['Install Date'] = $this->deploymentConfig->get('install/date');
221
    }
222
223
    protected function addVersionInfo()
224
    {
225
        $this->infos['Name'] = $this->productMetadata->getName();
226
        $this->infos['Version'] = $this->productMetadata->getVersion();
227
        $this->infos['Edition'] = $this->productMetadata->getEdition();
228
    }
229
230
    protected function addVendors()
231
    {
232
        $vendors = [];
233
234
        $moduleList = $this->moduleList->getAll();
235
236
        foreach ($moduleList as $moduleName => $info) {
237
            // First index is (probably always) vendor
238
            $moduleNameData = explode('_', $moduleName);
239
240
            if (isset($moduleNameData[0])) {
241
                $vendors[] = $moduleNameData[0];
242
            }
243
        }
244
245
        $this->infos['Vendors'] = implode(', ', array_unique($vendors));
246
    }
247
}
248