LanguageManager   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 2
Metric Value
wmc 19
c 5
b 0
f 2
lcom 1
cbo 7
dl 0
loc 131
ccs 54
cts 54
cp 1
rs 10

7 Methods

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