18F/identity-idp

View on GitHub
app/models/phone_configuration.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
# frozen_string_literal: true

class PhoneConfiguration < ApplicationRecord
  include EncryptableAttribute

  belongs_to :user, inverse_of: :phone_configurations
  validates :encrypted_phone, presence: true

  encrypted_attribute(name: :phone)

  enum delivery_preference: { sms: 0, voice: 1 }

  def formatted_phone
    PhoneFormatter.format(phone)
  end

  def masked_phone
    PhoneFormatter.mask(phone)
  end

  def selection_presenters
    options = []

    if capabilities.supports_sms?
      options << TwoFactorAuthentication::SignInPhoneSelectionPresenter.
        new(user:, configuration: self, delivery_method: :sms)
    end

    if capabilities.supports_voice?
      options << TwoFactorAuthentication::SignInPhoneSelectionPresenter.
        new(user:, configuration: self, delivery_method: :voice)
    end

    options
  end

  def capabilities
    PhoneNumberCapabilities.new(phone, phone_confirmed: !!confirmed_at?)
  end

  def friendly_name
    :phone
  end

  def self.selection_presenters(set)
    set.flat_map(&:selection_presenters)
  end
end