Passed
Pull Request — master (#3)
by Harry
03:37 queued 01:14
created

SchemaParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Graze\Sprout\Parser;
4
5
use Graze\Sprout\Config;
6
7
class SchemaParser
8
{
9
    /** @var Config */
10
    private $config;
11
    /** @var null|string */
12
    private $group;
13
    /** @var TablePopulator */
14
    private $populator;
15
16
    /**
17
     * SchemaParser constructor.
18
     *
19
     * @param TablePopulator $populator
20
     * @param Config         $config
21
     * @param string|null    $group
22
     */
23 3
    public function __construct(TablePopulator $populator, Config $config, string $group = null)
24
    {
25 3
        $this->populator = $populator;
26 3
        $this->config = $config;
27 3
        $this->group = $group;
28 3
    }
29
30
    /**
31
     * @param array $schemaTables
32
     *
33
     * @return ParsedSchema[]
34
     */
35 3
    public function extractSchemas(array $schemaTables = [])
36
    {
37 3
        if (count($schemaTables) === 0) {
38
            $schemaTables = array_map(
39
                function (Config\SchemaConfigInterface $schemaConfig) {
40
                    return $schemaConfig->getSchema();
41
                },
42
                $this->config->get(Config::CONFIG_SCHEMAS)
43
            );
44
        }
45
46 3
        $parsedSchemas = [];
47 3
        foreach ($schemaTables as $schemaTable) {
48 3
            if (preg_match('/^([a-z0-9_]+):(.+)$/i', $schemaTable, $matches)) {
49 2
                $schema = $matches[1];
50 2
                $tables = explode(',', $matches[2]);
51
            } else {
52 2
                $schema = $schemaTable;
53 2
                $tables = [];
54
            }
55
56 3
            $parsedSchemas[] = new ParsedSchema(
57 3
                $this->config->getSchemaConfiguration($schema),
58 3
                $this->config->getSchemaPath($this->config->getSchemaConfiguration($schema), $this->group),
59 3
                $tables
60
            );
61
        };
62
63 3
        return array_filter(array_map([$this->populator, 'populateTables'], $parsedSchemas));
64
    }
65
}
66