These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Rebuild Url Rewrite for magento 2 |
||
4 | * Copyright (C) 2017 |
||
5 | * |
||
6 | * This file is part of DanielSousa/UrlRewrite. |
||
7 | * |
||
8 | * DanielSousa/UrlRewrite is free software: you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU General Public License as published by |
||
10 | * the Free Software Foundation, either version 3 of the License, or |
||
11 | * (at your option) any later version. |
||
12 | * |
||
13 | * This program is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
16 | * GNU General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU General Public License |
||
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
20 | */ |
||
21 | |||
22 | namespace DanielSousa\UrlRewrite\Console\Command; |
||
23 | |||
24 | use Magento\Backend\App\Area\FrontNameResolver; |
||
25 | use Symfony\Component\Console\Command\Command; |
||
26 | use Symfony\Component\Console\Input\InputInterface; |
||
27 | use Symfony\Component\Console\Output\OutputInterface; |
||
28 | |||
29 | class AbstractUrlRewriteCommand extends Command |
||
30 | { |
||
31 | |||
32 | /** |
||
33 | * @var \Symfony\Component\Console\Helper\ProgressBar |
||
34 | */ |
||
35 | protected $progressBar; |
||
36 | |||
37 | /** |
||
38 | * @var \Magento\Framework\ObjectManagerInterface |
||
39 | */ |
||
40 | protected $objectManager; |
||
41 | /** |
||
42 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
43 | */ |
||
44 | protected $output; |
||
45 | /** |
||
46 | * @var \Magento\Framework\App\ObjectManagerFactory |
||
47 | */ |
||
48 | private $objectManagerFactory; |
||
49 | |||
50 | /** |
||
51 | * @param \Magento\Framework\App\ObjectManagerFactory $objectManagerFactory |
||
52 | * @internal param \Magento\Framework\ObjectManagerInterface $objectManager |
||
53 | * @internal param \Magento\Framework\App\State $state |
||
54 | */ |
||
55 | public function __construct( |
||
56 | \Magento\Framework\App\ObjectManagerFactory $objectManagerFactory |
||
57 | ) |
||
58 | { |
||
59 | $this->objectManagerFactory = $objectManagerFactory; |
||
60 | parent::__construct(); |
||
61 | |||
62 | } |
||
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | protected function execute(InputInterface $input, OutputInterface $output) |
||
68 | { |
||
69 | $this->output = $output; |
||
70 | $this->setupProgress(); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Setup progress bar |
||
75 | */ |
||
76 | private function setupProgress() |
||
77 | { |
||
78 | $this->progressBar = new \Symfony\Component\Console\Helper\ProgressBar($this->output); |
||
79 | $this->progressBar->setFormat( |
||
80 | '<info>Product ID: %message%</info> %current%/%max% [%bar%] %percent:3s%%' |
||
81 | ); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Replace data with new product urls |
||
86 | * |
||
87 | * @param $urls |
||
88 | */ |
||
89 | protected function replaceUrls(array $urls) |
||
90 | { |
||
91 | if (empty($urls)) { |
||
92 | return; |
||
93 | } |
||
94 | |||
95 | $this->getUrlPersist()->replace( |
||
96 | $urls |
||
97 | ); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Get Url UrlPersist |
||
102 | * @return \Magento\UrlRewrite\Model\UrlPersistInterface |
||
103 | */ |
||
104 | protected function getUrlPersist() |
||
105 | { |
||
106 | /** @var \Magento\UrlRewrite\Model\UrlPersistInterface $urlPersist */ |
||
107 | return $this->getObjectManager()->get('\Magento\UrlRewrite\Model\UrlPersistInterface'); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Gets initialized object manager |
||
112 | * |
||
113 | * @return \Magento\Framework\ObjectManagerInterface |
||
114 | */ |
||
115 | protected function getObjectManager() |
||
0 ignored issues
–
show
|
|||
116 | { |
||
117 | if (null == $this->objectManager) { |
||
118 | $area = FrontNameResolver::AREA_CODE; |
||
119 | $this->objectManager = $this->objectManagerFactory->create($_SERVER); |
||
120 | /** @var \Magento\Framework\App\State $appState */ |
||
121 | $appState = $this->objectManager->get('Magento\Framework\App\State'); |
||
122 | $appState->setAreaCode($area); |
||
123 | $configLoader = $this->objectManager->get('Magento\Framework\ObjectManager\ConfigLoaderInterface'); |
||
124 | $this->objectManager->configure($configLoader->load($area)); |
||
125 | } |
||
126 | return $this->objectManager; |
||
127 | } |
||
128 | } |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: