Completed
Pull Request — master (#124)
by Łukasz
02:54
created

ProductViewFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 58
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A create() 0 22 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 ProductAttributeValuesViewFactoryInterface
21
     */
22
    private $attributeValuesViewFactory;
23
24
    /**
25
     * @var string
26
     */
27
    private $fallback;
28
29
    /**
30
     * @param ImageViewFactoryInterface $imageViewFactory
31
     * @param ProductAttributeValuesViewFactoryInterface $attributeValuesViewFactory
32
     * @param string $fallback
33
     */
34
    public function __construct(
35
        ImageViewFactoryInterface $imageViewFactory,
36
        ProductAttributeValuesViewFactoryInterface $attributeValuesViewFactory,
37
        $fallback
38
    ) {
39
        $this->imageViewFactory = $imageViewFactory;
40
        $this->attributeValuesViewFactory = $attributeValuesViewFactory;
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
        $productView->attributes = $this->attributeValuesViewFactory->create($product->getAttributesByLocale($locale, $this->fallback));
0 ignored issues
show
Bug introduced by
It seems like $product->getAttributesB...ocale, $this->fallback) targeting Sylius\Component\Attribu...getAttributesByLocale() can also be of type object<Doctrine\Common\Collections\Collection>; however, Sylius\ShopApiPlugin\Fac...toryInterface::create() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
66
67
        return $productView;
68
    }
69
}
70