Processor   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 96.88%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 4
dl 0
loc 78
ccs 31
cts 32
cp 0.9688
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A process() 0 10 5
B flush() 0 28 7
A getName() 0 4 1
A resolveOption() 0 8 2
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