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