Completed
Push — master ( 0eab77...b2f729 )
by Paul
05:35
created

BusinessPageReferenceBuilder::buildReference()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 31
Code Lines 25

Duplication

Lines 7
Ratio 22.58 %

Importance

Changes 0
Metric Value
dl 7
loc 31
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 25
nc 6
nop 2
1
<?php
2
3
namespace Victoire\Bundle\BusinessPageBundle\Builder;
4
5
use Doctrine\ORM\EntityManager;
6
use Symfony\Component\PropertyAccess\PropertyAccessor;
7
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage;
8
use Victoire\Bundle\CoreBundle\Entity\View;
9
use Victoire\Bundle\ViewReferenceBundle\Builder\BaseReferenceBuilder;
10
use Victoire\Bundle\ViewReferenceBundle\Helper\ViewReferenceHelper;
11
use Victoire\Bundle\ViewReferenceBundle\ViewReference\BusinessPageReference;
12
use Victoire\Bundle\ViewReferenceBundle\ViewReference\ViewReference;
13
14
/**
15
 * BusinessPageReferenceBuilder.
16
 */
17
class BusinessPageReferenceBuilder extends BaseReferenceBuilder
18
{
19
    /**
20
     * @param BusinessPage  $businessPage
21
     * @param EntityManager $em
22
     *
23
     * @return BusinessPageReference|ViewReference
24
     */
25
    public function buildReference(View $businessPage, EntityManager $em)
26
    {
27
        $businessEntity = $businessPage->getEntityProxy()->getBusinessEntity();
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 getEntityProxy() 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...
28
        $entity = $businessPage->getEntityProxy()->getEntity();
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 getEntityProxy() 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...
29
        $accessor = new PropertyAccessor();
30
        $entityId = null;
31 View Code Duplication
        if ($entity) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
            if (method_exists($entity, 'getId')) {
33
                $entityId = $entity->getId();
34
            } else {
35
                $entityId = $accessor->getValue($entity, $businessEntity->getBusinessIdentifiers()->first()->getName());
36
            }
37
        }
38
        $referenceId = ViewReferenceHelper::generateViewReferenceId($businessPage);
39
        $businessPageReference = new BusinessPageReference();
40
        $businessPageReference->setId($referenceId);
41
        $businessPageReference->setLocale($businessPage->getCurrentLocale());
42
        $businessPageReference->setName($businessPage->getName());
43
        $businessPageReference->setViewId($businessPage->getId());
44
        $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...
45
        $businessPageReference->setSlug($businessPage->getSlug());
46
        $businessPageReference->setEntityId($entityId);
47
        $businessPageReference->setBusinessEntity($businessEntity->getId());
48
        $businessPageReference->setViewNamespace($em->getClassMetadata(get_class($businessPage))->name);
49
        if ($parent = $businessPage->getParent()) {
50
            $parent->setCurrentLocale($businessPage->getCurrentLocale());
51
            $businessPageReference->setParent(ViewReferenceHelper::generateViewReferenceId($parent));
52
        }
53
54
        return $businessPageReference;
55
    }
56
}
57