action = $action; $this->status = $status; $this->refusal = $refusal; } public static function message(string $refusal): string { $messages = [ self::SOURCE_CHANGED => 'The source post changed since it was read.', self::TARGET_CHANGED => 'The translation changed since it was read.', self::TARGET_MISSING => 'The translation no longer exists.', self::TARGET_PUBLISHED => 'The translation is live, and the write asked for a draft.', ]; return $messages[$refusal] ?? 'The translation was not written.'; } public static function decide( string $requestedStatus, string $sourceStatus, string $sourceHash, string $expectedSourceHash, ?Target $target, ?string $expectedTargetHash, string $desiredHash ): self { if ($sourceHash !== $expectedSourceHash) { return new self(self::REFUSE, '', self::SOURCE_CHANGED); } $publish = $requestedStatus === 'publish' && $sourceStatus === 'publish'; if ($target === null) { return $expectedTargetHash === null ? new self(self::CREATE, $publish ? 'publish' : 'draft') : new self(self::REFUSE, '', self::TARGET_MISSING); } if ( $target->hash === $desiredHash || ($target->writtenFrom === $desiredHash && $target->writtenAs === $target->hash) ) { return $publish && in_array($target->status, self::UNPUBLISHED, true) ? new self(self::UPDATE, 'publish') : new self(self::UNCHANGED, $target->status); } if ($target->hash !== $expectedTargetHash) { return new self(self::REFUSE, '', self::TARGET_CHANGED); } if (!$publish) { return in_array($target->status, self::LIVE, true) ? new self(self::REFUSE, '', self::TARGET_PUBLISHED) : new self(self::UPDATE, $target->status); } return new self( self::UPDATE, in_array($target->status, self::KEPT_WHEN_PUBLISHING, true) ? $target->status : 'publish' ); } }