Completed
Push — 1.0 ( 2156a0...733517 )
by Valentin
07:09
created

LanguageManager   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 138
Duplicated Lines 11.59 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 19
c 3
b 0
f 2
lcom 1
cbo 9
dl 16
loc 138
ccs 56
cts 56
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A update() 0 13 2
A setLogger() 0 4 1
A find() 16 16 4
A create() 0 20 3
A createOrUpdate() 0 14 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\Exception\ObjectNotFoundException;
22
use Transfer\EzPlatform\Repository\Values\LanguageObject;
23
use Transfer\EzPlatform\Exception\UnsupportedObjectOperationException;
24
use Transfer\EzPlatform\Repository\Manager\Type\CreatorInterface;
25
use Transfer\EzPlatform\Repository\Manager\Type\FinderInterface;
26
use Transfer\EzPlatform\Repository\Manager\Type\RemoverInterface;
27
use Transfer\EzPlatform\Repository\Manager\Type\UpdaterInterface;
28
29
/**
30
 * Content type manager.
31
 *
32
 * @internal
33
 *
34
 * @author Harald Tollefsen <[email protected]>
35
 */
36
class LanguageManager implements LoggerAwareInterface, CreatorInterface, UpdaterInterface, RemoverInterface, FinderInterface
37
{
38
    /**
39
     * @var Repository
40
     */
41
    private $repository;
42
43
    /**
44
     * @var LoggerInterface Logger
45
     */
46
    private $logger;
47
48
    /**
49
     * @var LanguageService Language service
50
     */
51
    private $languageService;
52
53
    /**
54
     * @param Repository $repository
55
     */
56 6
    public function __construct(Repository $repository)
57
    {
58 6
        $this->repository = $repository;
59 6
        $this->languageService = $repository->getContentLanguageService();
60 6
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 22
    public function setLogger(LoggerInterface $logger)
66
    {
67 22
        $this->logger = $logger;
68 22
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 37 View Code Duplication
    public function find(ValueObject $object)
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...
74
    {
75
        try {
76 37
            if (isset($object->data['code'])) {
77 37
                $language = $this->languageService->loadLanguage($object->data['code']);
78 36
            }
79 37
        } catch (NotFoundException $notFoundException) {
80
            // We'll throw our own exception later instead.
81
        }
82
83 37
        if (!isset($language)) {
84 3
            throw new ObjectNotFoundException(Language::class, array('code'));
85
        }
86
87 36
        return $language;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 36
    public function create(ObjectInterface $object)
94
    {
95 36
        if (!$object instanceof LanguageObject) {
96 1
            throw new UnsupportedObjectOperationException(LanguageObject::class, get_class($object));
97
        }
98
99
        try {
100 35
            $language = $this->find($object);
101 33
            $this->languageService->enableLanguage($language);
102 35
        } catch (NotFoundException $notFoundException) {
103 2
            $languageCreateStruct = $this->languageService->newLanguageCreateStruct();
104 2
            $languageCreateStruct->languageCode = $object->data['code'];
105 2
            $languageCreateStruct->name = $object->data['name'];
106 2
            $language = $this->languageService->createLanguage($languageCreateStruct);
107
        }
108
109 35
        $object->getMapper()->languageToObject($language);
110
111 35
        return $object;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117 2
    public function update(ObjectInterface $object)
118
    {
119 2
        if (!$object instanceof LanguageObject) {
120 1
            throw new UnsupportedObjectOperationException(LanguageObject::class, get_class($object));
121
        }
122
123 1
        $language = $this->find($object);
124 1
        $language = $this->languageService->updateLanguageName($language, $object->data['name']);
125
126 1
        $object->getMapper()->languageToObject($language);
127
128 1
        return $object;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 3
    public function createOrUpdate(ObjectInterface $object)
135
    {
136 3
        if (!$object instanceof LanguageObject) {
137 1
            throw new UnsupportedObjectOperationException(LanguageObject::class, get_class($object));
138
        }
139
140
        try {
141 2
            $this->find($object);
142
143 1
            return $this->update($object);
144 2
        } catch (ObjectNotFoundException $notFound) {
145 2
            return $this->create($object);
146 1
        }
147 1
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152 4
    public function remove(ObjectInterface $object)
153
    {
154 4
        if (!$object instanceof LanguageObject) {
155 1
            throw new UnsupportedObjectOperationException(LanguageObject::class, get_class($object));
156
        }
157
158
        try {
159 3
            $language = $this->find($object);
160 2
            $this->languageService->deleteLanguage($language);
161 3
        } catch (NotFoundException $e) {
162 2
            return true;
163 1
        } catch (InvalidArgumentException $ee) {
164 1
            if ($this->logger) {
165 1
                $this->logger->warning('Tried to delete the main language, or a language that still has existing translations (is in use).');
166 1
            }
167
168 1
            return false;
169
        }
170
171 1
        return true;
172
    }
173
}
174