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

ImportCommand::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 28

Duplication

Lines 36
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 36
loc 36
rs 8.8571
cc 1
eloc 28
nc 1
nop 0
1
<?php
2
3
namespace Notamedia\ConsoleJedi\Schema\Command;
4
5
use Notamedia\ConsoleJedi\Application\Command\BitrixCommand;
6
use Notamedia\ConsoleJedi\Schema\Exception\SchemaException;
7
use Notamedia\ConsoleJedi\Schema\Schema;
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
class ImportCommand extends BitrixCommand
16
{
17
    use CommandTrait;
18
19
    /**
20
     * {@inheritdoc}
21
     */
22 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...
23
    {
24
        $this
25
            ->setName('schema:import')
26
            ->setDescription('Import information block(s) from xml')
27
            ->addArgument(
28
                'type',
29
                InputArgument::REQUIRED,
30
                'Information iblock type'
31
            )
32
            ->addArgument(
33
                'sites',
34
                InputArgument::REQUIRED,
35
                'Sites to which the information iblock will be bound (if it is to be created)'
36
            )
37
            ->addArgument(
38
                'dir',
39
                InputArgument::OPTIONAL,
40
                'Directory to import'
41
            )
42
            ->addOption(
43
                'sections',
44
                's',
45
                InputOption::VALUE_OPTIONAL,
46
                'If an existing section is no longer in the source file [ leave: "N", deactivate: "A", delete: "D" ]',
47
                'A'
48
            )
49
            ->addOption(
50
                'elements',
51
                'e',
52
                InputOption::VALUE_OPTIONAL,
53
                'If an existing element is no longer in the source file [ leave: "N", deactivate: "A", delete: "D" ]',
54
                'A'
55
            );
56
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    protected function initialize(InputInterface $input, OutputInterface $output)
63
    {
64
        Loader::includeModule('iblock');
65
    }
66
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    protected function interact(InputInterface $input, OutputInterface $output)
72
    {
73
        $this->setDir($input);
74
        $this->setType($input);
75
        $this->setSites($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
        $import = Schema::import()
91
            ->setType($this->type)
92
            ->setSites($this->sites)
93
            ->setActionSection($input->getOption('sections'))
94
            ->setActionElement($input->getOption('elements'));;
95
96
        foreach (glob(implode(DIRECTORY_SEPARATOR . '*', [$this->dir, $this->extension])) as $file) {
97
98
            try {
99
                $import
100
                    ->setPath($file)
101
                    ->execute();
102
103
                $output->writeln(sprintf('<info>%s</info> file %s', 'success', $file));
104
105
            } catch (SchemaException $e) {
106
                $output->writeln(sprintf('<error>%s</error> file %s', 'fail', $file));
107
                if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
108
                    $output->writeln($e->getMessage());
109
                }
110
            }
111
        }
112
    }
113
}