SchemaParser::__construct()   A
last analyzed

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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * This file is part of graze/sprout.
4
 *
5
 * Copyright © 2018 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/sprout/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/sprout
12
 */
13
14
namespace Graze\Sprout\Parser;
15
16
use Graze\Sprout\Config\Config;
17
use Graze\Sprout\Config\SchemaConfigInterface;
18
19
class SchemaParser
20
{
21
    /** @var Config */
22
    private $config;
23
    /** @var null|string */
24
    private $group;
25
    /** @var TablePopulatorInterface */
26
    private $populator;
27
28
    /**
29
     * SchemaParser constructor.
30
     *
31
     * @param TablePopulatorInterface $populator
32
     * @param Config                  $config
33
     * @param string|null             $group
34
     */
35 4
    public function __construct(TablePopulatorInterface $populator, Config $config, string $group = null)
36
    {
37 4
        $this->populator = $populator;
38 4
        $this->config = $config;
39 4
        $this->group = $group;
40 4
    }
41
42
    /**
43
     * @param array $schemaTables
44
     *
45
     * @return ParsedSchema[]
46
     */
47 4
    public function extractSchemas(array $schemaTables = [])
48
    {
49 4
        if (count($schemaTables) === 0) {
50 1
            $schemaTables = array_map(
51 1
                function (SchemaConfigInterface $schemaConfig) {
52 1
                    return $schemaConfig->getSchema();
53 1
                },
54 1
                $this->config->get(Config::CONFIG_SCHEMAS)
0 ignored issues
show
Bug introduced by
It seems like $this->config->get(Graze...Config::CONFIG_SCHEMAS) can also be of type null; however, parameter $array of array_map() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
                /** @scrutinizer ignore-type */ $this->config->get(Config::CONFIG_SCHEMAS)
Loading history...
55
            );
56
        }
57
58 4
        $parsedSchemas = [];
59 4
        foreach ($schemaTables as $schemaTable) {
60 4
            if (preg_match('/^([a-z0-9_]+):(.+)$/i', $schemaTable, $matches)) {
61 2
                $schema = $matches[1];
62 2
                $tables = explode(',', $matches[2]);
63
            } else {
64 3
                $schema = $schemaTable;
65 3
                $tables = [];
66
            }
67
68 4
            $parsedSchemas[] = new ParsedSchema(
69 4
                $this->config->getSchemaConfiguration($schema),
70 4
                $this->config->getSchemaPath($this->config->getSchemaConfiguration($schema), $this->group),
71 4
                $tables
72
            );
73
        };
74
75 4
        return array_filter(array_map([$this->populator, 'populateTables'], $parsedSchemas));
76
    }
77
}
78