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

GridFactory::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
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