Completed
Pull Request — master (#123)
by Łukasz
02:59
created

ProductViewFactory::create()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 13
nc 8
nop 3
1
<?php
2
3
namespace Sylius\ShopApiPlugin\Factory;
4
5
use Sylius\Component\Attribute\Model\AttributeValueInterface;
6
use Sylius\Component\Core\Model\ChannelInterface;
7
use Sylius\Component\Core\Model\ProductImageInterface;
8
use Sylius\Component\Core\Model\ProductInterface;
9
use Sylius\Component\Core\Model\TaxonInterface;
10
use Sylius\ShopApiPlugin\View\ProductView;
11
12
final class ProductViewFactory implements ProductViewFactoryInterface
13
{
14
    /**
15
     * @var ImageViewFactoryInterface
16
     */
17
    private $imageViewFactory;
18
19
    /**
20
     * @var ProductAttributeValueViewFactoryInterface
21
     */
22
    private $attributeValueViewFactory;
23
24
    /**
25
     * @var string
26
     */
27
    private $fallback;
28
29
    /**
30
     * @param ImageViewFactoryInterface $imageViewFactory
31
     * @param ProductAttributeValueViewFactoryInterface $attributeValueViewFactory
32
     * @param string $fallback
33
     */
34
    public function __construct(
35
        ImageViewFactoryInterface $imageViewFactory,
36
        ProductAttributeValueViewFactoryInterface $attributeValueViewFactory,
37
        $fallback
38
    ) {
39
        $this->imageViewFactory = $imageViewFactory;
40
        $this->attributeValueViewFactory = $attributeValueViewFactory;
41
        $this->fallback = $fallback;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function create(ProductInterface $product, ChannelInterface $channel, $locale)
48
    {
49
        $productView = new ProductView();
50
        $productView->name = $product->getTranslation($locale)->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...
51
        $productView->code = $product->getCode();
52
        $productView->slug = $product->getTranslation($locale)->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...
53
54
        /** @var ProductImageInterface $image */
55
        foreach ($product->getImages() as $image) {
56
            $imageView = $this->imageViewFactory->create($image);
57
            $productView->images[] = $imageView;
58
        }
59
60
        /** @var TaxonInterface $taxon */
61
        foreach ($product->getTaxons() as $taxon) {
62
            $productView->taxons[] = $taxon->getCode();
63
        }
64
65
        /** @var AttributeValueInterface $attribute */
66
        foreach ($product->getAttributesByLocale($locale, $this->fallback) as $attribute) {
67
            $productView->attributes[] = $this->attributeValueViewFactory->create($attribute);
68
        }
69
70
        return $productView;
71
    }
72
}
73