1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\qa\Workflows; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\DependencyInjection\ContainerInjectionInterface; |
6
|
|
|
use Drupal\Core\Entity\EntityStorageInterface; |
7
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
8
|
|
|
use Drupal\Core\StringTranslation\StringTranslationTrait; |
9
|
|
|
use Grafizzi\Graph\Attribute; |
10
|
|
|
use Grafizzi\Graph\Edge; |
11
|
|
|
use Grafizzi\Graph\Graph; |
12
|
|
|
use Grafizzi\Graph\Node; |
13
|
|
|
use Monolog\Handler\StreamHandler; |
14
|
|
|
use Monolog\Logger; |
15
|
|
|
use Pimple\Container; |
16
|
|
|
use Pimple\Tests\Fixtures\PimpleServiceProvider; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
18
|
|
|
|
19
|
|
|
class ContentModerationGraph extends ContentModerationReportBase implements ContainerInjectionInterface { |
20
|
|
|
|
21
|
|
|
use StringTranslationTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The Pimple instance used by Grafizzi. |
25
|
|
|
* |
26
|
|
|
* @var \Pimple\Container |
27
|
|
|
*/ |
28
|
|
|
protected $pimple; |
29
|
|
|
|
30
|
|
|
public function __construct(EntityStorageInterface $stateStorage, EntityStorageInterface $transStorage, Container $pimple) { |
31
|
|
|
$this->pimple = $pimple; |
32
|
|
|
$this->stateStorage = $stateStorage; |
33
|
|
|
$this->transStorage = $transStorage; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return \Grafizzi\Graph\Graph |
38
|
|
|
*/ |
39
|
|
|
protected function buildGraph() { |
40
|
|
|
$dic = $this->pimple; |
41
|
|
|
$g = new Graph($dic); |
42
|
|
|
$states = $this->getStates(); |
43
|
|
|
$transList = $this->getTrans(); |
44
|
|
|
|
45
|
|
|
$nodes = []; |
46
|
|
|
foreach ($states as $name => $label) { |
47
|
|
|
$nodes[$name] = new Node($dic, $name, [ |
48
|
|
|
new Attribute($dic, 'label', "${label}\n${name}"), |
49
|
|
|
]); |
50
|
|
|
$g->addChild($nodes[$name]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
foreach ($transList as $name => $trans) { |
54
|
|
|
$g->addChild(new Edge($dic, |
55
|
|
|
$nodes[$trans['from']], |
56
|
|
|
$nodes[$trans['to']], |
57
|
|
|
[new Attribute($dic, 'label', "${trans['label']}\n$name")] |
58
|
|
|
)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $g; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
View Code Duplication |
protected function buildRow(string $stateId, array $transList) { |
|
|
|
|
65
|
|
|
$states = $this->getStates(); |
66
|
|
|
$trans = $this->getTrans(); |
67
|
|
|
$row = ["${states[$stateId]}\n${stateId}"]; |
68
|
|
|
foreach ($transList as $from => $transIds) { |
69
|
|
|
$cellArray = []; |
70
|
|
|
foreach ($transIds as $transId) { |
71
|
|
|
assert($trans[$transId]['from'] = $stateId); |
72
|
|
|
$transLabel = $trans[$transId]['label']; |
73
|
|
|
} |
74
|
|
|
$cellArray[] = $transLabel; |
|
|
|
|
75
|
|
|
$cellArray[] = $transId; |
|
|
|
|
76
|
|
|
$cellArray[] = ""; |
77
|
|
|
$cell = implode("\n", $cellArray); |
78
|
|
|
$row[$trans[$transId]['to']] = $cell; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $row; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public static function create(ContainerInterface $container) { |
85
|
|
|
/** @var EntityTypeManagerInterface $etm */ |
86
|
|
|
$etm = $container->get('entity_type.manager'); |
87
|
|
|
|
88
|
|
|
$stateStorage = $etm->getStorage('moderation_state'); |
89
|
|
|
$transStorage = $etm->getStorage('moderation_state_transition'); |
90
|
|
|
|
91
|
|
|
$logger = new Logger(basename(__FILE__, '.php')); |
92
|
|
|
// Change the minimum logging level using the Logger:: constants. |
93
|
|
|
$logger->pushHandler(new StreamHandler('php://stderr', Logger::INFO)); |
94
|
|
|
|
95
|
|
|
$pimple = new Container([ |
96
|
|
|
'logger' => $logger, |
97
|
|
|
'directed' => TRUE, |
98
|
|
|
]); |
99
|
|
|
|
100
|
|
|
return new static($stateStorage, $transStorage, $pimple); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function report() { |
104
|
|
|
$grid = $this->buildGrid(); |
105
|
|
|
return $this->buildGraph($grid) |
106
|
|
|
->build(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.