League   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 64.58%

Importance

Changes 0
Metric Value
wmc 16
lcom 2
cbo 2
dl 0
loc 140
ccs 31
cts 48
cp 0.6458
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addTeam() 0 4 1
A getStandings() 0 16 2
A resetStandings() 0 6 2
A createFinalPositions() 0 6 2
A getTeamByPosition() 0 6 1
A getPositionByTeam() 0 12 3
A getTeams() 0 4 1
A addFixture() 0 4 1
A getId() 0 4 1
A getFixtures() 0 4 1
1
<?php
2
3
namespace OSS\CoreBundle\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\Mapping as ORM;
7
8
/**
9
 * @ORM\Entity
10
 */
11
class League
12
{
13
    /**
14
     * @var int
15
     *
16
     * @ORM\Id
17
     * @ORM\GeneratedValue
18
     * @ORM\Column(type="integer")
19
     */
20
    private $id;
21
22
    /**
23
     * @var ArrayCollection|Team[]
24
     *
25
     * @ORM\OneToMany(targetEntity="Team", mappedBy="league")
26
     */
27
    private $teams;
28
29
    /**
30
     * @var ArrayCollection|Fixture[]
31
     *
32
     * @ORM\OneToMany(targetEntity="Fixture", mappedBy="league")
33
     */
34
    private $fixtures;
35
36 6
    public function __construct()
37
    {
38 6
        $this->teams = new ArrayCollection();
39 6
        $this->fixtures = new ArrayCollection();
40 6
    }
41
42
    /**
43
     * @param Team $team
44
     */
45 6
    public function addTeam(Team $team)
46
    {
47 6
        $this->teams[] = $team;
48 6
    }
49
50
    /**
51
     * @return Team[]
52
     */
53 5
    public function getStandings()
54
    {
55
        /** @var Team[] $teams */
56 5
        $teams = $this->teams->toArray();
57 5
        $points = array();
58 5
        $goalsDifference = array();
59 5
        $goalsFor = array();
60 5
        foreach ($teams as $k => $team) {
61 5
            $points[$k] = $team->getPoints();
62 5
            $goalsDifference[$k] = $team->getGoalsDifference();
63 5
            $goalsFor[$k] = $team->getGoalsFor();
64 5
        }
65 5
        array_multisort($points, SORT_DESC, $goalsDifference, SORT_DESC, $goalsFor, SORT_DESC, $teams);
66
67 5
        return $teams;
68
    }
69
70
    public function resetStandings()
71
    {
72
        foreach ($this->teams as $team) {
73
            $team->resetPointsAndGoals();
74
        }
75
    }
76
77
    /**
78
     * @param int $season
79
     */
80
    public function createFinalPositions($season)
81
    {
82
        foreach ($this->teams as $team) {
83
            $team->createFinalPosition($season);
84
        }
85
    }
86
87
    /**
88
     * @param int $position
89
     *
90
     * @return Team
91
     */
92 3
    public function getTeamByPosition($position)
93
    {
94 3
        $standings = $this->getStandings();
95
96 3
        return $standings[$position - 1];
97
    }
98
99
    /**
100
     * @param Team $team
101
     *
102
     * @return int
103
     *
104
     * @throws \Exception
105
     */
106 1
    public function getPositionByTeam(Team $team)
107
    {
108 1
        $standings = $this->getStandings();
109
110 1
        foreach ($standings as $index => $t) {
111 1
            if ($t->equals($team)) {
112 1
                return $index + 1;
113
            }
114 1
        }
115
116
        throw new \Exception('team not found in standings');
117
    }
118
119
    /**
120
     * @return ArrayCollection|Team[]
121
     */
122
    public function getTeams()
123
    {
124
        return $this->teams;
125
    }
126
127
    /**
128
     * @param Fixture $fixture
129
     */
130 1
    public function addFixture(Fixture $fixture)
131
    {
132 1
        $this->fixtures->add($fixture);
133 1
    }
134
135
    /**
136
     * @return int
137
     */
138
    public function getId()
139
    {
140
        return $this->id;
141
    }
142
143
    /**
144
     * @return ArrayCollection|Fixture[]
145
     */
146
    public function getFixtures()
147
    {
148
        return $this->fixtures;
149
    }
150
}
151