BaseGallery::prePersist()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\MediaBundle\PHPCR;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Sonata\MediaBundle\Model\Gallery;
18
use Sonata\MediaBundle\Model\GalleryItemInterface;
19
20
/**
21
 * Bundle\MediaBundle\Document\BaseGallery.
22
 */
23
abstract class BaseGallery extends Gallery
24
{
25
    /**
26
     * @var string
27
     */
28
    private $uuid;
29
30
    public function __construct()
31
    {
32
        $this->galleryItems = new ArrayCollection();
33
    }
34
35
    /**
36
     * Get universal unique id.
37
     *
38
     * @return string
39
     */
40
    public function getUuid()
41
    {
42
        return $this->uuid;
43
    }
44
45
    public function addGalleryItem(GalleryItemInterface $galleryItem): void
46
    {
47
        $galleryItem->setGallery($this);
48
49
        // set nodename of GalleryItem
50
        if (!$galleryItem->getNodename()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sonata\MediaBundle\Model\GalleryItemInterface as the method getNodename() does only exist in the following implementations of said interface: Sonata\MediaBundle\PHPCR\BaseGalleryItem.

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
            $galleryItem->setNodename(
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sonata\MediaBundle\Model\GalleryItemInterface as the method setNodename() does only exist in the following implementations of said interface: Sonata\MediaBundle\PHPCR\BaseGalleryItem.

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...
52
                'media'.($this->galleryItems->count() + 1)
53
            );
54
        }
55
56
        $this->galleryItems->set($galleryItem->getNodename(), $galleryItem);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sonata\MediaBundle\Model\GalleryItemInterface as the method getNodename() does only exist in the following implementations of said interface: Sonata\MediaBundle\PHPCR\BaseGalleryItem.

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...
57
    }
58
59
    /**
60
     * Pre persist method.
61
     */
62
    public function prePersist(): void
63
    {
64
        $this->createdAt = new \DateTime();
65
        $this->updatedAt = new \DateTime();
66
67
        $this->reorderGalleryItems();
68
    }
69
70
    /**
71
     * Pre Update method.
72
     */
73
    public function preUpdate(): void
74
    {
75
        $this->updatedAt = new \DateTime();
76
77
        $this->reorderGalleryItems();
78
    }
79
}
80