/** * Validates the version is a LinkedVersion and returns the class name of its migration * * @param VersionInterface $version * * @return string * * @throws InvalidArgumentException */ protected final function getMigrationClass(VersionInterface $version) { if (!$version instanceof LinkedVersionInterface) { throw new InvalidArgumentException(sprintf("Expected version %s to be linked to a migration", $version->getId())); } return get_class($version->getMigration()); }
/** * Returns true if the specified version is valid (can be added) to the collection. Otherwise, it MUST throw * an exception. * * @param VersionInterface $version * * @return bool * * @throws CollectionException * @throws \Baleen\Migrations\Exception\Version\Collection\AlreadyExistsException */ public function validate(VersionInterface $version) { if (!$version->isMigrated()) { throw new CollectionException('Invalid version specified. This collection only accepts versions that are migrated.'); } return parent::validate($version); }
/** * @inheritdoc */ public final function update(VersionInterface $version) { if ($version->isMigrated()) { $result = $this->save($version); } else { $result = $this->delete($version); } return $result; }
/** * Returns true if the operation is forced, or if the direction is the opposite to the state of the migration. * * @param VersionInterface $version * @param OptionsInterface $options * * @return bool */ protected function shouldMigrate(VersionInterface $version, OptionsInterface $options) { return $options->isForced() || $options->isDirectionUp() ^ $version->isMigrated(); // direction is opposite to state }
/** * The internal compare function. Should return less than zero (0), zero or greater than zero if the first item is * respectively less than, equal to, or greater than the second item. * * @param VersionInterface $version1 * @param VersionInterface $version2 * * @return mixed */ protected function compare(VersionInterface $version1, VersionInterface $version2) { return strcmp($version1->getId(), $version2->getId()); }
/** * Adds a new version to the collection if it doesn't exist or replaces it if it does. * * @param VersionInterface $version */ public function addOrReplace(VersionInterface $version) { $index = $this->indexOfId($version->getId()); if (null !== $index) { $this->remove($index); } $this->add($version); }
/** * @{inheritdoc} * @param VersionInterface $version * @return bool|int * @throws StorageException */ public function delete(VersionInterface $version) { $result = false; $stored = $this->fetchAll(); $element = $stored->getById($version->getId()); if ($element) { $stored->removeElement($element); $result = $this->saveCollection($stored); } return $result; }