Completed
Push — master ( 1e7ae1...1641c1 )
by Axel
04:29
created

PollManager::getCurrentProjectPoll()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 1
dl 0
loc 11
ccs 7
cts 8
cp 0.875
crap 3.0175
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Manager\Project;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
7
8
use App\Event\Project\NewPollEvent;
9
10
use App\Entity\Project\{
11
    Details,
12
    Poll,
13
    Project
14
};
15
16
class PollManager
17
{
18
    /** @var EntityManagerInterface **/
19
    protected $em;
20
    /** @var EventDispatcherInterface **/
21
    protected $eventDispatcher;
22
    /** @var string **/
23
    protected $pollDuration;
24
    
25 1
    public function __construct(EntityManagerInterface $em, EventDispatcherInterface $eventDispatcher, string $pollDuration)
26
    {
27 1
        $this->em = $em;
28 1
        $this->eventDispatcher = $eventDispatcher;
29 1
        $this->pollDuration = $pollDuration;
30 1
    }
31
    
32
    public function createPoll(Project $project, Details $details): Poll
33
    {
34
        $poll =
35
            (new Poll())
36
            ->setProject($project)
37
            ->setDetails($details)
38
            ->setCreatedAt(new \DateTime())
39
            ->setEndedAt(new \DateTime($this->pollDuration))
40
        ;
41
        $this->em->persist($poll);
42
        $this->em->flush();
43
        $this->eventDispatcher->dispatch(NewPollEvent::NAME, new NewPollEvent($poll));
44
        return $poll;
45
    }
46
    
47
    public function get(int $id): ?Poll
48
    {
49
        return $this->em->getRepository(Poll::class)->find($id);
50
    }
51
    
52 1
    public function getCurrentProjectPoll(Project $project): ?Poll
53
    {
54 1
        $results = $this->em->getRepository(Poll::class)->findBy([
55 1
            'project' => $project
56
        ], [
57 1
            'createdAt' => 'DESC'
58 1
        ], 1);
59 1
        if (count($results) === 0 || $results[0]->getEndedAt() < new \DateTime()) {
60 1
            return null;
61
        }
62
        return $results[0];
63
    }
64
}