Completed
Push — master ( b01ced...379682 )
by André
19:40
created

BlockMatcherFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 5
c 2
b 1
f 1
lcom 1
cbo 5
dl 0
loc 52
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A getMatcher() 0 8 2
A setSiteAccess() 0 8 2
1
<?php
2
3
/**
4
 * File containing the BlockMatcherFactory class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 *
9
 * @version //autogentag//
10
 */
11
namespace eZ\Bundle\EzPublishCoreBundle\Matcher;
12
13
use eZ\Publish\API\Repository\Repository;
14
use eZ\Publish\Core\MVC\ConfigResolverInterface;
15
use eZ\Publish\Core\MVC\Symfony\Matcher\BlockMatcherFactory as BaseFactory;
16
use eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessAware;
17
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
18
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
19
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
20
21
/**
22
 * @deprecated Deprecated since 6.0, will be removed in 6.1. Use the ServiceAwareMatcherFactory instead.
23
 */
24
class BlockMatcherFactory extends BaseFactory implements SiteAccessAware, ContainerAwareInterface
0 ignored issues
show
Deprecated Code introduced by
The class eZ\Publish\Core\MVC\Symf...her\BlockMatcherFactory has been deprecated with message: Deprecated since 6.0, will be removed in 6.1. Use the AbstractMatcherFactory instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
25
{
26
    use ContainerAwareTrait;
27
28
    /**
29
     * @var \eZ\Publish\Core\MVC\ConfigResolverInterface;
30
     */
31
    private $configResolver;
32
33
    public function __construct(ConfigResolverInterface $configResolver, Repository $repository)
34
    {
35
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
36
            "BlockMatcherFactory is deprecated, and will be removed in ezpublish-kernel 6.1.\n" .
37
            'Use the ServiceAwareMatcherFactory with the relative namespace as a constructor argument.',
38
            E_USER_DEPRECATED
39
        );
40
41
        $this->configResolver = $configResolver;
42
        parent::__construct(
43
            $repository,
44
            $this->configResolver->getParameter('block_view')
45
        );
46
    }
47
48
    /**
49
     * @param string $matcherIdentifier
50
     *
51
     * @return \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\MatcherInterface
52
     */
53
    protected function getMatcher($matcherIdentifier)
54
    {
55
        if ($this->container->has($matcherIdentifier)) {
56
            return $this->container->get($matcherIdentifier);
57
        }
58
59
        return parent::getMatcher($matcherIdentifier);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return parent::getMatcher($matcherIdentifier); (eZ\Publish\Core\MVC\Symf...\Block\MatcherInterface) is incompatible with the return type documented by eZ\Bundle\EzPublishCoreB...cherFactory::getMatcher of type eZ\Publish\Core\MVC\Symf...tBased\MatcherInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
60
    }
61
62
    /**
63
     * Changes internal configuration to use the one for passed SiteAccess.
64
     *
65
     * @param SiteAccess $siteAccess
66
     */
67
    public function setSiteAccess(SiteAccess $siteAccess = null)
68
    {
69
        if ($siteAccess === null) {
70
            return;
71
        }
72
73
        $this->matchConfig = $this->configResolver->getParameter('block_view', 'ezsettings', $siteAccess->name);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->configResolver->g...gs', $siteAccess->name) of type * is incompatible with the declared type array of property $matchConfig.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
74
    }
75
}
76