Completed
Push — master ( 68da2d...d119ba )
by Gaetano
07:21
created

LanguageManager::setReferences()   D

Complexity

Conditions 10
Paths 15

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 14.3949

Importance

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

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 Kaliop\eZMigrationBundle\API\Collection\LanguageCollection;
6
7
/**
8
 * Handles language migrations.
9
 */
10
class LanguageManager extends RepositoryExecutor
11
{
12
    protected $supportedStepTypes = array('language');
13
    protected $supportedActions = array('create', 'delete');
14
15
    /**
16
     * Handles the language create migration action
17
     */
18 1
    protected function create($step)
19
    {
20 1
        $languageService = $this->repository->getContentLanguageService();
21
22 1
        if (!isset($step->dsl['lang'])) {
23
            throw new \Exception("The 'lang' key is required to create a new language.");
24
        }
25
26 1
        $languageCreateStruct = $languageService->newLanguageCreateStruct();
27 1
        $languageCreateStruct->languageCode = $step->dsl['lang'];
28 1
        if (isset($step->dsl['name'])) {
29 1
            $languageCreateStruct->name = $step->dsl['name'];
30 1
        }
31 1
        if (isset($step->dsl['enabled'])) {
32
            $languageCreateStruct->enabled = (bool)$step->dsl['enabled'];
33
        }
34 1
        $language = $languageService->createLanguage($languageCreateStruct);
35
36 1
        $this->setReferences($language, $step);
37 1
38
        return $language;
39
    }
40
41
    /**
42
     * Handles the language update migration action
43
     *
44
     * @todo use a matcher for flexible matching?
45
     */
46
    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...
47
    {
48
        throw new \Exception('Language update is not implemented yet');
49
50
        /*$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...
51
52
        if (!isset($step->dsl['lang'])) {
53
            throw new \Exception("The 'lang' key is required to update a language.");
54
        }
55
56
        $this->setReferences($language, $step);*/
57
    }
58
59
    /**
60 1
     * Handles the language delete migration action
61
     *
62 1
     * @todo use a matcher for flexible matching?
63
     */
64
    protected function delete($step)
65
    {
66 1
        if (!isset($step->dsl['lang'])) {
67 1
            throw new \Exception("The 'lang' key is required to delete a language.");
68
        }
69 1
70 1
        $languageService = $this->repository->getContentLanguageService();
71
        $language = $languageService->loadLanguage($step->dsl['lang']);
72
73
        $languageService->deleteLanguage($language);
74
75
        return $language;
76
    }
77
78
    /**
79
     * Sets references to certain language attributes.
80 1
     *
81
     * @param \eZ\Publish\API\Repository\Values\Content\Language|LanguageCollection $language
82 1
     * @throws \InvalidArgumentException When trying to set a reference to an unsupported attribute
83
     * @return boolean
84
     */
85
    protected function setReferences($language, $step)
86 1
    {
87
        if (!array_key_exists('references', $step->dsl)) {
88
            return false;
89
        }
90
91
        $this->setReferencesCommon($language, $step);
92
        $language = $this->insureSingleEntity($language, $step);
93 1
94
        foreach ($step->dsl['references'] as $reference) {
95 1
96 1
            switch ($reference['attribute']) {
97 1
                case 'language_id':
98 1
                case 'id':
99 1
                    $value = $language->id;
100
                    break;
101
                case 'enabled':
102 1
                    $value = $language->enabled;
103
                    break;
104 1
                case 'language_code':
105 1
                    $value = $language->languageCode;
106
                    break;
107 1
                case 'language_name':
108
                case 'name':
109
                    $value = $language->name;
110
                    break;
111
                default:
112
                    throw new \InvalidArgumentException('Language Manager does not support setting references for attribute ' . $reference['attribute']);
113
            }
114
115
            $overwrite = false;
116
            if (isset($reference['overwrite'])) {
117
                $overwrite = $reference['overwrite'];
118
            }
119
            $this->referenceResolver->addReference($reference['identifier'], $value, $overwrite);
120
        }
121
122
        return true;
123
    }
124
}
125