Completed
Push — translation-class ( c4acc3 )
by Kamil
18:30
created

Taxon::createTranslation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Component\Taxonomy\Model;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\Common\Collections\Collection;
16
use Sylius\Component\Resource\Model\TranslatableTrait;
17
use Sylius\Component\Resource\Model\TranslationInterface;
18
19
/**
20
 * @author Paweł Jędrzejewski <[email protected]>
21
 * @author Gonzalo Vilaseca <[email protected]>
22
 */
23
class Taxon implements TaxonInterface
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: addTranslation, getTranslation, getTranslations, hasTranslation, removeTranslation, setCurrentLocale, setFallbackLocale
Loading history...
24
{
25
    use TranslatableTrait {
26
        __construct as private initializeTranslationsCollection;
27
    }
28
29
    /**
30
     * @var mixed
31
     */
32
    protected $id;
33
34
    /**
35
     * @var string
36
     */
37
    protected $code;
38
39
    /**
40
     * @var TaxonInterface
41
     */
42
    protected $root;
43
44
    /**
45
     * @var TaxonInterface
46
     */
47
    protected $parent;
48
49
    /**
50
     * @var Collection|TaxonInterface[]
51
     */
52
    protected $children;
53
54
    /**
55
     * @var int
56
     */
57
    protected $left;
58
59
    /**
60
     * @var int
61
     */
62
    protected $right;
63
64
    /**
65
     * @var int
66
     */
67
    protected $level;
68
69
    /**
70
     * @var int
71
     */
72
    protected $position;
73
74
    public function __construct()
75
    {
76
        $this->initializeTranslationsCollection();
77
78
        $this->children = new ArrayCollection();
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function __toString()
85
    {
86
        return (string) $this->getTranslation()->__toString();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\Resourc...el\TranslationInterface as the method __toString() does only exist in the following implementations of said interface: Sylius\Component\Payment...aymentMethodTranslation, Sylius\Component\Shippin...ippingMethodTranslation, Sylius\Component\Taxonomy\Model\TaxonTranslation.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function getId()
93
    {
94
        return $this->id;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function getCode()
101
    {
102
        return $this->code;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function setCode($code)
109
    {
110
        $this->code = $code;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function isRoot()
117
    {
118
        return null === $this->parent;
119
    }
120
121
    /**
122
     * @return TaxonInterface
123
     */
124
    public function getRoot()
125
    {
126
        return $this->root;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function getParent()
133
    {
134
        return $this->parent;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function setParent(TaxonInterface $parent = null)
141
    {
142
        $this->parent = $parent;
143
        if (null !== $parent) {
144
            $parent->addChild($this);
145
        }
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function getParents()
152
    {
153
        if (null === $parent = $this->getParent()) {
154
            return [];
155
        }
156
157
        $parents = [$parent];
158
159
        while (null !== $parent->getParent()) {
160
            $parents[] = $parent = $parent->getParent();
161
        }
162
163
        return $parents;
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function getChildren()
170
    {
171
        return $this->children;
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177
    public function hasChild(TaxonInterface $taxon)
178
    {
179
        return $this->children->contains($taxon);
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185
    public function addChild(TaxonInterface $taxon)
186
    {
187
        if (!$this->hasChild($taxon)) {
188
            $this->children->add($taxon);
189
        }
190
191
        if ($this !== $taxon->getParent()) {
192
            $taxon->setParent($this);
193
        }
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function removeChild(TaxonInterface $taxon)
200
    {
201
        if ($this->hasChild($taxon)) {
202
            $taxon->setParent(null);
203
204
            $this->children->removeElement($taxon);
205
        }
206
    }
207
208
    /**
209
     * {@inheritdoc}
210
     */
211
    public function getName()
212
    {
213
        return $this->getTranslation()->getName();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\Resourc...el\TranslationInterface as the method getName() does only exist in the following implementations of said interface: Sylius\Component\Attribu...el\AttributeTranslation, Sylius\Component\Core\Model\ProductTranslation, Sylius\Component\Payment...aymentMethodTranslation, Sylius\Component\Product...uctAttributeTranslation, Sylius\Component\Product...roductOptionTranslation, Sylius\Component\Product\Model\ProductTranslation, Sylius\Component\Shippin...ippingMethodTranslation, Sylius\Component\Taxonomy\Model\TaxonTranslation.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
214
    }
215
216
    /**
217
     * {@inheritdoc}
218
     */
219
    public function setName($name)
220
    {
221
        $this->getTranslation()->setName($name);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\Resourc...el\TranslationInterface as the method setName() does only exist in the following implementations of said interface: Sylius\Component\Attribu...el\AttributeTranslation, Sylius\Component\Core\Model\ProductTranslation, Sylius\Component\Payment...aymentMethodTranslation, Sylius\Component\Product...uctAttributeTranslation, Sylius\Component\Product...roductOptionTranslation, Sylius\Component\Product\Model\ProductTranslation, Sylius\Component\Shippin...ippingMethodTranslation, Sylius\Component\Taxonomy\Model\TaxonTranslation.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
222
    }
223
224
    /**
225
     * {@inheritdoc}
226
     */
227
    public function getSlug()
228
    {
229
        return $this->getTranslation()->getSlug();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\Resourc...el\TranslationInterface as the method getSlug() does only exist in the following implementations of said interface: Sylius\Component\Core\Model\ProductTranslation, Sylius\Component\Product\Model\ProductTranslation, Sylius\Component\Taxonomy\Model\TaxonTranslation.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
230
    }
231
232
    /**
233
     * {@inheritdoc}
234
     */
235
    public function setSlug($slug = null)
236
    {
237
        $this->getTranslation()->setSlug($slug);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\Resourc...el\TranslationInterface as the method setSlug() does only exist in the following implementations of said interface: Sylius\Component\Core\Model\ProductTranslation, Sylius\Component\Product\Model\ProductTranslation, Sylius\Component\Taxonomy\Model\TaxonTranslation.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
238
    }
239
240
    /**
241
     * {@inheritdoc}
242
     */
243
    public function getDescription()
244
    {
245
        return $this->getTranslation()->getDescription();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\Resourc...el\TranslationInterface as the method getDescription() does only exist in the following implementations of said interface: Sylius\Component\Core\Model\ProductTranslation, Sylius\Component\Payment...aymentMethodTranslation, Sylius\Component\Product\Model\ProductTranslation, Sylius\Component\Shippin...ippingMethodTranslation, Sylius\Component\Taxonomy\Model\TaxonTranslation.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
246
    }
247
248
    /**
249
     * {@inheritdoc}
250
     */
251
    public function setDescription($description)
252
    {
253
        $this->getTranslation()->setDescription($description);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\Resourc...el\TranslationInterface as the method setDescription() does only exist in the following implementations of said interface: Sylius\Component\Core\Model\ProductTranslation, Sylius\Component\Payment...aymentMethodTranslation, Sylius\Component\Product\Model\ProductTranslation, Sylius\Component\Shippin...ippingMethodTranslation, Sylius\Component\Taxonomy\Model\TaxonTranslation.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
254
    }
255
256
    /**
257
     * {@inheritdoc}
258
     */
259
    public function getLeft()
260
    {
261
        return $this->left;
262
    }
263
264
    /**
265
     * {@inheritdoc}
266
     */
267
    public function setLeft($left)
268
    {
269
        $this->left = $left;
270
    }
271
272
    /**
273
     * {@inheritdoc}
274
     */
275
    public function getRight()
276
    {
277
        return $this->right;
278
    }
279
280
    /**
281
     * {@inheritdoc}
282
     */
283
    public function setRight($right)
284
    {
285
        $this->right = $right;
286
    }
287
288
    /**
289
     * {@inheritdoc}
290
     */
291
    public function getLevel()
292
    {
293
        return $this->level;
294
    }
295
296
    /**
297
     * {@inheritdoc}
298
     */
299
    public function setLevel($level)
300
    {
301
        $this->level = $level;
302
    }
303
304
    /**
305
     * {@inheritdoc}
306
     */
307
    public function getPosition()
308
    {
309
        return $this->position;
310
    }
311
312
    /**
313
     * {@inheritdoc}
314
     */
315
    public function setPosition($position)
316
    {
317
        $this->position = $position;
318
    }
319
320
    /**
321
     * {@inheritdoc}
322
     */
323
    protected function createTranslation()
324
    {
325
        return new TaxonTranslation();
326
    }
327
}
328