GridFactory::create()   A
last analyzed

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