Completed
Push — master ( caf771...f662c4 )
by Gaetano
07:54
created

LanguageManager   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 99
rs 10
wmc 14
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 20 4
A update() 0 12 1
A delete() 0 11 2
C setReferences() 0 29 7
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
15
    /**
16
     * Handle the content create migration action
17
     */
18
    protected function create()
19
    {
20
        $languageService = $this->repository->getContentLanguageService();
21
22
        if (!isset($this->dsl['lang'])) {
23
            throw new \Exception("The 'lang' key is required to create a new language.");
24
        }
25
26
        $languageCreateStruct = $languageService->newLanguageCreateStruct();
27
        $languageCreateStruct->languageCode = $this->dsl['lang'];
28
        if (isset($this->dsl['name'])) {
29
            $languageCreateStruct->name = $this->dsl['name'];
30
        }
31
        if (isset($this->dsl['enabled'])) {
32
            $languageCreateStruct->name = (bool)$this->dsl['enabled'];
0 ignored issues
show
Documentation Bug introduced by
The property $name was declared of type string, but (bool) $this->dsl['enabled'] is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
33
        }
34
        $language = $languageService->createLanguage($languageCreateStruct);
35
36
        $this->setReferences($language);
37
    }
38
39
    /**
40
     * Handle the language update migration action
41
     *
42
     * @todo use a matcher for flexible matching?
43
     */
44
    protected function update()
45
    {
46
        throw new \Exception('Language update is not implemented yet');
47
48
        /*$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...
49
50
        if (!isset($this->dsl['lang'])) {
51
            throw new \Exception("The 'lang' key is required to update a language.");
52
        }
53
54
        $this->setReferences($language);*/
55
    }
56
57
    /**
58
     * Handle the language delete migration action
59
     */
60
    protected function delete()
61
    {
62
        if (!isset($this->dsl['lang'])) {
63
            throw new \Exception("The 'lang' key is required to delete a language.");
64
        }
65
66
        $languageService = $this->repository->getContentLanguageService();
67
        $language = $languageService->loadLanguage($this->dsl['lang']);
68
69
        $languageService->deleteLanguage($language);
70
    }
71
72
    /**
73
     * Sets references to certain content attributes.
74
     * The Content Manager currently supports setting references to object_id and location_id
75
     *
76
     * @param \eZ\Publish\API\Repository\Values\Content\Language|LanguageCollection $language
77
     * @throws \InvalidArgumentException When trying to set a reference to an unsupported attribute
78
     * @return boolean
79
     */
80
    protected function setReferences($language)
81
    {
82
        if (!array_key_exists('references', $this->dsl)) {
83
            return false;
84
        }
85
86
        if ($language instanceof LanguageCollection) {
87
            if (count($language) > 1) {
88
                throw new \InvalidArgumentException('Content Manager does not support setting references for creating/updating of multiple languages');
89
            }
90
            $language = reset($language);
91
        }
92
93
        foreach ($this->dsl['references'] as $reference) {
94
95
            switch ($reference['attribute']) {
96
                case 'language_id':
97
                case 'id':
98
                    $value = $language->id;
99
                    break;
100
                default:
101
                    throw new \InvalidArgumentException('Content Manager does not support setting references for attribute ' . $reference['attribute']);
102
            }
103
104
            $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...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...
105
        }
106
107
        return true;
108
    }
109
}
110