FixtureService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace OSS\CoreBundle\Services;
4
5
use Doctrine\ORM\EntityManager;
6
use OSS\CoreBundle\Entity\Fixture;
7
use OSS\CoreBundle\Entity\League;
8
use OSS\CoreBundle\FixtureGenerator\Generator;
9
10
class FixtureService
11
{
12
    /**
13
     * @var EntityManager
14
     */
15
    private $entityManager;
16
17
    /**
18
     * @param EntityManager $entityManager
19
     */
20
    public function __construct(EntityManager $entityManager)
21
    {
22
        $this->entityManager = $entityManager;
23
    }
24
25
    /**
26
     * @param int $season
27
     *
28
     * @throws \Exception
29
     */
30
    public function createFixtures($season)
31
    {
32
        /** @var League $league */
33
        $league = $this->entityManager->getRepository('CoreBundle:League')->findOneBy(array());
34
        $generator = new Generator(count($league->getTeams()));
35
        $matches = $generator->createFixtures();
36
        $listOfTeams = $league->getTeams();
37
38
        foreach ($matches as $match) {
39
            $fixture = new Fixture();
40
            $fixture->setSeason($season);
41
            $fixture->setWeek($match->getWeek());
42
            $fixture->setTeamHome($listOfTeams[$match->getTeamHome() - 1]);
43
            $fixture->setTeamAway($listOfTeams[$match->getTeamAway() - 1]);
44
            $fixture->setLeague($league);
45
46
            $this->entityManager->persist($fixture);
47
        }
48
        $this->entityManager->flush();
49
    }
50
}
51