Completed
Push — master ( 5d51b4...87fdd9 )
by Paul
10s
created

BusinessPageReferenceBuilder::buildReference()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 2
eloc 16
nc 2
nop 2
1
<?php
2
3
namespace Victoire\Bundle\BusinessPageBundle\Builder;
4
5
use Doctrine\ORM\EntityManager;
6
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage;
7
use Victoire\Bundle\CoreBundle\Entity\View;
8
use Victoire\Bundle\ViewReferenceBundle\Builder\BaseReferenceBuilder;
9
use Victoire\Bundle\ViewReferenceBundle\Helper\ViewReferenceHelper;
10
use Victoire\Bundle\ViewReferenceBundle\ViewReference\BusinessPageReference;
11
use Victoire\Bundle\ViewReferenceBundle\ViewReference\ViewReference;
12
13
/**
14
 * BusinessPageReferenceBuilder.
15
 */
16
class BusinessPageReferenceBuilder extends BaseReferenceBuilder
17
{
18
    /**
19
     * @param BusinessPage  $businessPage
20
     * @param EntityManager $em
21
     *
22
     * @return BusinessPageReference|ViewReference
23
     */
24
    public function buildReference(View $businessPage, EntityManager $em)
25
    {
26
        $referenceId = ViewReferenceHelper::generateViewReferenceId($businessPage);
27
        $businessPageReference = new BusinessPageReference();
28
        $businessPageReference->setId($referenceId);
29
        $businessPageReference->setLocale($businessPage->getCurrentLocale());
30
        $businessPageReference->setName($businessPage->getName());
31
        $businessPageReference->setViewId($businessPage->getId());
32
        $businessPageReference->setTemplateId($businessPage->getTemplate()->getId());
0 ignored issues
show
Bug introduced by
The method getId cannot be called on $businessPage->getTemplate() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
33
        $businessPageReference->setSlug($businessPage->getSlug());
34
        $businessPageReference->setEntityId($businessPage->getBusinessEntity()->getId());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Victoire\Bundle\CoreBundle\Entity\View as the method getBusinessEntity() does only exist in the following sub-classes of Victoire\Bundle\CoreBundle\Entity\View: Victoire\Bundle\Business...dle\Entity\BusinessPage, Victoire\Bundle\Business...ity\VirtualBusinessPage. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
35
        $businessPageReference->setEntityNamespace($em->getClassMetadata(get_class($businessPage->getBusinessEntity()))->name);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Victoire\Bundle\CoreBundle\Entity\View as the method getBusinessEntity() does only exist in the following sub-classes of Victoire\Bundle\CoreBundle\Entity\View: Victoire\Bundle\Business...dle\Entity\BusinessPage, Victoire\Bundle\Business...ity\VirtualBusinessPage. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
36
        $businessPageReference->setViewNamespace($em->getClassMetadata(get_class($businessPage))->name);
37
        if ($parent = $businessPage->getParent()) {
38
            $parent->setCurrentLocale($businessPage->getCurrentLocale());
39
            $businessPageReference->setParent(ViewReferenceHelper::generateViewReferenceId($parent));
40
        }
41
42
        return $businessPageReference;
43
    }
44
}
45