Completed
Push — master ( 09cee1...75eab4 )
by Gaetano
06:21
created

LanguageManager::getReferencesValues()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 30
Code Lines 22

Duplication

Lines 23
Ratio 76.67 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 23
loc 30
ccs 0
cts 26
cp 0
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 22
nc 8
nop 2
crap 72
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Executor;
4
5
use eZ\Publish\API\Repository\Values\Content\Language;
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');
15
16
    /**
17
     * Handles the language create migration action
18
     */
19
    protected function create($step)
20
    {
21
        $languageService = $this->repository->getContentLanguageService();
22
23
        if (!isset($step->dsl['lang'])) {
24
            throw new \Exception("The 'lang' key is required to create a new language.");
25
        }
26
27
        $languageCreateStruct = $languageService->newLanguageCreateStruct();
28
        $languageCreateStruct->languageCode = $step->dsl['lang'];
29
        if (isset($step->dsl['name'])) {
30
            $languageCreateStruct->name = $step->dsl['name'];
31
        }
32
        if (isset($step->dsl['enabled'])) {
33
            $languageCreateStruct->enabled = (bool)$step->dsl['enabled'];
34
        }
35
        $language = $languageService->createLanguage($languageCreateStruct);
36
37
        $this->setReferences($language, $step);
0 ignored issues
show
Documentation introduced by
$language is of type object<eZ\Publish\API\Re...alues\Content\Language>, but the function expects a object<Object>|object<Ka...ctCollectionCollection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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
    /**
61
     * Handles the language delete migration action
62
     *
63
     * @todo use a matcher for flexible matching?
64
     */
65
    protected function delete($step)
66
    {
67
        if (!isset($step->dsl['lang'])) {
68
            throw new \Exception("The 'lang' key is required to delete a language.");
69
        }
70
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
     * @param Language $language
81
     * @param array $references the definitions of the references to set
82
     * @throws \InvalidArgumentException When trying to assign a reference to an unsupported attribute
83
     * @return array key: the reference names, values: the reference values
84
     */
85
    protected function getReferencesValues(Language $language, array $references)
86
    {
87
        $refs = array();
88
89 View Code Duplication
        foreach ($references as $reference) {
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...
90
91
            switch ($reference['attribute']) {
92
                case 'language_id':
93
                case 'id':
94
                    $value = $language->id;
95
                    break;
96
                case 'enabled':
97
                    $value = $language->enabled;
98
                    break;
99
                case 'language_code':
100
                    $value = $language->languageCode;
101
                    break;
102
                case 'language_name':
103
                case 'name':
104
                    $value = $language->name;
105
                    break;
106
                default:
107
                    throw new \InvalidArgumentException('Language Manager does not support setting references for attribute ' . $reference['attribute']);
108
            }
109
110
            $refs[$reference['identifier']] = $value;
111
        }
112
113
        return $refs;
114
    }
115
}
116