Passed
Push — master ( 03ea78...8fc551 )
by Harry
10:08
created

FileTablePopulator::populateTables()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 48
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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