Completed
Pull Request — master (#20)
by
unknown
03:04
created

MigrationCommandTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 130
Duplicated Lines 23.08 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

4 Methods

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