GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( d3ddc5...97aa73 )
by Daniel
01:53
created

Products::createProductFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Rebuild Url Rewrite for magento 2
4
 * Copyright (C) 2016
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\UrlRewrite\Service\V1\Data\UrlRewrite;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Output\OutputInterface;
27
28
29
class Products extends AbstractUrlRewriteCommand
0 ignored issues
show
Comprehensibility Best Practice introduced by
The type DanielSousa\UrlRewrite\Console\Command\Products has been defined more than once; this definition is ignored, only the first definition in Console/Command/AbstractUrlRewriteCommand.php (L29-212) is considered.

This check looks for classes that have been defined more than once.

If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.

This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.

Loading history...
30
{
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected function configure()
36
    {
37
        $this->setName('urlrewrite:rebuild:products');
38
        $this->setDescription('Rebuild Product URL Rewrites');
39
        parent::configure();
40
    }
41
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    protected function execute(InputInterface $input, OutputInterface $output)
47
    {
48
        parent::execute($input,$output);
49
        try {
50
            $productCollection = $this->getProductCollection();
51
            $this->progressBar->start($productCollection->getSize());
52
53
            $this->getIterator()->walk(
54
                $productCollection->getSelect(),
55
                [[$this, 'callbackGenerateProductUrl']],
56
                [
57
                    'product' => $this->getProductFactory()
58
                ]
59
            );
60
61
            $this->progressBar->finish();
62
        } catch (\Exception $e) {
63
            $this->output->writeln($e->getMessage());
64
            return \Magento\Framework\Console\Cli::RETURN_FAILURE;
65
        }
66
        return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
67
    }
68
69
70
    /**
71
     * Generate product url by product
72
     * @param $args
73
     */
74 View Code Duplication
    public function callbackGenerateProductUrl($args)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
75
    {
76
        if (!isset($args['row']['entity_id'])) {
77
            $this->output->writeln('Id not found');
78
            return;
79
        }
80
        $id = $args['row']['entity_id'];
81
        $this->progressBar->setMessage($id);
82
        $this->progressBar->advance();
83
84
        try {
85
            $product = clone $args['product'];
86
            $product->load($id);
87
            $product->setStoreId(null);
88
            $this->replaceUrls(
89
                $this->prepareUrls($product)
90
            );
91
        } catch (\Exception $e) {
92
            $this->output->writeln($e->getMessage() . '- Product ID -' . $id);
93
            return;
94
        }
95
    }
96
97
98
99
100
    /**
101
     *  Remove Product urls
102
     *
103
     * @param $productId
104
     * @param null $storeId
105
     * @return bool
106
     */
107 View Code Duplication
    protected function removeProductUrls($productId, $storeId = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
108
    {
109
        if (!is_numeric($productId)) {
110
            return false;
111
        }
112
        $data = [
113
            UrlRewrite::ENTITY_ID => $productId,
114
            UrlRewrite::ENTITY_TYPE => \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator::ENTITY_TYPE,
115
            UrlRewrite::REDIRECT_TYPE => 0,
116
        ];
117
118
        if (!is_null($storeId)) {
119
            $data[] = [UrlRewrite::STORE_ID => $storeId];
120
        }
121
        try {
122
            $this->getUrlPersist()->deleteByData($data);
123
        } catch (\Exception $e) {
124
            $this->output->writeln($e->getMessage());
125
            return false;
126
        }
127
        return true;
128
    }
129
130
    /**
131
     *  Generate list of product urls
132
     *
133
     * @param $product
134
     * @return UrlRewrite[]
135
     */
136
    private function prepareUrls($product)
137
    {
138
        return $this->getProductUrlRewriteGenerator()->generate($product);
139
    }
140
}
141