Completed
Push — develop ( e322b0...461e3c )
by Barry
02:41
created

GridFactory::getCells()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Barryvanveen\CCA\Factories;
4
5
use Barryvanveen\CCA\Cell;
6
use Barryvanveen\CCA\Config;
7
use Barryvanveen\CCA\Coordinate;
8
use Barryvanveen\CCA\Grid;
9
use Barryvanveen\CCA\Neighborhood;
10
11
class GridFactory
12
{
13 3
    public static function create(Config $config)
14
    {
15 3
        self::setSeed($config);
16
17 3
        $cells = self::getCells($config);
18
19 3
        $neighbors = self::getNeighbors($config);
20
21 3
        return new Grid($config, $cells, $neighbors);
22
    }
23
24 3
    protected static function setSeed(Config $config)
25
    {
26 3
        mt_srand($config->seed());
27 3
    }
28
29 3
    protected static function getCells(Config $config)
30
    {
31 3
        $cells = [];
32
33 3
        for ($row = 0; $row < $config->rows(); $row++) {
34 3
            for ($column = 0; $column < $config->columns(); $column++) {
35 3
                $coordinate = new Coordinate($row, $column, $config->columns());
36
37 3
                $cells[$coordinate->position()] = new Cell($config);
38
            }
39
        }
40
41 3
        return $cells;
42
    }
43
44 3
    protected static function getNeighbors(Config $config)
45
    {
46 3
        $neighbors = [];
47
48 3
        for ($row = 0; $row < $config->rows(); $row++) {
49 3
            for ($column = 0; $column < $config->columns(); $column++) {
50 3
                $coordinate = new Coordinate($row, $column, $config->columns());
51
52 3
                $neighborhood = new Neighborhood($config, $coordinate);
53
54 3
                $neighbors[$coordinate->position()] = $neighborhood->getNeighbors();
55
            }
56
        }
57
58 3
        return $neighbors;
59
    }
60
}
61