Processor::process()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.6111
c 0
b 0
f 0
cc 5
nc 4
nop 2
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shapin\Datagen\DBAL;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\Schema\Schema;
9
use Shapin\Datagen\FixtureInterface;
10
use Shapin\Datagen\ProcessorInterface;
11
use Shapin\Datagen\ReferenceManager;
12
13
class Processor implements ProcessorInterface
14
{
15
    private $connection;
16
    private $referenceManager;
17
    private $schema;
18
19
    private $fixturesToLoad = [];
20
21 5
    public function __construct(Connection $connection, ReferenceManager $referenceManager)
22
    {
23 5
        $this->connection = $connection;
24 5
        $this->referenceManager = $referenceManager;
25 5
        $this->schema = new Schema();
26 5
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 5
    public function process(FixtureInterface $fixture, array $options = []): void
32
    {
33 5
        if ($fixture instanceof Table && !$this->resolveOption($options, 'fixtures_only', false)) {
34 4
            $fixture->addTableToSchema($this->schema);
35
        }
36
37 5
        if ($fixture instanceof FixtureCollection && !$this->resolveOption($options, 'schema_only', false)) {
38 4
            $this->fixturesToLoad[] = $fixture;
39
        }
40 5
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 5
    public function flush(array $options = []): void
46
    {
47 5
        if (!$this->resolveOption($options, 'fixtures_only', false)) {
48 4
            $statements = $this->schema->toSql($this->connection->getDatabasePlatform());
49 4
            foreach ($statements as $statement) {
50 4
                $this->connection->query($statement);
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\DBAL\Connection::query() has been deprecated with message: Use {@link executeQuery()} instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
51
            }
52
53
            // Reset schema
54 4
            $this->schema = new Schema();
55
        }
56
57 5
        if ($this->resolveOption($options, 'schema_only', false)) {
58 2
            return;
59
        }
60
61 4
        foreach ($this->fixturesToLoad as $fixtureCollection) {
62 4
            foreach ($fixtureCollection->getFixtures() as $key => $fixture) {
63 4
                $fields = $this->referenceManager->findAndReplace($fixture->getFields());
64
65 4
                $this->connection->insert($fixture->getTableName(), $fields, $fixture->getTypes());
66
67 4
                if (\is_string($key)) {
68
                    $this->referenceManager->add($fixture->getTableName(), $key, $fields);
69
                }
70
            }
71
        }
72 4
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 2
    public function getName(): string
78
    {
79 2
        return 'dbal';
80
    }
81
82 5
    private function resolveOption(array $options, string $key, $defaultValue = null)
83
    {
84 5
        if (!\array_key_exists($key, $options)) {
85 3
            return $defaultValue;
86
        }
87
88 4
        return $options[$key];
89
    }
90
}
91