annotate core/modules/migrate/src/Audit/AuditResult.php @ 5:12f9dff5fda9 tip

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