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

LanguageManager::remove()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 5
Bugs 0 Features 3
Metric Value
c 5
b 0
f 3
dl 0
loc 21
ccs 13
cts 13
cp 1
rs 8.7624
cc 5
eloc 13
nc 8
nop 1
crap 5
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
                if($throwExceptions) {
87 3
                    throw $notFoundException;
88
                }
89
            }
90 36
        }
91
92 36
        return isset($language) ? $language : false;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression isset($language) ? $language : false; of type eZ\Publish\API\Repositor...\Content\Language|false adds false to the return on line 92 which is incompatible with the return type documented by Transfer\EzPlatform\Repo...r\LanguageManager::find of type eZ\Publish\API\Repository\Values\Content\Language. It seems like you forgot to handle an error condition.
Loading history...
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 36
    public function create(ObjectInterface $object)
99
    {
100 36
        if (!$object instanceof LanguageObject) {
101 1
            throw new UnsupportedObjectOperationException(LanguageObject::class, get_class($object));
102
        }
103
104
        try {
105 35
            $language = $this->find($object, true);
106 33
            $this->languageService->enableLanguage($language);
107 35
        } catch (NotFoundException $notFoundException) {
108 2
            $languageCreateStruct = $this->languageService->newLanguageCreateStruct();
109 2
            $languageCreateStruct->languageCode = $object->data['code'];
110 2
            $languageCreateStruct->name = $object->data['name'];
111 2
            $language = $this->languageService->createLanguage($languageCreateStruct);
112
        }
113
114 35
        $object->getMapper()->languageToObject($language);
115
116 35
        return $object;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 2
    public function update(ObjectInterface $object)
123
    {
124 2
        if (!$object instanceof LanguageObject) {
125 1
            throw new UnsupportedObjectOperationException(LanguageObject::class, get_class($object));
126
        }
127
128 1
        $language = $this->find($object, true);
129 1
        $language = $this->languageService->updateLanguageName($language, $object->data['name']);
130
131 1
        $object->getMapper()->languageToObject($language);
132
133 1
        return $object;
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139 3
    public function createOrUpdate(ObjectInterface $object)
140
    {
141 3
        if (!$object instanceof LanguageObject) {
142 2
            throw new UnsupportedObjectOperationException(LanguageObject::class, get_class($object));
143
        }
144
145 2
        if (!$this->find($object)) {
146 2
            return $this->create($object);
147 1
        } else {
148 1
            return $this->update($object);
149 1
        }
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155 4
    public function remove(ObjectInterface $object)
156
    {
157 4
        if (!$object instanceof LanguageObject) {
158 1
            throw new UnsupportedObjectOperationException(LanguageObject::class, get_class($object));
159
        }
160
161
        try {
162 4
            $language = $this->find($object, true);
163 2
            $this->languageService->deleteLanguage($language);
164 3
        } catch (NotFoundException $e) {
165 1
            return true;
166 1
        } catch (InvalidArgumentException $ee) {
167 1
            if ($this->logger) {
168 1
                $this->logger->warning('Tried to delete the main language, or a language that still has existing translations (is in use).');
169 1
            }
170
171 1
            return false;
172
        }
173
174 1
        return true;
175
    }
176
}
177