Completed
Pull Request — master (#20)
by
unknown
01:33
created

MigrationCommandTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 133
Duplicated Lines 22.56 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 30
loc 133
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setDir() 0 19 4
A setIblocks() 0 16 2
A setType() 15 15 2
A setSites() 15 15 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Notamedia\ConsoleJedi\Iblock\Command;
4
5
use Notamedia\ConsoleJedi\Application\Application;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Filesystem\Filesystem;
8
use Bitrix\Main\SiteTable;
9
use Bitrix\Iblock\TypeTable;
10
use Bitrix\Iblock\IblockTable;
11
12
/**
13
 * Trait checks and sets import/export parameters information block
14
 */
15
trait MigrationCommandTrait
16
{
17
    /**
18
     * Errors check parameters
19
     *
20
     * @var array
21
     */
22
    protected $errors = [];
23
24
    /**
25
     * Information block sites
26
     *
27
     * @var array
28
     */
29
    protected $sites = [];
30
31
    /**
32
     * Information block type
33
     *
34
     * @var string
35
     */
36
    protected $type = '';
37
38
    /**
39
     * Information blocks
40
     *
41
     * @var array
42
     */
43
    protected $iblocks = [];
44
45
    /**
46
     * Directory with file(s)
47
     *
48
     * @var string
49
     */
50
    protected $dir;
51
52
    /**
53
     * Extension file(s)
54
     *
55
     * @var string
56
     */
57
    protected $extension = '.xml';
58
59
    /**
60
     * Check argument directory and set $this->dir
61
     *
62
     * @param InputInterface $input
63
     */
64
    protected function setDir(InputInterface $input)
65
    {
66
        $app = new Application();
67
        $filesystem = new Filesystem();
68
        $dir = $input->getArgument('dir');
69
70
        if (!$dir) {
71
            $dir = $app->getRoot();
72
        } elseif (!$filesystem->isAbsolutePath($dir)) {
73
            $dir = $app->getRoot() . DIRECTORY_SEPARATOR . $dir;
74
        }
75
        $dir = rtrim($dir, DIRECTORY_SEPARATOR);
76
77
        if (!$filesystem->exists($dir)) {
78
            $this->errors[] = "Directory $dir not found";
79
        }
80
81
        $this->dir = $dir;
82
    }
83
84
    /**
85
     * Check arguments type and code, set $this->iblocks
86
     *
87
     * @param InputInterface $input
88
     */
89
    protected function setIblocks(InputInterface $input)
90
    {
91
        $iblocks = IblockTable::query()
92
            ->setFilter([
93
                'IBLOCK_TYPE_ID' => $input->getArgument('type'),
94
                'CODE' => $input->getArgument('code')
95
            ])
96
            ->setSelect(['ID', 'CODE'])
97
            ->exec();
98
99
        if ($iblocks->getSelectedRowsCount() <= 0) {
100
            $this->errors[] = 'Iblock(s) not found';
101
        }
102
103
        $this->iblocks = $iblocks->fetchAll();
104
    }
105
106
    /**
107
     * Check argument type and set $this->type
108
     *
109
     * @param InputInterface $input
110
     */
111 View Code Duplication
    protected function setType(InputInterface $input)
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...
112
    {
113
        $type = TypeTable::query()
114
            ->setFilter([
115
                'ID' => $input->getArgument('type'),
116
            ])
117
            ->setSelect(['ID'])
118
            ->exec();
119
120
        if ($type->getSelectedRowsCount() <= 0) {
121
            $this->errors[] = 'Type not found';
122
        }
123
124
        $this->type = $type->fetch()['ID'];
125
    }
126
127
    /**
128
     * Check argument sites and set $this->sites
129
     *
130
     * @param InputInterface $input
131
     */
132 View Code Duplication
    protected function setSites(InputInterface $input)
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...
133
    {
134
        $sites = SiteTable::query()
135
            ->setFilter([
136
                'LID' => $input->getArgument('sites'),
137
            ])
138
            ->setSelect(['LID'])
139
            ->exec();
140
141
        if ($sites->getSelectedRowsCount() <= 0) {
142
            $this->errors[] = 'Sites not found';
143
        }
144
145
        $this->sites = $sites->fetchAll();
146
    }
147
}