Completed
Push — master ( 22789e...7fd0d6 )
by Craig
06:03
created

CapabilityApi   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 80
Duplicated Lines 7.5 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 6
loc 80
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A load() 0 11 3
A getExtensionsCapableOf() 0 8 3
A isCapable() 3 11 3
A getCapabilitiesOf() 3 8 2

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 Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\ExtensionsModule\Api;
13
14
use Zikula\ExtensionsModule\Api\ApiInterface\CapabilityApiInterface;
15
use Zikula\ExtensionsModule\Constant;
16
use Zikula\ExtensionsModule\Entity\ExtensionEntity;
17
use Zikula\ExtensionsModule\Entity\Repository\ExtensionRepository;
18
use Zikula\ExtensionsModule\Entity\RepositoryInterface\ExtensionRepositoryInterface;
19
20
/**
21
 * Class CapabilityApi
22
 */
23
class CapabilityApi implements CapabilityApiInterface
24
{
25
    /**
26
     * @var ExtensionRepository
27
     */
28
    private $extensionRepository;
29
30
    /**
31
     * @var ExtensionEntity[]
32
     */
33
    private $extensionsByCapability = [];
34
35
    /**
36
     * @var ExtensionEntity[]
37
     */
38
    private $extensionsByName = [];
39
40
    /**
41
     * Capability constructor.
42
     * @param ExtensionRepositoryInterface $extensionRepository
43
     */
44
    public function __construct(ExtensionRepositoryInterface $extensionRepository)
45
    {
46
        $this->extensionRepository = $extensionRepository;
47
    }
48
49
    /**
50
     * Load extensions into private property cache.
51
     */
52
    private function load()
53
    {
54
        $extensions = $this->extensionRepository->findBy(['state' => Constant::STATE_ACTIVE]);
55
        /** @var ExtensionEntity $extension */
56
        foreach ($extensions as $extension) {
57
            foreach ($extension->getCapabilities() as $capability => $definition) {
58
                $this->extensionsByCapability[$capability][] = $extension;
59
            }
60
            $this->extensionsByName[$extension->getName()] = $extension;
61
        }
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getExtensionsCapableOf($capability)
68
    {
69
        if (empty($this->extensionsByCapability[$capability])) {
70
            $this->load();
71
        }
72
73
        return !empty($this->extensionsByCapability[$capability]) ? $this->extensionsByCapability[$capability] : [];
0 ignored issues
show
Bug Compatibility introduced by
The expression !empty($this->extensions...$capability] : array(); of type Zikula\ExtensionsModule\...y\ExtensionEntity|array adds the type Zikula\ExtensionsModule\Entity\ExtensionEntity to the return on line 73 which is incompatible with the return type declared by the interface Zikula\ExtensionsModule\...:getExtensionsCapableOf of type Zikula\ExtensionsModule\Entity\ExtensionEntity[].
Loading history...
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function isCapable($extensionName, $requestedCapability)
80
    {
81 View Code Duplication
        if (empty($this->extensionsByName[$extensionName])) {
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...
82
            $this->extensionsByName[$extensionName] = $this->extensionRepository->findOneBy(['name' => $extensionName]);
83
        }
84
        $capabilities = $this->extensionsByName[$extensionName]->getCapabilities();
85
86
        return array_key_exists($requestedCapability, $capabilities)
87
            ? $capabilities[$requestedCapability]
88
            : false;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getCapabilitiesOf($extensionName)
95
    {
96 View Code Duplication
        if (empty($this->extensionsByName[$extensionName])) {
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...
97
            $this->extensionsByName[$extensionName] = $this->extensionRepository->findOneBy(['name' => $extensionName]);
98
        }
99
100
        return $this->extensionsByName[$extensionName]->getCapabilities();
101
    }
102
}
103