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

SprintManager::createSprint()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 19
ccs 13
cts 13
cp 1
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
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
}