Completed
Pull Request — 1.0 (#53)
by Harald
06:53
created

LanguageManager   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 148
Duplicated Lines 13.51 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 10
Bugs 0 Features 4
Metric Value
wmc 21
c 10
b 0
f 4
lcom 1
cbo 9
dl 20
loc 148
ccs 60
cts 60
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B find() 20 20 6
A setLogger() 0 4 1
A create() 0 20 3
A update() 0 13 2
A createOrUpdate() 0 12 3
B remove() 0 21 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of Transfer.
5
 *
6
 * For the full copyright and license information, please view the LICENSE file located
7
 * in the root directory.
8
 */
9
10
namespace Transfer\EzPlatform\Repository\Manager;
11
12
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
13
use eZ\Publish\API\Repository\LanguageService;
14
use eZ\Publish\API\Repository\Repository;
15
use eZ\Publish\API\Repository\Values\Content\Language;
16
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
17
use Psr\Log\LoggerAwareInterface;
18
use Psr\Log\LoggerInterface;
19
use Transfer\Data\ObjectInterface;
20
use Transfer\Data\ValueObject;
21
use Transfer\EzPlatform\Repository\Values\LanguageObject;
22
use Transfer\EzPlatform\Exception\UnsupportedObjectOperationException;
23
use Transfer\EzPlatform\Repository\Manager\Type\CreatorInterface;
24
use Transfer\EzPlatform\Repository\Manager\Type\FinderInterface;
25
use Transfer\EzPlatform\Repository\Manager\Type\RemoverInterface;
26
use Transfer\EzPlatform\Repository\Manager\Type\UpdaterInterface;
27
28
/**
29
 * Content type manager.
30
 *
31
 * @internal
32
 *
33
 * @author Harald Tollefsen <[email protected]>
34
 */
35
class LanguageManager implements LoggerAwareInterface, CreatorInterface, UpdaterInterface, RemoverInterface, FinderInterface
36
{
37
    /**
38
     * @var Repository
39
     */
40
    private $repository;
41
42
    /**
43
     * @var LoggerInterface Logger
44
     */
45
    private $logger;
46
47
    /**
48
     * @var LanguageService Language service
49
     */
50
    private $languageService;
51
52
    /**
53
     * @param Repository $repository
54
     */
55 6
    public function __construct(Repository $repository)
56
    {
57 6
        $this->repository = $repository;
58 6
        $this->languageService = $repository->getContentLanguageService();
59 6
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 22
    public function setLogger(LoggerInterface $logger)
65 1
    {
66 22
        $this->logger = $logger;
67 22
    }
68
69
    /**
70
     * Find LanguageObject by code.
71
     * Returns Language.
72
     *
73
     * @param ValueObject $object
74
     * @param bool        $throwExceptions
75
     *
76
     * @return Language
77
     *
78
     * @throws NotFoundException
79
     */
80 37 View Code Duplication
    public function find(ValueObject $object, $throwExceptions = false)
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...
81
    {
82 37
        if (isset($object->data['code'])) {
83
            try {
84 37
                $language = $this->languageService->loadLanguage($object->data['code']);
85 37
            } catch (NotFoundException $notFoundException) {
86 3
                $exception = $notFoundException;
87
            }
88 37
        }
89
90 37
        if (!isset($language)) {
91 3
            if (isset($exception) && $throwExceptions) {
92 3
                throw $exception;
93
            }
94
95 2
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Transfer\EzPlatform\Repo...r\LanguageManager::find of type eZ\Publish\API\Repository\Values\Content\Language.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
96
        }
97
98 36
        return $language;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 36
    public function create(ObjectInterface $object)
105
    {
106 36
        if (!$object instanceof LanguageObject) {
107 1
            throw new UnsupportedObjectOperationException(LanguageObject::class, get_class($object));
108
        }
109
110
        try {
111 35
            $language = $this->find($object, true);
112 33
            $this->languageService->enableLanguage($language);
113 35
        } catch (NotFoundException $notFoundException) {
114 2
            $languageCreateStruct = $this->languageService->newLanguageCreateStruct();
115 2
            $languageCreateStruct->languageCode = $object->data['code'];
116 2
            $languageCreateStruct->name = $object->data['name'];
117 2
            $language = $this->languageService->createLanguage($languageCreateStruct);
118
        }
119
120 35
        $object->getMapper()->languageToObject($language);
121
122 35
        return $object;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 2
    public function update(ObjectInterface $object)
129
    {
130 2
        if (!$object instanceof LanguageObject) {
131 1
            throw new UnsupportedObjectOperationException(LanguageObject::class, get_class($object));
132
        }
133
134 1
        $language = $this->find($object, true);
135 1
        $language = $this->languageService->updateLanguageName($language, $object->data['name']);
136
137 1
        $object->getMapper()->languageToObject($language);
138
139 1
        return $object;
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 3
    public function createOrUpdate(ObjectInterface $object)
146 1
    {
147 3
        if (!$object instanceof LanguageObject) {
148 1
            throw new UnsupportedObjectOperationException(LanguageObject::class, get_class($object));
149 1
        }
150
151 2
        if (!$this->find($object)) {
152 2
            return $this->create($object);
153
        } else {
154 1
            return $this->update($object);
155
        }
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 34
    public function remove(ObjectInterface $object)
162 1
    {
163 4
        if (!$object instanceof LanguageObject) {
164 1
            throw new UnsupportedObjectOperationException(LanguageObject::class, get_class($object));
165
        }
166
167
        try {
168 3
            $language = $this->find($object, true);
169 2
            $this->languageService->deleteLanguage($language);
170 3
        } catch (NotFoundException $e) {
171 1
            return true;
172 1
        } catch (InvalidArgumentException $ee) {
173 1
            if ($this->logger) {
174 1
                $this->logger->warning('Tried to delete the main language, or a language that still has existing translations (is in use).');
175 1
            }
176
177 1
            return false;
178
        }
179
180 34
        return true;
181
    }
182
}
183