Completed
Pull Request — master (#133)
by Damian
20:49 queued 18:16
created

LanguageManager::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 2.0116
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 2
    protected function create($step)
19
    {
20 2
        $languageService = $this->repository->getContentLanguageService();
21
22 2
        if (!isset($step->dsl['lang'])) {
23
            throw new \Exception("The 'lang' key is required to create a new language.");
24
        }
25
26 2
        $languageCreateStruct = $languageService->newLanguageCreateStruct();
27 2
        $languageCreateStruct->languageCode = $step->dsl['lang'];
28 2
        if (isset($step->dsl['name'])) {
29 2
            $languageCreateStruct->name = $step->dsl['name'];
30
        }
31 2
        if (isset($step->dsl['enabled'])) {
32
            $languageCreateStruct->enabled = (bool)$step->dsl['enabled'];
33
        }
34 2
        $language = $languageService->createLanguage($languageCreateStruct);
35
36 2
        $this->setReferences($language, $step);
37
38 2
        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
     * Handles the language delete migration action
61
     *
62
     * @todo use a matcher for flexible matching?
63
     */
64 1
    protected function delete($step)
65
    {
66 1
        if (!isset($step->dsl['lang'])) {
67
            throw new \Exception("The 'lang' key is required to delete a language.");
68
        }
69
70 1
        $languageService = $this->repository->getContentLanguageService();
71 1
        $language = $languageService->loadLanguage($step->dsl['lang']);
72
73 1
        $languageService->deleteLanguage($language);
74
75 1
        return $language;
76
    }
77
78
    /**
79
     * Sets references to certain language attributes.
80
     *
81
     * @param \eZ\Publish\API\Repository\Values\Content\Language|LanguageCollection $language
82
     * @throws \InvalidArgumentException When trying to set a reference to an unsupported attribute
83
     * @return boolean
84
     */
85 2 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...
86
    {
87 2
        if (!array_key_exists('references', $step->dsl)) {
88 1
            return false;
89
        }
90
91 1
        $references = $this->setReferencesCommon($language, $step->dsl['references']);
92 1
        $language = $this->insureSingleEntity($language, $references);
93
94 1
        foreach ($references as $reference) {
95
96 1
            switch ($reference['attribute']) {
97 1
                case 'language_id':
98 1
                case 'id':
99 1
                    $value = $language->id;
100 1
                    break;
101
                case 'enabled':
102
                    $value = $language->enabled;
103
                    break;
104
                case 'language_code':
105
                    $value = $language->languageCode;
106
                    break;
107
                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 1
            $overwrite = false;
116 1
            if (isset($reference['overwrite'])) {
117
                $overwrite = $reference['overwrite'];
118
            }
119 1
            $this->referenceResolver->addReference($reference['identifier'], $value, $overwrite);
120
        }
121
122 1
        return true;
123
    }
124
}
125