Passed
Pull Request — 8.x-1.x (#20)
by Frédéric G.
05:58
created

ContentModerationReportBase   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 37
c 1
b 0
f 0
dl 0
loc 66
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTrans() 0 11 1
A getStates() 0 8 1
A getDuplicateTransLabels() 0 15 3
A buildGrid() 0 15 3
1
<?php
2
3
namespace Drupal\qa\Workflows;
4
5
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6
use Drupal\Core\StringTranslation\StringTranslationTrait;
7
8
abstract class ContentModerationReportBase implements ContainerInjectionInterface {
9
10
  use StringTranslationTrait;
11
12
  protected $stateStorage;
13
14
  protected $workflowStorage;
15
16
  protected function buildGrid() {
17
    $grid = [];
18
    $stateIds = array_keys($this->getStates());
19
20
    $stateCells = array_fill(0, count($stateIds), []);
21
22
    foreach ($stateIds as $stateId) {
23
      $grid[$stateId] = $stateCells;
24
    }
25
26
    foreach ($this->getTrans() as $transId => $trans) {
27
      $grid[$trans['from']][$trans['to']][] = $transId;
28
    }
29
30
    return $grid;
31
  }
32
33
  public function getDuplicateTransLabels() {
34
    $labels = [];
35
    foreach ($this->getTrans() as $trans) {
36
      if (isset($labels[$trans['label']])) {
37
        $labels[$trans['label']]++;
38
      }
39
      else {
40
        $labels[$trans['label']] = 1;
41
      }
42
    }
43
    $repeatedLabels = array_filter($labels, function ($count) {
44
      return $count > 1;
45
    });
46
47
    return $repeatedLabels;
48
  }
49
50
  protected function getStates() {
51
    $fullStates = $this->stateStorage->loadMultiple();
52
    print_R($fullStates);
53
    $simpleStates = array_map(function ($entity) {
54
      return $entity->label();
55
    }, $fullStates);
56
    asort($simpleStates);
57
    return $simpleStates;
58
  }
59
60
  protected function getTrans() {
61
    $fullTrans = $this->workflowStorage->loadMultiple();
62
    $simpleTrans = array_map(function ($trans) {
63
      return [
64
        'label' => $trans->label(),
65
        'from' => $trans->getFromState(),
66
        'to' => $trans->getToState(),
67
      ];
68
    }, $fullTrans);
69
70
    return $simpleTrans;
71
  }
72
73
  public abstract function report();
74
}
75