Completed
Push — master ( 61c842...2ec63c )
by Paweł
293:00 queued 278:23
created

ImageUploadListener::uploadProductImage()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
rs 6.7272
cc 7
eloc 14
nc 9
nop 1
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\Bundle\CoreBundle\EventListener;
13
14
use Sylius\Component\Core\Model\ProductInterface;
15
use Sylius\Component\Core\Model\ProductVariantInterface;
16
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
17
use Sylius\Component\Resource\Exception\UnexpectedTypeException;
18
use Sylius\Component\Taxonomy\Model\TaxonInterface;
19
use Symfony\Component\EventDispatcher\GenericEvent;
20
21
class ImageUploadListener
22
{
23
    /**
24
     * @var ImageUploaderInterface
25
     */
26
    protected $uploader;
27
28
    /**
29
     * @param ImageUploaderInterface $uploader
30
     */
31
    public function __construct(ImageUploaderInterface $uploader)
32
    {
33
        $this->uploader = $uploader;
34
    }
35
36
    /**
37
     * @param GenericEvent $event
38
     */
39
    public function uploadProductImage(GenericEvent $event)
40
    {
41
        $subject = $event->getSubject();
42
        if (!$subject instanceof ProductInterface && !$subject instanceof ProductVariantInterface) {
43
            throw new UnexpectedTypeException(
44
                $subject,
45
                'Sylius\Component\Core\Model\ProductInterface or Sylius\Component\Core\Model\ProductVariantInterface'
46
            );
47
        }
48
49
        $variant = $subject instanceof ProductVariantInterface ? $subject : $subject->getMasterVariant();
50
51
        if (null === $variant) {
52
            return;
53
        }
54
55
        $images = $variant->getImages();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\Variation\Model\VariantInterface as the method getImages() does only exist in the following implementations of said interface: Sylius\Component\Core\Model\ProductVariant.

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...
56
        foreach ($images as $image) {
57
            $this->uploader->upload($image);
58
59
            // Upload failed? Let's remove that image.
60
            if (null === $image->getPath()) {
61
                $images->removeElement($image);
62
            }
63
        }
64
    }
65
66
    /**
67
     * @param GenericEvent $event
68
     */
69
    public function uploadTaxonImage(GenericEvent $event)
70
    {
71
        $subject = $event->getSubject();
72
73
        if (!$subject instanceof TaxonInterface) {
74
            throw new UnexpectedTypeException(
75
                $subject,
76
                TaxonInterface::class
77
            );
78
        }
79
80
        if ($subject->hasFile()) {
81
            $this->uploader->upload($subject);
0 ignored issues
show
Documentation introduced by
$subject is of type object<Sylius\Component\...y\Model\TaxonInterface>, but the function expects a object<Sylius\Component\...e\Model\ImageInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82
        }
83
    }
84
}
85