ContentModerationGrid::buildGrid()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 16
rs 9.9332
cc 3
nc 4
nop 0
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 Symfony\Component\DependencyInjection\ContainerInterface;
10
11
class ContentModerationGrid implements ContainerInjectionInterface {
12
13
  use StringTranslationTrait;
14
15
  protected $stateStorage;
16
17
  protected $transStorage;
18
19
  public function __construct(EntityStorageInterface $stateStorage, EntityStorageInterface $transStorage) {
20
    $this->stateStorage = $stateStorage;
21
    $this->transStorage = $transStorage;
22
  }
23
24
  protected function buildGrid() {
25
    $grid = [];
26
    $stateIds = array_keys($this->getStates());
27
    $stateCells = array_map(function ($stateId) {
0 ignored issues
show
Unused Code introduced by
The parameter $stateId is not used and could be removed. ( Ignorable by Annotation )

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

27
    $stateCells = array_map(function (/** @scrutinizer ignore-unused */ $stateId) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
      return [];
29
    }, array_flip($stateIds));
30
31
    foreach ($stateIds as $stateId) {
32
      $grid[$stateId] = $stateCells;
33
    }
34
35
    foreach ($this->getTrans() as $transId => $trans) {
36
      $grid[$trans['from']][$trans['to']][] = $transId;
37
    }
38
39
    return $grid;
40
  }
41
42
  protected function buildRow(string $stateId, array $transList) {
43
    $states = $this->getStates();
44
    $trans = $this->getTrans();
45
    $row = ["${states[$stateId]}\n${stateId}"];
46
    foreach ($transList as $from => $transIds) {
47
      $cellArray = [];
48
      foreach ($transIds as $transId) {
49
        assert($trans[$transId]['from'] = $stateId);
50
        $transLabel = $trans[$transId]['label'];
51
      }
52
      $cellArray[] = $transLabel;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $transLabel does not seem to be defined for all execution paths leading up to this point.
Loading history...
53
      $cellArray[] = $transId;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $transId does not seem to be defined for all execution paths leading up to this point.
Loading history...
54
      $cellArray[] = "";
55
      $cell = implode("\n", $cellArray);
56
      $row[$trans[$transId]['to']] = $cell;
57
    }
58
59
    return $row;
60
  }
61
62
  public function getDuplicateTransLabels() {
63
    $labels = [];
64
    foreach ($this->getTrans() as $trans) {
65
      if (isset($labels[$trans['label']])) {
66
        $labels[$trans['label']]++;
67
      }
68
      else {
69
        $labels[$trans['label']] = 1;
70
      }
71
    }
72
    $repeatedLabels = array_filter($labels, function ($count) {
73
      return $count > 1;
74
    });
75
76
    return $repeatedLabels;
77
  }
78
79
  public static function create(ContainerInterface $container) {
80
    /** @var EntityTypeManagerInterface $etm */
81
    $etm = $container->get('entity_type.manager');
82
83
    $stateStorage = $etm->getStorage('moderation_state');
84
    $transStorage = $etm->getStorage('moderation_state_transition');
85
86
    return new static($stateStorage, $transStorage);
87
  }
88
89
  protected function getStates() {
90
    $fullStates = $this->stateStorage->loadMultiple();
91
    $simpleStates = array_map(function ($entity) {
92
      return $entity->label();
93
    }, $fullStates);
94
    asort($simpleStates);
95
    return $simpleStates;
96
  }
97
98
  protected function getTrans() {
99
    $fullTrans = $this->transStorage->loadMultiple();
100
    $simpleTrans = array_map(function ($trans) {
101
      return [
102
        'label' => $trans->label(),
103
        'from' => $trans->getFromState(),
104
        'to' => $trans->getToState(),
105
      ];
106
    }, $fullTrans);
107
108
    return $simpleTrans;
109
  }
110
111
  public function report() {
112
    $grid = $this->buildGrid();
113
114
    $rows = [];
115
116
    // Build header rows.
117
    $headerStates = array_merge(["From \\\n" => "    \\ To"], $this->getStates());
118
    $rows[] = array_map(function ($id, $label) {
119
      return "${label}\n${id}";
120
    }, array_keys($headerStates), $headerStates);
121
122
    // Build data rows.
123
    foreach ($grid as $id => $transList) {
124
      $rows[] = $this->buildRow($id, $transList);
125
    }
126
127
    // Render.
128
    drush_print_table($rows, TRUE);
0 ignored issues
show
Bug introduced by
The function drush_print_table was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

128
    /** @scrutinizer ignore-call */ 
129
    drush_print_table($rows, TRUE);
Loading history...
129
    drush_print("");
0 ignored issues
show
Bug introduced by
The function drush_print was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

129
    /** @scrutinizer ignore-call */ 
130
    drush_print("");
Loading history...
130
131
    $duplicates = $this->getDuplicateTransLabels();
132
    if (!empty($duplicates)) {
133
      $count = count($duplicates);
134
      $message = $this->formatPlural($count, "One repeated transition label: @info", "@count repeated transition labels: @info", [
135
        "@info" => implode(", ", array_keys($duplicates))
136
      ]);
137
      drush_print($message, 'warning');
138
    }
139
  }
140
141
}
142