Completed
Push — master ( 396a44...7d840c )
by Tobias
11:02
created

ExtractCommand::getConfiguredFinder()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 6
cts 8
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 1
crap 3.1406
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\Bundle\Command;
13
14
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Style\SymfonyStyle;
20
use Symfony\Component\Finder\Finder;
21
use Translation\Bundle\Model\Configuration;
22
use Translation\Extractor\Model\Error;
23
24
/**
25
 * @author Tobias Nyholm <[email protected]>
26
 */
27
class ExtractCommand extends ContainerAwareCommand
28
{
29
    use BundleTrait;
30
31 1 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...
32
    {
33
        $this
34 1
            ->setName('translation:extract')
35 1
            ->setDescription('Extract translations from source code.')
36 1
            ->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
37 1
            ->addArgument('locale', InputArgument::OPTIONAL, 'The locale ot use. If omitted, we use all configured locales.', false)
38 1
            ->addOption('hide-errors', null, InputOption::VALUE_NONE, 'If we should print error or not')
39 1
            ->addOption('bundle', 'b', InputOption::VALUE_REQUIRED, 'The bundle you want extract translations from.')
40
        ;
41 1
    }
42
43 1
    protected function execute(InputInterface $input, OutputInterface $output)
44
    {
45 1
        $container = $this->getContainer();
46 1
        $importer = $container->get('php_translation.importer');
47 1
        $config = $container->get('php_translation.configuration_manager')
48 1
            ->getConfiguration($input->getArgument('configuration'));
49
50 1
        $locales = [];
51 1
        if ($inputLocale = $input->getArgument('locale')) {
52 1
            $locales = [$inputLocale];
53
        }
54
55 1
        $catalogues = $container->get('php_translation.catalogue_fetcher')
56 1
            ->getCatalogues($config, $locales);
57
58 1
        $this->configureBundleDirs($input, $config);
59 1
        $finder = $this->getConfiguredFinder($config);
60
61 1
        $result = $importer->extractToCatalogues($finder, $catalogues, [
62 1
            'blacklist_domains' => $config->getBlacklistDomains(),
63 1
            'whitelist_domains' => $config->getWhitelistDomains(),
64 1
            'project_root' => $config->getProjectRoot(),
65
        ]);
66 1
        $errors = $result->getErrors();
67
68 1
        $container->get('php_translation.catalogue_writer')
69 1
            ->writeCatalogues($config, $result->getMessageCatalogues());
70
71 1
        $catalogueCounter = $container->get('php_translation.catalogue_counter');
72 1
        $definedBefore = $catalogueCounter->getNumberOfDefinedMessages($catalogues[0]);
73 1
        $definedAfter = $catalogueCounter->getNumberOfDefinedMessages($result->getMessageCatalogues()[0]);
74
75
        /*
76
         * Print results
77
         */
78 1
        $io = new SymfonyStyle($input, $output);
79 1
        $io->table(['Type', 'Count'], [
80 1
            ['Total defined messages', $definedAfter],
81 1
            ['New messages', $definedAfter - $definedBefore],
82 1
            ['Errors', count($errors)],
83
        ]);
84
85 1
        if (!$input->getOption('hide-errors')) {
86
            /** @var Error $error */
87 1
            foreach ($errors as $error) {
88
                $io->error(
89
                    sprintf("%s\nLine: %s\nMessage: %s", $error->getPath(), $error->getLine(), $error->getMessage())
90
                );
91
            }
92
        }
93 1
    }
94
95
    /**
96
     * @param Configuration $config
97
     *
98
     * @return Finder
99
     */
100 1
    private function getConfiguredFinder(Configuration $config)
101
    {
102 1
        $finder = new Finder();
103 1
        $finder->in($config->getDirs());
104
105 1
        foreach ($config->getExcludedDirs() as $exclude) {
106
            $finder->notPath($exclude);
107
        }
108
109 1
        foreach ($config->getExcludedNames() as $exclude) {
110
            $finder->notName($exclude);
111
        }
112
113 1
        return $finder;
114
    }
115
}
116