Completed
Pull Request — master (#77)
by Łukasz
02:27
created

ProductViewFactory::create()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 14
nc 6
nop 3
1
<?php
2
3
namespace Sylius\ShopApiPlugin\Factory;
4
5
use Sylius\Component\Core\Model\ChannelInterface;
6
use Sylius\Component\Core\Model\ProductImageInterface;
7
use Sylius\Component\Core\Model\ProductInterface;
8
use Sylius\Component\Core\Model\ProductVariantInterface;
9
use Sylius\ShopApiPlugin\View\ProductVariantView;
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 ProductVariantViewFactoryInterface
21
     */
22
    private $variantViewFactory;
23
24
    /**
25
     * @param ImageViewFactoryInterface $imageViewFactory
26
     * @param ProductVariantViewFactoryInterface $variantViewFactory
27
     */
28
    public function __construct(ImageViewFactoryInterface $imageViewFactory, ProductVariantViewFactoryInterface $variantViewFactory)
29
    {
30
        $this->imageViewFactory = $imageViewFactory;
31
        $this->variantViewFactory = $variantViewFactory;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function create(ProductInterface $product, ChannelInterface $channel, $locale)
38
    {
39
        $productView = new ProductView();
40
        $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...
41
        $productView->code = $product->getCode();
42
        $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...
43
44
        /** @var ProductVariantInterface $variant */
45
        foreach ($product->getVariants() as $variant) {
46
            $productView->variants[$variant->getCode()] = $this->variantViewFactory->create($variant, $channel, $locale);
47
        }
48
49
        /** @var ProductImageInterface $image */
50
        foreach ($product->getImages() as $image) {
51
            $imageView = $this->imageViewFactory->create($image);
52
            $productView->images[] = $imageView;
53
54
            foreach ($image->getProductVariants() as $productVariant) {
55
                /** @var ProductVariantView $variantView */
56
                $variantView = $productView->variants[$productVariant->getCode()];
57
58
                $variantView->images[] = $imageView;
59
            }
60
        }
61
62
        return $productView;
63
    }
64
}
65