annotate core/modules/migrate/src/Audit/AuditResult.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 1fec387a4317
children
rev   line source
Chris@14 1 <?php
Chris@14 2
Chris@14 3 namespace Drupal\migrate\Audit;
Chris@14 4
Chris@14 5 use Drupal\Component\Render\MarkupInterface;
Chris@14 6 use Drupal\migrate\Plugin\MigrationInterface;
Chris@14 7
Chris@14 8 /**
Chris@14 9 * Encapsulates the result of a migration audit.
Chris@14 10 */
Chris@14 11 class AuditResult implements MarkupInterface, \Countable {
Chris@14 12
Chris@14 13 /**
Chris@14 14 * The audited migration.
Chris@14 15 *
Chris@14 16 * @var \Drupal\migrate\Plugin\MigrationInterface
Chris@14 17 */
Chris@14 18 protected $migration;
Chris@14 19
Chris@14 20 /**
Chris@14 21 * The result of the audit (TRUE if passed, FALSE otherwise).
Chris@14 22 *
Chris@14 23 * @var bool
Chris@14 24 */
Chris@14 25 protected $status;
Chris@14 26
Chris@14 27 /**
Chris@14 28 * The reasons why the migration passed or failed the audit.
Chris@14 29 *
Chris@14 30 * @var string[]
Chris@14 31 */
Chris@14 32 protected $reasons = [];
Chris@14 33
Chris@14 34 /**
Chris@14 35 * AuditResult constructor.
Chris@14 36 *
Chris@14 37 * @param \Drupal\migrate\Plugin\MigrationInterface $migration
Chris@14 38 * The audited migration.
Chris@14 39 * @param bool $status
Chris@14 40 * The result of the audit (TRUE if passed, FALSE otherwise).
Chris@14 41 * @param string[] $reasons
Chris@14 42 * (optional) The reasons why the migration passed or failed the audit.
Chris@14 43 */
Chris@14 44 public function __construct(MigrationInterface $migration, $status, array $reasons = []) {
Chris@14 45 if (!is_bool($status)) {
Chris@14 46 throw new \InvalidArgumentException('Audit results must have a boolean status.');
Chris@14 47 }
Chris@14 48 $this->migration = $migration;
Chris@14 49 $this->status = $status;
Chris@14 50 array_walk($reasons, [$this, 'addReason']);
Chris@14 51 }
Chris@14 52
Chris@14 53 /**
Chris@14 54 * Returns the audited migration.
Chris@14 55 *
Chris@14 56 * @return \Drupal\migrate\Plugin\MigrationInterface
Chris@14 57 * The audited migration.
Chris@14 58 */
Chris@14 59 public function getMigration() {
Chris@14 60 return $this->migration;
Chris@14 61 }
Chris@14 62
Chris@14 63 /**
Chris@14 64 * Returns the boolean result of the audit.
Chris@14 65 *
Chris@14 66 * @return bool
Chris@14 67 * The result of the audit. TRUE if the migration passed the audit, FALSE
Chris@14 68 * otherwise.
Chris@14 69 */
Chris@14 70 public function passed() {
Chris@14 71 return $this->status;
Chris@14 72 }
Chris@14 73
Chris@14 74 /**
Chris@14 75 * Adds a reason why the migration passed or failed the audit.
Chris@14 76 *
Chris@14 77 * @param string|object $reason
Chris@14 78 * The reason to add. Can be a string or a string-castable object.
Chris@14 79 *
Chris@14 80 * @return $this
Chris@14 81 */
Chris@14 82 public function addReason($reason) {
Chris@14 83 array_push($this->reasons, (string) $reason);
Chris@14 84 return $this;
Chris@14 85 }
Chris@14 86
Chris@14 87 /**
Chris@14 88 * Creates a passing audit result for a migration.
Chris@14 89 *
Chris@14 90 * @param \Drupal\migrate\Plugin\MigrationInterface $migration
Chris@14 91 * The audited migration.
Chris@14 92 * @param string[] $reasons
Chris@14 93 * (optional) The reasons why the migration passed the audit.
Chris@14 94 *
Chris@14 95 * @return static
Chris@14 96 */
Chris@14 97 public static function pass(MigrationInterface $migration, array $reasons = []) {
Chris@14 98 return new static($migration, TRUE, $reasons);
Chris@14 99 }
Chris@14 100
Chris@14 101 /**
Chris@14 102 * Creates a failing audit result for a migration.
Chris@14 103 *
Chris@14 104 * @param \Drupal\migrate\Plugin\MigrationInterface $migration
Chris@14 105 * The audited migration.
Chris@14 106 * @param array $reasons
Chris@14 107 * (optional) The reasons why the migration failed the audit.
Chris@14 108 *
Chris@14 109 * @return static
Chris@14 110 */
Chris@14 111 public static function fail(MigrationInterface $migration, array $reasons = []) {
Chris@14 112 return new static($migration, FALSE, $reasons);
Chris@14 113 }
Chris@14 114
Chris@14 115 /**
Chris@14 116 * Implements \Countable::count() for Twig template compatibility.
Chris@14 117 *
Chris@14 118 * @return int
Chris@14 119 *
Chris@14 120 * @see \Drupal\Component\Render\MarkupInterface
Chris@14 121 */
Chris@14 122 public function count() {
Chris@14 123 return count($this->reasons);
Chris@14 124 }
Chris@14 125
Chris@14 126 /**
Chris@14 127 * Returns the reasons the migration passed or failed, as a string.
Chris@14 128 *
Chris@14 129 * @return string
Chris@14 130 *
Chris@14 131 * @see \Drupal\Component\Render\MarkupInterface
Chris@14 132 */
Chris@14 133 public function __toString() {
Chris@14 134 return implode("\n", $this->reasons);
Chris@14 135 }
Chris@14 136
Chris@14 137 /**
Chris@14 138 * Returns the reasons the migration passed or failed, for JSON serialization.
Chris@14 139 *
Chris@14 140 * @return string[]
Chris@14 141 */
Chris@14 142 public function jsonSerialize() {
Chris@14 143 return $this->reasons;
Chris@14 144 }
Chris@14 145
Chris@14 146 }