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

LanguageManager::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2.0116
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 3
    public function __construct(Repository $repository)
56
    {
57 3
        $this->repository = $repository;
58 3
        $this->languageService = $repository->getContentLanguageService();
59 3
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 3
    public function setLogger(LoggerInterface $logger)
65 1
    {
66 3
        $this->logger = $logger;
67 3
    }
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 16 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 16
        if (isset($object->data['code'])) {
83
            try {
84 16
                $language = $this->languageService->loadLanguage($object->data['code']);
85 16
            } catch (NotFoundException $notFoundException) {
86 2
                $exception = $notFoundException;
87
            }
88 16
        }
89
90 16
        if (!isset($language)) {
91 2
            if (isset($exception) && $throwExceptions) {
92 2
                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 16
        return $language;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 16
    public function create(ObjectInterface $object)
105
    {
106 16
        if (!$object instanceof LanguageObject) {
107
            throw new UnsupportedObjectOperationException(LanguageObject::class, get_class($object));
108
        }
109
110
        try {
111 16
            $language = $this->find($object, true);
112 14
            $this->languageService->enableLanguage($language);
113 16
        } 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 16
        $object->getMapper()->languageToObject($language);
121
122 16
        return $object;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 1
    public function update(ObjectInterface $object)
129
    {
130 1
        if (!$object instanceof LanguageObject) {
131
            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 2
    public function createOrUpdate(ObjectInterface $object)
146 1
    {
147 2
        if (!$object instanceof LanguageObject) {
148
            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 14
    public function remove(ObjectInterface $object)
162 1
    {
163 1
        if (!$object instanceof LanguageObject) {
164
            throw new UnsupportedObjectOperationException(LanguageObject::class, get_class($object));
165
        }
166
167
        try {
168 1
            $language = $this->find($object, true);
169 1
            $this->languageService->deleteLanguage($language);
170 1
        } catch (NotFoundException $e) {
171
            return true;
172
        } catch (InvalidArgumentException $ee) {
173
            if($this->logger) {
174
                $this->logger->warning('Tried to delete the main language, or a language that still has existing translations (is in use).');
175
            }
176
            return false;
177
        }
178
179 1
        return true;
180 14
    }
181
}
182