Completed
Push — master ( a23f84...2e53d7 )
by Michael
04:05
created

ElectionManager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A isElection() 0 4 1
A getEligibleVoters() 0 4 1
A calculateElectionResult() 0 4 1
A submitVotes() 0 4 1
A getElectionPollVotes() 0 4 1
A getElectionResults() 0 4 1
A getElectionWinner() 0 4 1
1
<?php
2
3
namespace AppBundle\Utils;
4
5
use Doctrine\ORM\EntityManager;
6
use AppBundle\Entity\Poll;
7
8
class ElectionManager
9
{
10
    protected $entityManager;
11
    protected $pollRepo;
12
13
    /**
14
     * Constructor
15
     *
16
     * @param EntityManager $entityManager
17
     */
18
    public function __construct(EntityManager $entityManager)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
19
    {
20
        $this->entityManager = $entityManager;
21
        $this->pollRepo = $this->entityManager->getRepository('AppBundle:Poll');
22
    }
23
24
    public function isElection(Poll $poll): boolean
0 ignored issues
show
Unused Code introduced by
The parameter $poll is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
25
    {
26
        return false;
27
    }
28
29
    public function getEligibleVoters(): array
30
    {
31
        return [];
32
    }
33
34
    public function calculateElectionResult(Poll $poll): array
35
    {
36
    	return getElectionResults($poll);
37
    }
38
39
    public function submitVotes(array $choices)
0 ignored issues
show
Unused Code introduced by
The parameter $choices is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41
        return;
42
    }
43
44
    public function getElectionPollVotes(Poll $poll): array
0 ignored issues
show
Unused Code introduced by
The parameter $poll is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
45
    {
46
    	return [];
47
    }
48
49
    public function getElectionResults(Poll $poll): array
0 ignored issues
show
Unused Code introduced by
The parameter $poll is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
    {
51
    	return [];
52
    }
53
54
    public function getElectionWinner(Poll $poll): AppBundle\Entity\Choice
0 ignored issues
show
Unused Code introduced by
The parameter $poll is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
    {
56
        return;
57
    }
58
}
59