Completed
Pull Request — master (#137)
by
unknown
07:57
created

LanguageManager::setReferences()   D

Complexity

Conditions 10
Paths 15

Size

Total Lines 39
Code Lines 28

Duplication

Lines 39
Ratio 100 %

Code Coverage

Tests 1
CRAP Score 10

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 39
loc 39
ccs 1
cts 1
cp 1
rs 4.8196
cc 10
eloc 28
nc 15
nop 2
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Executor;
4
5
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
6
use Kaliop\eZMigrationBundle\API\Collection\LanguageCollection;
7
8
/**
9
 * Handles language migrations.
10
 */
11
class LanguageManager extends RepositoryExecutor
12
{
13
    protected $supportedStepTypes = array('language');
14
    protected $supportedActions = array('create', 'delete', 'upsert');
15
16
    /**
17
     * Handles the language create migration action
18 1
     */
19
    protected function create($step)
20 1
    {
21
        $languageService = $this->repository->getContentLanguageService();
22 1
23
        if (!isset($step->dsl['lang'])) {
24
            throw new \Exception("The 'lang' key is required to create a new language.");
25
        }
26 1
27 1
        $languageCreateStruct = $languageService->newLanguageCreateStruct();
28 1
        $languageCreateStruct->languageCode = $step->dsl['lang'];
29 1
        if (isset($step->dsl['name'])) {
30 1
            $languageCreateStruct->name = $step->dsl['name'];
31 1
        }
32
        if (isset($step->dsl['enabled'])) {
33
            $languageCreateStruct->enabled = (bool)$step->dsl['enabled'];
34 1
        }
35
        $language = $languageService->createLanguage($languageCreateStruct);
36 1
37 1
        $this->setReferences($language, $step);
38
39
        return $language;
40
    }
41
42
    /**
43
     * Handles the language update migration action
44
     *
45
     * @todo use a matcher for flexible matching?
46
     */
47
    protected function update($step)
0 ignored issues
show
Unused Code introduced by
The parameter $step is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
    {
49
        throw new \Exception('Language update is not implemented yet');
50
51
        /*$languageService = $this->repository->getContentLanguageService();
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
52
53
        if (!isset($step->dsl['lang'])) {
54
            throw new \Exception("The 'lang' key is required to update a language.");
55
        }
56
57
        $this->setReferences($language, $step);*/
58
    }
59
60 1
    /**
61
     * Handles the language delete migration action
62 1
     *
63
     * @todo use a matcher for flexible matching?
64
     */
65 View Code Duplication
    protected function delete($step)
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...
66 1
    {
67 1
        if (!isset($step->dsl['lang'])) {
68
            throw new \Exception("The 'lang' key is required to delete a language.");
69 1
        }
70 1
71
        $languageService = $this->repository->getContentLanguageService();
72
        $language = $languageService->loadLanguage($step->dsl['lang']);
73
74
        $languageService->deleteLanguage($language);
75
76
        return $language;
77
    }
78
79
    /**
80 1
     * Method that create a language if it doesn't already exist.
81
     */
82 1 View Code Duplication
    protected function upsert($step)
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...
83
    {
84
        if (!isset($step->dsl['lang'])) {
85
            throw new \Exception("The 'lang' key is missing in a language upsert definition");
86 1
        }
87
88
        $languageService = $this->repository->getContentLanguageService();
89
90
        try {
91
            $language = $languageService->loadLanguage($step->dsl['lang']);
92
93 1
            return $language;
94
        } catch (NotFoundException $e) {
95 1
            return $this->create($step);
96 1
        }
97 1
    }
98 1
99 1
    /**
100
     * Sets references to certain language attributes.
101
     *
102 1
     * @param \eZ\Publish\API\Repository\Values\Content\Language|LanguageCollection $language
103
     * @throws \InvalidArgumentException When trying to set a reference to an unsupported attribute
104 1
     * @return boolean
105 1
     */
106 View Code Duplication
    protected function setReferences($language, $step)
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...
107 1
    {
108
        if (!array_key_exists('references', $step->dsl)) {
109
            return false;
110
        }
111
112
        $references = $this->setReferencesCommon($language, $step->dsl['references']);
113
        $language = $this->insureSingleEntity($language, $references);
114
115
        foreach ($references as $reference) {
116
117
            switch ($reference['attribute']) {
118
                case 'language_id':
119
                case 'id':
120
                    $value = $language->id;
121
                    break;
122
                case 'enabled':
123
                    $value = $language->enabled;
124
                    break;
125
                case 'language_code':
126
                    $value = $language->languageCode;
127
                    break;
128
                case 'language_name':
129
                case 'name':
130
                    $value = $language->name;
131
                    break;
132
                default:
133
                    throw new \InvalidArgumentException('Language Manager does not support setting references for attribute ' . $reference['attribute']);
134
            }
135
136
            $overwrite = false;
137
            if (isset($reference['overwrite'])) {
138
                $overwrite = $reference['overwrite'];
139
            }
140
            $this->referenceResolver->addReference($reference['identifier'], $value, $overwrite);
141
        }
142
143
        return true;
144
    }
145
}
146