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

ImportCommand::interact()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
namespace Notamedia\ConsoleJedi\Iblock\Command;
4
5
use Notamedia\ConsoleJedi\Application\Command\BitrixCommand;
6
use Notamedia\ConsoleJedi\Iblock\Exception\IblockException;
7
use Notamedia\ConsoleJedi\Iblock\Importer;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Helper\FormatterHelper;
13
use Bitrix\Main\Loader;
14
15
/**
16
 * Command import information block(s) from xml file(s)
17
 */
18
class ImportCommand extends BitrixCommand
19
{
20
    use MigrationCommandTrait;
21
22
    /**
23
     * {@inheritdoc}
24
     */
25 View Code Duplication
    protected function configure()
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...
26
    {
27
        $this
28
            ->setName('iblock:import')
29
            ->setDescription('Import information block(s) from xml')
30
            ->addArgument(
31
                'type',
32
                InputArgument::REQUIRED,
33
                'Information block type'
34
            )
35
            ->addArgument(
36
                'sites',
37
                InputArgument::REQUIRED,
38
                'Sites to which the information block will be bound (if it is to be created)'
39
            )
40
            ->addArgument(
41
                'dir',
42
                InputArgument::OPTIONAL,
43
                'Directory to import'
44
            )
45
            ->addOption(
46
                'sections',
47
                's',
48
                InputOption::VALUE_OPTIONAL,
49
                'If an existing section is no longer in the source file [ leave: "N", deactivate: "A", delete: "D" ]',
50
                'A'
51
            )
52
            ->addOption(
53
                'elements',
54
                'e',
55
                InputOption::VALUE_OPTIONAL,
56
                'If an existing element is no longer in the source file [ leave: "N", deactivate: "A", delete: "D" ]',
57
                'A'
58
            );
59
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    protected function initialize(InputInterface $input, OutputInterface $output)
66
    {
67
        Loader::includeModule('iblock');
68
    }
69
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    protected function interact(InputInterface $input, OutputInterface $output)
75
    {
76
        $this->setDir($input);
77
        $this->setType($input);
78
        $this->setSites($input);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    protected function execute(InputInterface $input, OutputInterface $output)
85
    {
86
        $formatter = new FormatterHelper();
87
88 View Code Duplication
        if (count($this->errors) > 0) {
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...
89
            $output->writeln($formatter->formatBlock($this->errors, 'error'));
90
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type of the parent method Symfony\Component\Console\Command\Command::execute of type null|integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
91
        }
92
93
        $importer = new Importer();
94
        $importer
95
            ->setType($this->type)
96
            ->setSites($this->sites)
97
            ->setActionSection($input->getOption('sections'))
98
            ->setActionElement($input->getOption('elements'));;
99
100
        foreach (glob(implode(DIRECTORY_SEPARATOR . '*', [$this->dir, $this->extension])) as $file) {
101
102
            try {
103
                $importer
104
                    ->setPath($file)
105
                    ->execute();
106
107
                $output->writeln(sprintf('<info>%s</info> file %s', 'success', $file));
108
109
            } catch (IblockException $e) {
110
                $output->writeln(sprintf('<error>%s</error> file %s', 'fail', $file));
111
                if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
112
                    $output->writeln($e->getMessage());
113
                }
114
            }
115
        }
116
    }
117
}