Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

Security/MaxNumberByIpEventListener.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\VotingBundle\EventListener\Security;
4
5
use Kunstmaan\VotingBundle\Event\EventInterface;
6
use Kunstmaan\VotingBundle\Services\RepositoryResolver;
7
8
/**
9
 * Security listener for prevent ip to vote more than maxnumber for an event
10
 */
11
class MaxNumberByIpEventListener
12
{
13
    /**
14
     * RepositoryResolver
15
     */
16
    protected $repositoryResolver;
17
18
    /**
19
     * Number
20
     */
21
    protected $maxNumber;
22
23
    /**
24
     * Constructor
25
     *
26
     * @param RepositoryResolver $repositoryResolver entity manager
27
     * @param int                $maxNumber          max number
28
     */
29
    public function __construct($repositoryResolver, $maxNumber)
30
    {
31
        $this->repositoryResolver = $repositoryResolver;
32
        $this->maxNumber = $maxNumber;
33
    }
34
35
    /**
36
     * On vote
37
     *
38
     * @param EventInterface $event event
39
     */
40
    public function onVote(EventInterface $event)
41
    {
42
        $repository = $this->repositoryResolver->getRepositoryForEvent($event);
0 ignored issues
show
$event is of type object<Kunstmaan\VotingB...e\Event\EventInterface>, but the function expects a object<Kunstmaan\VotingBundle\Services\Event>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43
44
        if (!$repository) {
45
            return;
46
        }
47
48
        $vote = $repository->countByReferenceAndByIp($event->getReference(), $event->getRequest()->getClientIp());
49
50
        if ($vote >= $this->maxNumber) {
51
            $event->stopPropagation();
52
        }
53
    }
54
}
55