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

LanguageManager::create()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.0052

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 20
ccs 11
cts 12
cp 0.9167
rs 9.4285
cc 3
eloc 13
nc 4
nop 1
crap 3.0052
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 2
    public function __construct(Repository $repository)
56
    {
57 2
        $this->repository = $repository;
58 2
        $this->languageService = $repository->getContentLanguageService();
59 2
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 2
    public function setLogger(LoggerInterface $logger)
65
    {
66 2
        $this->logger = $logger;
67 2
    }
68
69
    /**
70
     * Find LanguageObject by code.
71
     * Returns Language.
72
     *
73
     * @param ValueObject $object
74
     * @param bool $throwExceptions
75
     *
76
     * @return Language
77
     * @throws NotFoundException
78
     */
79 13 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...
80
    {
81 13
        if (isset($object->data['code'])) {
82
            try {
83 13
                $language = $this->languageService->loadLanguage($object->data['code']);
84 13
            } catch (NotFoundException $notFoundException) {
85 1
                $exception = $notFoundException;
86
            }
87 13
        }
88
89 13
        if (!isset($language)) {
90 1
            if (isset($exception) && $throwExceptions) {
91 1
                throw $exception;
92
            }
93
94 1
            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...
95
        }
96
97 13
        return $language;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 13
    public function create(ObjectInterface $object)
104
    {
105 13
        if (!$object instanceof LanguageObject) {
106
            throw new UnsupportedObjectOperationException(LanguageObject::class, get_class($object));
107
        }
108
109
        try {
110 13
            $language = $this->find($object, true);
111 12
            $this->languageService->enableLanguage($language);
112 13
        } catch (NotFoundException $notFoundException) {
113 1
            $languageCreateStruct = $this->languageService->newLanguageCreateStruct();
114 1
            $languageCreateStruct->languageCode = $object->data['code'];
115 1
            $languageCreateStruct->name = $object->data['name'];
116 1
            $language = $this->languageService->createLanguage($languageCreateStruct);
117
        }
118
119 13
        $object->getMapper()->languageToObject($language);
120
121 13
        return $object;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127 1
    public function update(ObjectInterface $object)
128
    {
129 1
        if (!$object instanceof LanguageObject) {
130
            throw new UnsupportedObjectOperationException(LanguageObject::class, get_class($object));
131
        }
132
133 1
        $language = $this->find($object, true);
134 1
        $language = $this->languageService->updateLanguageName($language, $object->data['name']);
135
136 1
        $object->getMapper()->languageToObject($language);
137
138 1
        return $object;
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144 1
    public function createOrUpdate(ObjectInterface $object)
145
    {
146 1
        if (!$object instanceof LanguageObject) {
147 1
            throw new UnsupportedObjectOperationException(LanguageObject::class, get_class($object));
148
        }
149
150 1
        if (!$this->find($object)) {
151 1
            return $this->create($object);
152
        } else {
153 1
            return $this->update($object);
154
        }
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160 12
    public function remove(ObjectInterface $object)
161
    {
162 1
        if (!$object instanceof LanguageObject) {
163
            throw new UnsupportedObjectOperationException(LanguageObject::class, get_class($object));
164
        }
165
166
        try {
167
            $language = $this->find($object, true);
168
            $this->languageService->deleteLanguage($language);
169
        } catch (NotFoundException $e) {
170
            return true;
171
        } catch (InvalidArgumentException $ee) {
172
            /*
173
             * Tried to delete the main language, or a language
174
             * that still has existing translations (is in use).
175
             */
176
            return false;
177
        }
178
179
        return true;
180 12
    }
181
}
182