Completed
Push — master ( 59e207...c2af96 )
by Nik
03:00
created

ExportCommand::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 28

Duplication

Lines 35
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 35
loc 35
rs 8.8571
cc 1
eloc 28
nc 1
nop 0
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\Exporter;
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 export information block(s) in xml file(s)
17
 */
18
class ExportCommand 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:export')
29
            ->setDescription('Export information block(s) to xml')
30
            ->addArgument(
31
                'type',
32
                InputArgument::REQUIRED,
33
                'Information block type'
34
            )
35
            ->addArgument(
36
                'code',
37
                InputArgument::REQUIRED,
38
                'Information block code'
39
            )
40
            ->addArgument(
41
                'dir',
42
                InputArgument::OPTIONAL,
43
                'Directory to export'
44
            )
45
            ->addOption(
46
                'sections',
47
                's',
48
                InputOption::VALUE_OPTIONAL,
49
                'Export sections [ "active", "all", "none" ]',
50
                'none'
51
            )
52
            ->addOption(
53
                'elements',
54
                'e',
55
                InputOption::VALUE_OPTIONAL,
56
                'Export elements [ "active", "all", "none" ]',
57
                'none'
58
            );
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    protected function initialize(InputInterface $input, OutputInterface $output)
65
    {
66
        Loader::includeModule('iblock');
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    protected function interact(InputInterface $input, OutputInterface $output)
73
    {
74
        $this->setDir($input);
75
        $this->setIblocks($input);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    protected function execute(InputInterface $input, OutputInterface $output)
82
    {
83
        $formatter = new FormatterHelper();
84
85 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...
86
            $output->writeln($formatter->formatBlock($this->errors, 'error'));
87
            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...
88
        }
89
90
        $exporter = new Exporter();
91
        $exporter
92
            ->setSections($input->getOption('sections'))
93
            ->setElements($input->getOption('elements'));
94
95
        foreach ($this->iblocks as $iblock) {
96
97
            try {
98
                $xml_id = \CIBlockCMLExport::GetIBlockXML_ID($iblock['ID']);
99
                $path = implode(DIRECTORY_SEPARATOR, [$this->dir, $xml_id]) . $this->extension;
100
101
                $exporter
102
                    ->setPath($path)
103
                    ->setId($iblock['ID'])
104
                    ->execute();
105
106
                $output->writeln(sprintf('<info>%s</info> iblock %s %s', 'success', $iblock['CODE'], $path));
107
108
            } catch (IblockException $e) {
109
                $output->writeln(sprintf('<error>%s</error> iblock %s', 'fail', $iblock['CODE']));
110
                if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
111
                    $output->writeln($e->getMessage());
112
                }
113
            }
114
        }
115
116
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) 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...
117
    }
118
}