Completed
Push — hatethisshit ( eaff83 )
by Kamil
22:55
created

ShippingMethod::getCategoryRequirementLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
nc 1
cc 1
eloc 5
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
declare(strict_types=1);
13
14
namespace Sylius\Component\Shipping\Model;
15
16
use Sylius\Component\Resource\Model\ArchivableTrait;
17
use Sylius\Component\Resource\Model\TimestampableTrait;
18
use Sylius\Component\Resource\Model\ToggleableTrait;
19
use Sylius\Component\Resource\Model\TranslatableTrait;
20
21
/**
22
 * @author Paweł Jędrzejewski <[email protected]>
23
 * @author Gonzalo Vilaseca <[email protected]>
24
 */
25
class ShippingMethod implements ShippingMethodInterface
26
{
27
    use ArchivableTrait, TimestampableTrait, ToggleableTrait;
28
    use TranslatableTrait {
29
        __construct as private initializeTranslationsCollection;
30
    }
31
32
    /**
33
     * @var mixed
34
     */
35
    protected $id;
36
37
    /**
38
     * @var string
39
     */
40
    protected $code;
41
42
    /**
43
     * @var int
44
     */
45
    protected $position;
46
47
    /**
48
     * @var ShippingCategoryInterface
49
     */
50
    protected $category;
51
52
    /**
53
     * @var int
54
     */
55
    protected $categoryRequirement = ShippingMethodInterface::CATEGORY_REQUIREMENT_MATCH_ANY;
56
57
    /**
58
     * @var string
59
     */
60
    protected $calculator;
61
62
    /**
63
     * @var array
64
     */
65
    protected $configuration = [];
66
67
    public function __construct()
68
    {
69
        $this->initializeTranslationsCollection();
70
71
        $this->createdAt = new \DateTime();
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public function __toString(): string
78
    {
79
        return $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...
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function getId()
86
    {
87
        return $this->id;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function getCode(): ?string
94
    {
95
        return $this->code;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function setCode(?string $code): void
102
    {
103
        $this->code = $code;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function getPosition(): ?int
110
    {
111
        return $this->position;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function setPosition(?int $position): void
118
    {
119
        $this->position = $position;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function getCategory(): ?ShippingCategoryInterface
126
    {
127
        return $this->category;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function setCategory(?ShippingCategoryInterface $category): void
134
    {
135
        $this->category = $category;
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function getCategoryRequirement(): ?int
142
    {
143
        return $this->categoryRequirement;
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function setCategoryRequirement(?int $categoryRequirement): void
150
    {
151
        $this->categoryRequirement = $categoryRequirement;
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function getName(): ?string
158
    {
159
        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...ociationTypeTranslation, Sylius\Component\Product...uctAttributeTranslation, Sylius\Component\Product...roductOptionTranslation, Sylius\Component\Product\Model\ProductTranslation, Sylius\Component\Product...oductVariantTranslation, 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...
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function setName(?string $name): void
166
    {
167
        $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...ociationTypeTranslation, Sylius\Component\Product...uctAttributeTranslation, Sylius\Component\Product...roductOptionTranslation, Sylius\Component\Product\Model\ProductTranslation, Sylius\Component\Product...oductVariantTranslation, 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...
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    public function getDescription(): ?string
174
    {
175
        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...
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181
    public function setDescription(?string $description): void
182
    {
183
        $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...
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189
    public function getCalculator(): ?string
190
    {
191
        return $this->calculator;
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197
    public function setCalculator(?string $calculator): void
198
    {
199
        $this->calculator = $calculator;
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205
    public function getConfiguration(): array
206
    {
207
        return $this->configuration;
208
    }
209
210
    /**
211
     * {@inheritdoc}
212
     */
213
    public function setConfiguration(array $configuration): void
214
    {
215
        $this->configuration = $configuration;
216
    }
217
218
    /**
219
     * {@inheritdoc}
220
     */
221
    protected function createTranslation(): ShippingMethodTranslationInterface
222
    {
223
        return new ShippingMethodTranslation();
224
    }
225
}
226