Completed
Push — master ( 30a2b5...89612e )
by Axel
02:59
created

SprintManager   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 20
dl 0
loc 42
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCurrentSprint() 0 3 1
A __construct() 0 4 1
A createSprint() 0 19 3
A get() 0 3 1
1
<?php
2
3
namespace Scrumban\Manager;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
7
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
8
9
use Scrumban\Event\SprintCreationEvent;
10
11
use Scrumban\Entity\Sprint;
12
13
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
14
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
15
16
class SprintManager
17
{
18
    /** @var ObjectManager **/
19
    protected $om;
20
    /** @var EventDispatcherInterface **/
21
    protected $eventDispatcher;
22
    
23 5
    public function __construct(ObjectManager $om, EventDispatcherInterface $eventDispatcher)
24
    {
25 5
        $this->om = $om;
26 5
        $this->eventDispatcher = $eventDispatcher;
27 5
    }
28
    
29 1
    public function getCurrentSprint(): ?Sprint
30
    {
31 1
        return $this->om->getRepository(Sprint::class)->getCurrentSprint();
32
    }
33
    
34 1
    public function get(int $id): ?Sprint
35
    {
36 1
        return $this->om->getRepository(Sprint::class)->find($id);
37
    }
38
    
39 3
    public function createSprint(\DateTime $beginAt, \DateTime $endedAt): Sprint
40
    {
41 3
        if ($beginAt >= $endedAt) {
42 1
            throw new BadRequestHttpException('The begin date must preceed the end date');
43
        }
44 2
        if (count($existingSprints = $this->om->getRepository(Sprint::class)->getSprintByPeriod($beginAt, $endedAt)) > 0) {
45 1
            throw new ConflictHttpException(
46 1
                "The given dates conflict with sprint {$existingSprints[0]->getId()} [{$existingSprints[0]->getBeginAt()->format('Y-m-d H:i:s')} - {$existingSprints[0]->getEndedAt()->format('Y-m-d H:i:s')}]"
47
            );
48
        }
49
        $sprint =
50 1
            (new Sprint())
51 1
            ->setBeginAt($beginAt)
52 1
            ->setEndedAt($endedAt)
53
        ;
54 1
        $this->om->persist($sprint);
55 1
        $this->om->flush();
56 1
        $this->eventDispatcher->dispatch(SprintCreationEvent::NAME, new SprintCreationEvent($sprint));
57 1
        return $sprint;
58
    }
59
}