Graphviz   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
eloc 40
dl 0
loc 141
ccs 0
cts 52
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A createNode() 0 9 2
A createNodeRelation() 0 3 1
A write() 0 3 1
A getOutput() 0 3 1
A acceptSchema() 0 6 1
A acceptTable() 0 7 1
A acceptForeignKey() 0 9 1
A createTableLabel() 0 27 4
1
<?php
2
3
namespace Bdf\Prime\Schema\Visitor;
4
5
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
6
use Doctrine\DBAL\Schema\Schema;
7
use Doctrine\DBAL\Schema\Table;
8
use Doctrine\DBAL\Schema\Visitor\AbstractVisitor;
9
10
/**
11
 * Create a Graphviz output of a Schema.
12
 *
13
 * @package Bdf\Prime\Schema\Visitor
14
 */
15
class Graphviz extends AbstractVisitor
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\DBAL\Schema\Visitor\AbstractVisitor has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

15
class Graphviz extends /** @scrutinizer ignore-deprecated */ AbstractVisitor
Loading history...
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $output = '';
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
26
    {
27
        $this->output .= $this->createNodeRelation(
28
            $fkConstraint->getLocalTableName().':col'.current($fkConstraint->getLocalColumns()).':se',
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\For...nt::getLocalTableName() has been deprecated: Use the table that contains the foreign key as part of its {@see Table::$_fkConstraints} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

28
            /** @scrutinizer ignore-deprecated */ $fkConstraint->getLocalTableName().':col'.current($fkConstraint->getLocalColumns()).':se',

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

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

Loading history...
29
            $fkConstraint->getForeignTableName().':col'.current($fkConstraint->getForeignColumns()).':se',
30
            [
31
                'dir'       => 'back',
32
                'arrowtail' => 'dot',
33
                'arrowhead' => 'normal',
34
            ]
35
        );
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function acceptSchema(Schema $schema)
42
    {
43
        $this->output  = 'digraph "' . sha1((string) mt_rand()) . '" {' . "\n";
44
        $this->output .= 'graph [fontname="helvetica", fontsize=12];' . "\n";
45
        $this->output .= 'node [fontname="helvetica", fontsize=12];' . "\n";
46
        $this->output .= 'edge [fontname="helvetica", fontsize=12];' . "\n";
47
//        $this->output .= 'splines = true;' . "\n";
48
//        $this->output .= 'overlap = false;' . "\n";
49
//        $this->output .= 'outputorder=edgesfirst;'."\n";
50
//        $this->output .= 'mindist = 0.6;' . "\n";
51
//        $this->output .= 'sep = .2;' . "\n";
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function acceptTable(Table $table)
58
    {
59
        $this->output .= $this->createNode(
60
            $table->getName(),
61
            [
62
                'label' => $this->createTableLabel($table),
63
                'shape' => 'plaintext',
64
            ]
65
        );
66
    }
67
68
    /**
69
     * @param \Doctrine\DBAL\Schema\Table $table
70
     *
71
     * @return string
72
     */
73
    protected function createTableLabel(Table $table)
74
    {
75
        // The title
76
        $label = '<tr><td border="0" colspan="2" align="center" bgcolor="#fcaf3e">'
77
            . $table->getName()
78
            . '</td></tr>';
79
80
        // The attributes block
81
        foreach ($table->getColumns() as $column) {
82
            $columnName = $column->getName();
83
84
            if ($table->hasPrimaryKey() && in_array($column->getName(), $table->getPrimaryKey()->getColumns())) {
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Table::hasPrimaryKey() has been deprecated: Use {@see getPrimaryKey()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

84
            if (/** @scrutinizer ignore-deprecated */ $table->hasPrimaryKey() && in_array($column->getName(), $table->getPrimaryKey()->getColumns())) {

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

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

Loading history...
85
                $columnName = '<b>'.$columnName.'</b>';
86
            }
87
88
            $label .=
89
            '<tr>'
90
                . '<td border="0" align="left">'
91
                    . $columnName
92
                . '</td>'
93
                . '<td border="0" align="left">'
94
                    . '<font point-size="10">'.lcfirst($column->getType()->getName()).'</font>'
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Types\Type::getName() has been deprecated: this method will be removed in Doctrine DBAL 4.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

94
                    . '<font point-size="10">'.lcfirst(/** @scrutinizer ignore-deprecated */ $column->getType()->getName()).'</font>'

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

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

Loading history...
95
                . '</td>'
96
            . '</tr>';
97
        }
98
99
        return '<<table cellspacing="2" border="1" align="left" bgcolor="#eeeeec">'.$label.'</table>>';
100
    }
101
102
    /**
103
     * @param string $name
104
     * @param array  $options
105
     *
106
     * @return string
107
     */
108
    protected function createNode($name, $options)
109
    {
110
        $node = '';
111
112
        foreach ($options as $key => $value) {
113
            $node .= $key.'='.$value.' ';
114
        }
115
116
        return $name.' ['.$node."]\n";
117
    }
118
119
    /**
120
     * @param string $node1
121
     * @param string $node2
122
     * @param array  $options
123
     *
124
     * @return string
125
     */
126
    protected function createNodeRelation($node1, $node2, $options)
127
    {
128
        return $this->createNode($node1.' -> '.$node2, $options);
129
    }
130
131
    /**
132
     * Get Graphviz Output
133
     *
134
     * @return string
135
     */
136
    public function getOutput()
137
    {
138
        return $this->output."}";
139
    }
140
141
    /**
142
     * Writes dot language output to a file. This should usually be a *.dot file.
143
     *
144
     * You have to convert the output into a viewable format. For example use "neato" on linux systems
145
     * and execute:
146
     *
147
     *  neato -Tpng -o er.png er.dot
148
     *
149
     * @param string $filename
150
     *
151
     * @return void
152
     */
153
    public function write($filename)
154
    {
155
        file_put_contents($filename, $this->getOutput());
156
    }
157
}
158