SprintManager   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 21
dl 0
loc 47
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getCurrentSprint() 0 3 1
A __construct() 0 4 1
A getPreviousSprint() 0 3 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 6
    public function __construct(ObjectManager $om, EventDispatcherInterface $eventDispatcher)
24
    {
25 6
        $this->om = $om;
26 6
        $this->eventDispatcher = $eventDispatcher;
27 6
    }
28
    
29 1
    public function getCurrentSprint(): ?Sprint
30
    {
31 1
        return $this->om->getRepository(Sprint::class)->getCurrentSprint();
32
    }
33
    
34 1
    public function getPreviousSprint(): ?Sprint
35
    {
36 1
        return $this->om->getRepository(Sprint::class)->getPreviousSprint();
37
    }
38
    
39 1
    public function get(int $id): ?Sprint
40
    {
41 1
        return $this->om->getRepository(Sprint::class)->find($id);
42
    }
43
    
44 3
    public function createSprint(\DateTime $beginAt, \DateTime $endedAt): Sprint
45
    {
46 3
        if ($beginAt >= $endedAt) {
47 1
            throw new BadRequestHttpException('The begin date must preceed the end date');
48
        }
49 2
        if (count($existingSprints = $this->om->getRepository(Sprint::class)->getSprintByPeriod($beginAt, $endedAt)) > 0) {
50 1
            throw new ConflictHttpException(
51 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')}]"
52
            );
53
        }
54
        $sprint =
55 1
            (new Sprint())
56 1
            ->setBeginAt($beginAt)
57 1
            ->setEndedAt($endedAt)
58
        ;
59 1
        $this->om->persist($sprint);
60 1
        $this->om->flush();
61 1
        $this->eventDispatcher->dispatch(SprintCreationEvent::NAME, new SprintCreationEvent($sprint));
62 1
        return $sprint;
63
    }
64
}