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

MysqlTableSeeder   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A seed() 0 19 2
A __construct() 0 5 1
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\Db\Mysql;
15
16
use Graze\ParallelProcess\Pool;
17
use Graze\Sprout\Config\ConnectionConfigInterface;
18
use Graze\Sprout\Seed\TableSeederInterface;
19
use InvalidArgumentException;
20
use League\Flysystem\AdapterInterface;
21
use Symfony\Component\Process\Process;
22
23
class MysqlTableSeeder implements TableSeederInterface
24
{
25
    /** @var ConnectionConfigInterface */
26
    private $connection;
27
    /** @var Pool */
28
    private $pool;
29
    /** @var AdapterInterface */
30
    private $fileSystem;
31
32
    /**
33
     * MysqlTableDumper constructor.
34
     *
35
     * @param Pool                      $pool
36
     * @param ConnectionConfigInterface $connection
37
     * @param AdapterInterface          $fileSystem
38
     */
39
    public function __construct(Pool $pool, ConnectionConfigInterface $connection, AdapterInterface $fileSystem)
40
    {
41
        $this->pool = $pool;
42
        $this->connection = $connection;
43
        $this->fileSystem = $fileSystem;
44
    }
45
46
    /**
47
     * @param string $file
48
     * @param string $schema
49
     * @param string $table
50
     */
51
    public function seed(string $file, string $schema, string $table)
0 ignored issues
show
Coding Style introduced by
Unknown type hint "string" found for $file
Loading history...
Coding Style introduced by
Unknown type hint "string" found for $schema
Loading history...
Coding Style introduced by
Unknown type hint "string" found for $table
Loading history...
52
    {
53
        if ($this->fileSystem->has($file) === false) {
54
            throw new InvalidArgumentException("seed: The file: {$file} does not exist");
55
        }
56
57
        $process = new Process('');
58
        $process->setCommandLine(
59
            sprintf(
60
                'mysql -h%1$s -u%2$s -p%3$s --default-character-set=utf8 %4$s < %5$s',
61
                escapeshellarg($this->connection->getHost()),
62
                escapeshellarg($this->connection->getUser()),
63
                escapeshellarg($this->connection->getPassword()),
64
                escapeshellarg($schema),
65
                escapeshellarg($file)
66
            )
67
        );
68
69
        $this->pool->add($process, ['seed', 'schema' => $schema, 'table' => $table]);
70
    }
71
}
72