FileTablePopulator::populateTables()   B
last analyzed

Complexity

Conditions 8
Paths 7

Size

Total Lines 48
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
eloc 24
nc 7
nop 1
dl 0
loc 48
ccs 26
cts 26
cp 1
crap 8
rs 8.4444
c 0
b 0
f 0
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 League\Flysystem\AdapterInterface;
17
18
class FileTablePopulator implements TablePopulatorInterface
19
{
20
    /** @var AdapterInterface */
21
    private $filesystem;
22
    /** @var TableFilterer */
23
    private $tableFilterer;
24
25
    /**
26
     * SchemaParser constructor.
27
     *
28
     * @param AdapterInterface   $filesystem
29
     * @param TableFilterer|null $tableFilterer
30
     */
31 6
    public function __construct(AdapterInterface $filesystem, TableFilterer $tableFilterer = null)
32
    {
33 6
        $this->filesystem = $filesystem;
34 6
        $this->tableFilterer = $tableFilterer ?: new TableFilterer();
35 6
    }
36
37
    /**
38
     * @param ParsedSchema $parsedSchema
39
     *
40
     * @return ParsedSchema|null
41
     */
42 6
    public function populateTables(ParsedSchema $parsedSchema)
43
    {
44 6
        if (count($parsedSchema->getTables()) === 0) {
45 5
            if ($parsedSchema->getPath() === '' || $this->filesystem->has($parsedSchema->getPath()) === false) {
46 2
                return null;
47
            }
48
49
            // find existing tables
50 3
            $files = $this->filesystem->listContents($parsedSchema->getPath());
51 3
            $files = array_values(array_filter(
52 3
                $files,
53
                function (array $file) {
54
                    // ignore empty file names (`.bla`) files
55 2
                    return (pathinfo($file['path'], PATHINFO_FILENAME) !== '');
56 3
                }
57
            ));
58
59
            // sort by file size, largest first
60 3
            usort(
61 3
                $files,
62
                function (array $a, array $b) {
63 2
                    return ($a['size'] == $b['size']) ? 0 : (($a['size'] > $b['size']) ? -1 : 1);
64 3
                }
65
            );
66
67
            // remove the file extensions to get the table names
68 3
            $tables = array_map(
69 3
                function (array $file) {
70 2
                    return pathinfo($file['path'], PATHINFO_FILENAME);
71 3
                },
72 3
                $files
73
            );
74
75 3
            if (count($parsedSchema->getSchemaConfig()->getExcludes()) > 0) {
76 1
                $tables = $this->tableFilterer->filter(
77 1
                    $tables,
78 1
                    $parsedSchema->getSchemaConfig()->getExcludes()
79
                );
80
            }
81
82 3
            $parsedSchema->setTables($tables);
83
        }
84
85 4
        if (count($parsedSchema->getTables()) === 0) {
86 1
            return null;
87
        }
88
89 3
        return $parsedSchema;
90
    }
91
}
92