PushCommand   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 131
Duplicated Lines 19.85 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 16
Bugs 1 Features 6
Metric Value
wmc 13
c 16
b 1
f 6
lcom 0
cbo 5
dl 26
loc 131
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 19 19 1
C execute() 7 102 12

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 Openl10n\Cli\Command;
4
5
use GuzzleHttp\Exception\BadResponseException;
6
use Openl10n\Sdk\Model\Resource;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class PushCommand extends AbstractCommand
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17 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...
18
    {
19
        $this
20
            ->setName('push')
21
            ->setDescription('Push the translations from the local files to the server')
22
            ->addOption(
23
                'locale',
24
                null,
25
                InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY,
26
                'The locale id, "default" for the source, "all" for every locales found',
27
                ['default']
28
            )
29
            ->addArgument(
30
                'files',
31
                InputArgument::IS_ARRAY,
32
                'File list you want to push to the server'
33
            )
34
        ;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    protected function execute(InputInterface $input, OutputInterface $output)
41
    {
42
        $api = $this->get('api');
43
        $projectApi = $api->getEntryPoint('project');
44
        $resourceApi = $api->getEntryPoint('resource');
45
46
        //
47
        // Get project
48
        //
49
        $projectSlug = $this->get('project_handler')->getProjectSlug();
50
        $project = $projectApi->get($projectSlug);
51
52
        //
53
        // Get project locales
54
        //
55
        $languages = $projectApi->getLanguages($project->getSlug());
56
        $projectLocales = array_map(function ($language) {
57
            return $language->getLocale();
58
        }, $languages);
59
60
        $defaultLocale = $project->getDefaultLocale();
61
62
        // Retrieve locales option
63
        $localesToPush = $input->getOption('locale');
64
        $localesToPush = array_unique($localesToPush);
65
        $pushAllLocale = false;
66
67
        // Process locales special cases
68 View Code Duplication
        if (in_array('all', $localesToPush)) {
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...
69
            $localesToPush = $projectLocales;
70
            $pushAllLocale = true;
71
        } elseif (false !== $key = array_search('default', $localesToPush)) {
72
            unset($localesToPush[$key]);
73
            $localesToPush[] = $defaultLocale;
74
        }
75
76
        // Deduplicate values
77
        $localesToPush = array_unique($localesToPush);
78
79
        //
80
        // Retrieve existing project's resources
81
        //
82
        $resources = $resourceApi->findByProject($project);
83
        // Set resources' pathname as array key
84
        $resources = array_combine(array_map(function ($resource) {
85
            return $resource->getPathname();
86
        }, $resources), $resources);
87
88
        //
89
        // Iterate over resources
90
        //
91
        $fileSets = $this->get('file_handler')->getFileSets();
92
        $fileFilter = $input->getArgument('files');
93
94
        foreach ($fileSets as $fileSet) {
95
            $files = $fileSet->getFiles();
96
            $options = $fileSet->getOptions('push');
97
98
            foreach ($files as $file) {
99
                $resourceIdentifier = $file->getPathname(['locale' => $defaultLocale]);
100
                $locale = $file->getAttribute('locale');
101
102
                // Ignore non specified locales
103
                if (!in_array($locale, $localesToPush) && !$pushAllLocale) {
104
                    continue;
105
                }
106
107
                // Skip unwanted files
108
                if (!empty($fileFilter) && !in_array($file->getRelativePathname(), $fileFilter)) {
109
                    continue;
110
                }
111
112
                // Create locale if non existing
113
                if (!in_array($locale, $projectLocales)) {
114
                    try {
115
                        $output->writeln(sprintf('<info>Adding</info> locale <comment>%s</comment>', $locale));
116
                        $projectApi->addLanguage($project->getSlug(), $locale);
117
                        $projectLocales[] = $locale;
118
                    } catch (BadResponseException $e) {
119
                        $output->writeln(sprintf('<error>Unknown</error> locale <comment>%s</comment>', $locale));
120
                        continue;
121
                    }
122
                }
123
124
                // Retrieve or create resource entity
125
                if (isset($resources[$resourceIdentifier])) {
126
                    $resource = $resources[$resourceIdentifier];
127
                } else {
128
                    $output->writeln(sprintf('<info>Creating</info> resource <comment>%s</comment>', $resourceIdentifier));
129
130
                    $resource = new Resource($project->getSlug());
131
                    $resource->setPathname($resourceIdentifier);
132
                    $resourceApi->create($resource);
133
134
                    $resources[$resourceIdentifier] = $resource;
135
                }
136
137
                $output->writeln(sprintf('<info>Uploading</info> file <comment>%s</comment>', $file->getRelativePathname()));
138
                $resourceApi->import($resource, $file->getAbsolutePathname(), $locale, $options);
139
            }
140
        }
141
    }
142
}
143