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

PollManager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getCurrentPolls() 0 6 1
A getEligibleVoters() 0 4 1
A getAllPolls() 0 6 1
A getPollStats() 0 7 1
A getPollResult() 0 7 1
A getStandardPollVotes() 0 4 1
A markPollClosed() 0 4 1
1
<?php
2
3
namespace AppBundle\Utils;
4
5
use Doctrine\ORM\EntityManager;
6
use AppBundle\Entity\Poll;
7
8
class PollManager
9
{
10
    protected $entityManager;
11
    protected $pollRepo;
12
    protected $electionManager;
13
14
	/**
15
	 * Constructor
16
	 *
17
	 * @param EntityManager $entityManager
18
	 */
19
	public function __construct(EntityManager $entityManager, ElectionManager $electionManager)
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...
20
	{
21
		$this->entityManager = $entityManager;
22
		$this->repo = $this->entityManager->getRepository('AppBundle:Poll');
0 ignored issues
show
Bug introduced by
The property repo does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
23
        $this->electionManager = $electionManager;
24
	}
25
26
	/**
27
     * Get an array of current polls (objects)
28
     *
29
     * @return array
30
     */
31
    public function getCurrentPolls(): array
32
    {
33
        $currentPolls = $this->repo->findByActive(true);
34
35
        return $currentPolls;
36
    }
37
38
    /**
39
     * Get an array of eligible votes
40
     *
41
     * @param  AppBundle/Entity/PollType $type
0 ignored issues
show
Documentation introduced by
The doc-type AppBundle/Entity/PollType could not be parsed: Unknown type name "AppBundle/Entity/PollType" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
42
     * @return array
43
     */
44
    public function getEligibleVoters(PollType $type): array
0 ignored issues
show
Unused Code introduced by
The parameter $type 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
    /**
50
     * Get an array of all polls (objects)
51
     *
52
     * @return array
53
     */
54
    public function getAllPolls(): array
55
    {
56
    	$polls = $this->repo->findAll();
57
58
    	return $polls;
59
    }
60
61
    /**
62
     * Get poll statistics
63
     *
64
     * @param  Poll   $poll
65
     * @return array
66
     */
67
    public function getPollStats(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...
68
    {
69
    	// If Poll is election then reject
70
71
    	// Get standard stats
72
    	return [];
73
    }
74
75
    /**
76
     * Get the result of the poll (in terms of a choice)
77
     *
78
     * @param  Poll   $poll
79
     * @return AppBundle/Entity/Choice
0 ignored issues
show
Documentation introduced by
The doc-type AppBundle/Entity/Choice could not be parsed: Unknown type name "AppBundle/Entity/Choice" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
80
     */
81
    public function getPollResult(Poll $poll): 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...
82
    {
83
    	// If election then call Election Manager
84
85
    	// Calculate winning choice
86
    	return;
87
    }
88
89
    public function getStandardPollVotes(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...
90
    {
91
    	return [];
92
    }
93
94
    public function markPollClosed(Poll $poll)
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...
95
    {
96
    	return;
97
    }
98
}
99