class ConfigurationPropertyValidator {

  public static Optional<Credential> getImageCredential(
      Consumer<LogEvent> logger,
      String usernameProperty,
      String passwordProperty,
      AuthProperty auth,
      RawConfiguration rawConfiguration) {
    // System property takes priority over build configuration
    String commandlineUsername = rawConfiguration.getProperty(usernameProperty).orElse("");
    String commandlinePassword = rawConfiguration.getProperty(passwordProperty).orElse("");
    if (!commandlineUsername.isEmpty() && !commandlinePassword.isEmpty()) {
      return Optional.of(Credential.from(commandlineUsername, commandlinePassword));
    }

    // Warn if a system property is missing
    String missingProperty =
        "%s system property is set, but %s is not; attempting other authentication methods.";
    if (!commandlinePassword.isEmpty()) {
      logger.accept(
          LogEvent.warn(String.format(missingProperty, passwordProperty, usernameProperty)));
    }
    if (!commandlineUsername.isEmpty()) {
      logger.accept(
          LogEvent.warn(String.format(missingProperty, usernameProperty, passwordProperty)));
    }

    // Check auth configuration next; warn if they aren't both set
    if (!Strings.isNullOrEmpty(auth.getUsername()) && !Strings.isNullOrEmpty(auth.getPassword())) {
      return Optional.of(Credential.from(auth.getUsername(), auth.getPassword()));
    }

    String missingConfig = "%s is missing from build configuration; ignoring auth section.";
    if (!Strings.isNullOrEmpty(auth.getPassword())) {
      logger.accept(LogEvent.warn(String.format(missingConfig, auth.getUsernameDescriptor())));
    }
    if (!Strings.isNullOrEmpty(auth.getUsername())) {
      logger.accept(LogEvent.warn(String.format(missingConfig, auth.getPasswordDescriptor())));
    }

    return Optional.empty();
  }

  private  ConfigurationPropertyValidator();

}