MysqlTableSeeder   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 49
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A seed() 0 21 2
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 4
    public function __construct(Pool $pool, ConnectionConfigInterface $connection, AdapterInterface $fileSystem)
40
    {
41 4
        $this->pool = $pool;
42 4
        $this->connection = $connection;
43 4
        $this->fileSystem = $fileSystem;
44 4
    }
45
46
    /**
47
     * @param string $file
48
     * @param string $schema
49
     * @param string $table
50
     */
51 2
    public function seed(string $file, string $schema, string $table)
52
    {
53 2
        if ($this->fileSystem->has($file) === false) {
54 1
            throw new InvalidArgumentException("seed: The file: {$file} does not exist");
55
        }
56
57 1
        $process = new Process('');
58 1
        $process->setCommandLine(
59 1
            sprintf(
60 1
                '(echo %6$s; cat %5$s; echo %7$s) | mysql -h%1$s -u%2$s -p%3$s --max_allowed_packet=512M --default-character-set=utf8 %4$s',
61 1
                escapeshellarg($this->connection->getHost()),
62 1
                escapeshellarg($this->connection->getUser()),
63 1
                escapeshellarg($this->connection->getPassword()),
64 1
                escapeshellarg($schema),
65 1
                escapeshellarg($file),
66 1
                escapeshellarg('SET AUTOCOMMIT=0; SET FOREIGN_KEY_CHECKS=0;'),
67 1
                escapeshellarg('SET AUTOCOMMIT=1; SET FOREIGN_KEY_CHECKS=1;')
68
            )
69
        );
70
71 1
        $this->pool->add($process, ['seed', 'schema' => $schema, 'table' => $table]);
72 1
    }
73
}
74