StatementRepository   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 23.73 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 14
loc 59
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 4
A findStatementById() 7 7 1
A findVoidedStatementById() 7 7 1
A findStatementsBy() 0 7 1
A storeStatement() 0 7 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 xAPI package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
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 XApi\Repository\Doctrine\Test;
13
14
use Doctrine\Common\Persistence\ObjectManager as LegacyObjectManager;
15
use Doctrine\Persistence\ObjectManager;
16
use Xabbuh\XApi\Model\Actor;
17
use Xabbuh\XApi\Model\Statement;
18
use Xabbuh\XApi\Model\StatementId;
19
use Xabbuh\XApi\Model\StatementsFilter;
20
use XApi\Repository\Api\StatementRepositoryInterface;
21
22
/**
23
 * Statement repository clearing the object manager between read and write operations.
24
 *
25
 * @author Christian Flothmann <[email protected]>
26
 */
27
final class StatementRepository implements StatementRepositoryInterface
28
{
29
    private $repository;
30
    private $objectManager;
31
32
    public function __construct(StatementRepositoryInterface $repository, $objectManager)
33
    {
34
        if (!$objectManager instanceof ObjectManager && !$objectManager instanceof LegacyObjectManager) {
35
            throw new \TypeError(sprintf('The second argument of %s() must be an instance of %s (%s given).', __METHOD__, ObjectManager::class, is_object($objectManager) ? get_class($objectManager) : gettype($objectManager)));
36
        }
37
38
        $this->repository = $repository;
39
        $this->objectManager = $objectManager;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 View Code Duplication
    public function findStatementById(StatementId $statementId, Actor $authority = null)
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...
46
    {
47
        $statement = $this->repository->findStatementById($statementId, $authority);
48
        $this->objectManager->clear();
49
50
        return $statement;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 View Code Duplication
    public function findVoidedStatementById(StatementId $voidedStatementId, Actor $authority = null)
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...
57
    {
58
        $statement = $this->repository->findVoidedStatementById($voidedStatementId, $authority);
59
        $this->objectManager->clear();
60
61
        return $statement;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function findStatementsBy(StatementsFilter $criteria, Actor $authority = null)
68
    {
69
        $statements = $this->repository->findStatementsBy($criteria, $authority);
70
        $this->objectManager->clear();
71
72
        return $statements;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function storeStatement(Statement $statement, $flush = true)
79
    {
80
        $statementId = $this->repository->storeStatement($statement);
81
        $this->objectManager->clear();
82
83
        return $statementId;
84
    }
85
}
86