Completed
Push — master ( 7e6070...203e38 )
by Gerrit
02:38
created

DataLoaderLazyLoadProxy   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 27.03 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 84%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 20
loc 74
ccs 21
cts 25
cp 0.84
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A storeDBALDataForEntity() 0 4 1
A prepareOnMetadataLoad() 0 4 1
A loadDataLoader() 20 20 3
A loadDBALDataForEntity() 0 4 1
A removeDBALDataForEntity() 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
 * Copyright (C) 2017  Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 * @license GPL-3.0
8
 * @author Gerrit Addiks <[email protected]>
9
 */
10
11
namespace Addiks\RDMBundle\DataLoader;
12
13
use Addiks\RDMBundle\DataLoader\DataLoaderInterface;
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
use Doctrine\ORM\EntityManagerInterface;
16
use Doctrine\ORM\Mapping\ClassMetadata;
17
use ErrorException;
18
19
final class DataLoaderLazyLoadProxy implements DataLoaderInterface
20
{
21
22
    /**
23
     * @var ContainerInterface
24
     */
25
    private $container;
26
27
    /**
28
     * @var string
29
     */
30
    private $serviceId;
31
32
    /**
33
     * @var ?DataLoaderInterface
34
     */
35
    private $loadedDataLoader;
36
37 9
    public function __construct(ContainerInterface $container, string $serviceId)
38
    {
39 9
        $this->container = $container;
40 9
        $this->serviceId = $serviceId;
41 9
    }
42
43
    /**
44
     * @param object $entity
45
     */
46 1
    public function loadDBALDataForEntity($entity, EntityManagerInterface $entityManager): array
47
    {
48 1
        return $this->loadDataLoader()->loadDBALDataForEntity($entity, $entityManager);
49
    }
50
51
    /**
52
     * @param object $entity
53
     */
54 3
    public function storeDBALDataForEntity($entity, EntityManagerInterface $entityManager): void
55
    {
56 3
        $this->loadDataLoader()->storeDBALDataForEntity($entity, $entityManager);
57 3
    }
58
59
    /**
60
     * @param object $entity
61
     */
62 1
    public function removeDBALDataForEntity($entity, EntityManagerInterface $entityManager): void
63
    {
64 1
        $this->loadDataLoader()->removeDBALDataForEntity($entity, $entityManager);
65 1
    }
66
67 5
    public function prepareOnMetadataLoad(EntityManagerInterface $entityManager, ClassMetadata $classMetadata): void
68
    {
69 5
        $this->loadDataLoader()->prepareOnMetadataLoad($entityManager, $classMetadata);
70 5
    }
71
72 8 View Code Duplication
    private function loadDataLoader(): DataLoaderInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
73
    {
74 8
        if (is_null($this->loadedDataLoader)) {
75
            /** @var object $loadedDataLoader */
76 8
            $loadedDataLoader = $this->container->get($this->serviceId);
77
78 8
            if ($loadedDataLoader instanceof DataLoaderInterface) {
79 8
                $this->loadedDataLoader = $loadedDataLoader;
80
81
            } else {
82
                throw new ErrorException(sprintf(
83
                    "Service with id '%s' must implement %s",
84
                    $this->serviceId,
85
                    DataLoaderInterface::class
86
                ));
87
            }
88
        }
89
90 8
        return $this->loadedDataLoader;
91
    }
92
}
93