Completed
Push — EZP-26057-permission-api ( abfa4f...880141 )
by
unknown
59:10
created

SignalDispatcherFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 6.41 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getSearchEngineAlias() 0 4 1
B addSlotsForSearchEngine() 5 15 5
A buildSignalDispatcher() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
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
namespace eZ\Publish\Core\Base\Container\ApiLoader\SignalSlot;
10
11
class SignalDispatcherFactory
12
{
13
    /**
14
     * Relative namespace for internal signals.
15
     */
16
    const RELATIVE_SIGNAL_NAMESPACE = 'eZ\\Publish\\Core\\SignalSlot\\Signal';
17
18
    /**
19
     * @var string
20
     */
21
    private $signalDispatcherClass;
22
23
    /**
24
     * @var array
25
     */
26
    private $signalSlotMap = [];
27
28
    /**
29
     * @var string
30
     */
31
    private $searchEngineAlias;
32
33
    /**
34
     * SignalDispatcherFactory constructor.
35
     *
36
     * @param string $signalDispatcherClass
37
     * @param $searchEngineAlias
38
     */
39
    public function __construct(
40
        $signalDispatcherClass,
41
        $searchEngineAlias
42
    ) {
43
        $this->signalDispatcherClass = $signalDispatcherClass;
44
        $this->searchEngineAlias = $searchEngineAlias;
45
    }
46
47
    /**
48
     * Get current search engine alias.
49
     *
50
     * @return string
51
     */
52
    public function getSearchEngineAlias()
53
    {
54
        return $this->searchEngineAlias;
55
    }
56
57
    /**
58
     * Bulk add all signal slots if needed for a search engine.
59
     *
60
     * @param string $searchEngineAlias
61
     * @param array $searchEngineSignalSlots [signal => array(slot1, slot2, ...)]
62
     */
63
    public function addSlotsForSearchEngine($searchEngineAlias, array $searchEngineSignalSlots)
64
    {
65
        if ($this->getSearchEngineAlias() !== $searchEngineAlias) {
66
            return;
67
        }
68
69
        foreach ($searchEngineSignalSlots as $signalIdentifier => $slots) {
70 View Code Duplication
            if ($signalIdentifier[0] === '\\') {
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...
71
                $signalIdentifier = substr($signalIdentifier, 1);
72
            } elseif ($signalIdentifier !== '*') {
73
                $signalIdentifier = static::RELATIVE_SIGNAL_NAMESPACE . "\\$signalIdentifier";
74
            }
75
            $this->signalSlotMap[$signalIdentifier] = $slots;
76
        }
77
    }
78
79
    /**
80
     * Build SignalDispatcher for SignalSlots.
81
     *
82
     * @return \eZ\Publish\Core\SignalSlot\SignalDispatcher
83
     */
84
    public function buildSignalDispatcher()
85
    {
86
        return new $this->signalDispatcherClass($this->signalSlotMap);
87
    }
88
}
89