Completed
Pull Request — master (#137)
by
unknown
401:51 queued 395:38
created

LanguageManager::upsert()   A

Complexity

Conditions 4
Paths 7

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 9
cp 0
rs 9.6666
c 0
b 0
f 0
cc 4
nc 7
nop 1
crap 20
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Executor;
4
5
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
6
use eZ\Publish\API\Repository\Values\Content\Language;
7
8
/**
9
 * Handles language migrations.
10
 */
11
class LanguageManager extends RepositoryExecutor
12
{
13
    protected $supportedStepTypes = array('language');
14
    protected $supportedActions = array('create', 'update', 'delete', 'upsert');
15
16
    /**
17
     * Handles the language create migration action
18
     */
19 2
    protected function create($step)
20
    {
21 2
        foreach (array('lang', 'name') as $key) {
22 2
            if (!isset($step->dsl[$key])) {
23 2
                throw new \Exception("The '$key' key is missing in a language creation definition");
24
            }
25
        }
26 2
        $languageService = $this->repository->getContentLanguageService();
27 2
        $languageCreateStruct = $languageService->newLanguageCreateStruct();
28 2
        $languageCreateStruct->languageCode = $step->dsl['lang'];
29 2
        $languageCreateStruct->name = $step->dsl['name'];
30 2
        if (isset($step->dsl['enabled'])) {
31
            $languageCreateStruct->enabled = (bool)$step->dsl['enabled'];
32
        }
33 2
        $language = $languageService->createLanguage($languageCreateStruct);
34
35 2
        $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...ion\AbstractCollection>.

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...
36
37 2
        return $language;
38
    }
39
40
    /**
41
     * Handles the language update migration action
42
     *
43
     * @todo use a matcher for flexible matching?
44
     */
45
    protected function update($step)
46
    {
47
        if (!isset($step->dsl['lang'])) {
48
            throw new \Exception("The 'lang' key is required to update a language.");
49
        }
50
51
        $languageService = $this->repository->getContentLanguageService();
52
        $language = $languageService->loadLanguage($step->dsl['lang']);
53
54
        if (isset($step->dsl['name'])) {
55
            $languageService->updateLanguageName($language, $step->dsl['name']);
56
        }
57
        if (isset($step->dsl['enabled'])) {
58
            if ((bool)$step->dsl['enabled']) {
59
                $languageService->enableLanguage($language);
60
            } else {
61
                $languageService->disableLanguage($language);
62
            }
63
        }
64
65
        $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...ion\AbstractCollection>.

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