Completed
Pull Request — master (#91)
by
unknown
06:19
created

LanguageManager::create()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.128

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 22
c 1
b 0
f 0
ccs 12
cts 15
cp 0.8
rs 8.9197
cc 4
eloc 13
nc 5
nop 0
crap 4.128
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Executor;
4
5
use Kaliop\eZMigrationBundle\API\Collection\LanguageCollection;
6
7
/**
8
 * Implements the actions for managing (create/update/delete) Languages in the system through
9
 * migrations and abstracts away the eZ Publish Public API.
10
 */
11
class LanguageManager extends RepositoryExecutor
12
{
13
    protected $supportedStepTypes = array('language');
14
    protected $supportedActions = array('create', 'delete');
15
16
    /**
17
     * Handle the content create migration action
18
     */
19 1
    protected function create()
20
    {
21 1
        $languageService = $this->repository->getContentLanguageService();
22
23 1
        if (!isset($this->dsl['lang'])) {
24
            throw new \Exception("The 'lang' key is required to create a new language.");
25
        }
26
27 1
        $languageCreateStruct = $languageService->newLanguageCreateStruct();
28 1
        $languageCreateStruct->languageCode = $this->dsl['lang'];
29 1
        if (isset($this->dsl['name'])) {
30 1
            $languageCreateStruct->name = $this->dsl['name'];
31 1
        }
32 1
        if (isset($this->dsl['enabled'])) {
33
            $languageCreateStruct->enabled = (bool)$this->dsl['enabled'];
34
        }
35 1
        $language = $languageService->createLanguage($languageCreateStruct);
36
37 1
        $this->setReferences($language);
38
39 1
        return $language;
40
    }
41
42
    /**
43
     * Handle the language update migration action
44
     *
45
     * @todo use a matcher for flexible matching?
46
     */
47
    protected function update()
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($this->dsl['lang'])) {
54
            throw new \Exception("The 'lang' key is required to update a language.");
55
        }
56
57
        $this->setReferences($language);*/
58
    }
59
60
    /**
61
     * Handle the language delete migration action
62
     */
63 1
    protected function delete()
64
    {
65 1
        if (!isset($this->dsl['lang'])) {
66
            throw new \Exception("The 'lang' key is required to delete a language.");
67
        }
68
69 1
        $languageService = $this->repository->getContentLanguageService();
70 1
        $language = $languageService->loadLanguage($this->dsl['lang']);
71
72 1
        $languageService->deleteLanguage($language);
73
74 1
        return $language;
75
    }
76
77
    /**
78
     * Sets references to certain content attributes.
79
     * The Content Manager currently supports setting references to object_id and location_id
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 1
    protected function setReferences($language)
86
    {
87 1
        if (!array_key_exists('references', $this->dsl)) {
88
            return false;
89
        }
90
91 1
        if ($language instanceof LanguageCollection) {
92
            if (count($language) > 1) {
93
                throw new \InvalidArgumentException('Content Manager does not support setting references for creating/updating of multiple languages');
94
            }
95
            $language = reset($language);
96
        }
97
98 1
        foreach ($this->dsl['references'] as $reference) {
99
100 1
            switch ($reference['attribute']) {
101 1
                case 'language_id':
102 1
                case 'id':
103 1
                    $value = $language->id;
104 1
                    break;
105
                default:
106
                    throw new \InvalidArgumentException('Content Manager does not support setting references for attribute ' . $reference['attribute']);
107 1
            }
108
109 1
            $this->referenceResolver->addReference($reference['identifier'], $value);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Kaliop\eZMigrationBundle...erenceResolverInterface as the method addReference() does only exist in the following implementations of said interface: Kaliop\eZMigrationBundle...ver\ChainPrefixResolver, Kaliop\eZMigrationBundle...ver\ChainRegexpResolver, Kaliop\eZMigrationBundle...eResolver\ChainResolver, Kaliop\eZMigrationBundle...CustomReferenceResolver.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
110 1
        }
111
112 1
        return true;
113
    }
114
}
115